【发布时间】:2021-06-16 03:15:50
【问题描述】:
我的函数的第一部分使用 PHP 的 fputcsv 生成一个 CSV 文件,然后我想使用我也安装的 PHPMailer 通过电子邮件发送该 CSV。
从数组生成 CSV 文件。
$output = fopen("php://output",'w') or die("Can't open php://output");
// Add column headings.
fputcsv($output, array('SKU', 'ASIN', 'Current Rating', 'Previous Week', 'Difference', 'Category', 'Total Ratings'));
foreach( $ratings as $rating ) {
fputcsv( $output, $rating );
}
fclose($output) or die("Can't close php://output");
通过 PHPMailer 发送电子邮件
// Create a new PHPMailer instance
$mail = new \PHPMailer\PHPMailer\PHPMailer();
// Tell PHPMailer to use SMTP
$mail->isSMTP();
// Set the hostname of the mail server
$mail->Host = 'smtp.eu.mailgun.org';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
// Set who the message is to be sent from
$mail->setFrom('', '');
$mail->Subject = 'Weekly ASIN Ratings Report';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
// Attach an image file
$mail->addAttachment('');
// Send the message, send errors to Sentry.
if (!$mail->send()) {
\Sentry\captureMessage( $mail->ErrorInfo );
} else {
echo 'Message sent!';
}
我尝试了什么
$mail->addAttachment($output);
我收到了电子邮件,但根本没有附件。
【问题讨论】:
-
@cOle2 -
$mail->addStringAttachment($output, 'report.csv');- “警告:base64_encode() 期望参数 1 是字符串,资源给定” -
php://output 发送到浏览器。你可能想要 php://memory 代替。
-
要么将 CSV 数据写入本地文件并将其路径传递给
addAttachment(),要么获取字符串中的输出并将其传递给addStringAttachment()。