我正在尝试从表单提交创建CSV文件,并通过电子邮件自动将该文件发送给特定用户。电子邮件本身发送得很好,但我无法通过附件。是否可以在不首先将文件保存到服务器的情况下创建附件?
function create_csv() {
$fd = fopen(\'php://temp/\', \'w\');
if($fd === FALSE) {
die(\'Failed to open temporary file\');
}
$records = array( array(\'dummy\', \'data\', \'found\', \'here\') );
fputcsv($fd, $headers);
foreach($records as $record) {
fputcsv($fd, $record);
}
rewind($fd);
$csv = stream_get_contents($fd);
fclose($fd);
return $csv;
}
$to = $email;
$subject = \'Subject\';
$message = \'Message\';
$headers = \'From: \' . $other_email;
$attachment = create_csv();
$sent = wp_mail($to, $subject, $message, $headers, $attachment);
SO网友:Saurabh Shukla
这是因为wp\\U mail希望附件是一个文件名(filepath),它可以附加并发送。您正在提供一个包含文件内容的字符串:
function create_csv() {
$filepath = \'/path/to/the/file.csv\';
$fd = fopen($filepath, \'w\');
if($fd === FALSE) {
die(\'Failed to open temporary file\');
}
$records = array( array(\'dummy\', \'data\', \'found\', \'here\') );
fputcsv($fd, $headers);
foreach($records as $record) {
fputcsv($fd, $record);
}
rewind($fd);
fclose($fd);
return $filepath;
}
会解决你的问题。看见
http://codex.wordpress.org/Function_Reference/wp_mail