【发布时间】:2020-03-06 18:30:59
【问题描述】:
我编写了以下代码,并且电子邮件发送正常,但我无法弄清楚为什么电子邮件不包含预期的附件(pdf 文件)。
我也尝试了 $mail->attach with MIME,结果出现 500 内部服务器错误。
$pdf = public_path() . '/assets/attachments/TermSheet.pdf';
$mail = new PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = '';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
//ENV VARIABLES
$mail->Host = env("MAIL_HOST");
$mail->Port = env("MAIL_PORT");
$mail->Username = env("MAIL_USERNAME");
$mail->Password = env("MAIL_PASSWORD");
$mail->SetFrom($fromemail, $fromname);
$mail->Subject = $emailsubject;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $emailsubject;
$mail->addAttachment($pdf);
$mail->Body = $initbody;
$mail->AddAddress($user);
if(isset($ccemaillist)){ //cc
$ccemail = explode(";",$ccemaillist);
foreach ($ccemail as $toccmail) {
$mail->AddAddress($toccmail);
}
}
if($mail->Send()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $currentapp.'/mail/send?api_user_id='.$creatorid);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"recipient_email=$user&email_type=WelcomeEmail&sys_status=1&creator=$creatorid");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
return "SUC";
}
我不确定为什么它不起作用,对此我将不胜感激。提前谢谢你:)
【问题讨论】:
-
仔细检查您的路径。
addAttachment如果无法访问该文件,将返回false。 -
路径正确,目前我可以通过 $mail->addStringAttachment($attachment, $filename); 获取附件但文件损坏了
-
好的,你是说
var_dump($mail->addAttachment($pdf));显示true?另外,为什么不使用addCC作为您的抄送地址? -
是的,这是 cc 的计划,这是我根据一些教程编写的代码。现在我的重点是修复电子邮件,我终于设法做到了^^ 解决方案:$mail->addStringAttachment(file_get_contents($attachment), $filename);其中 $attachments 是文件的完整路径。
-
addAttachment就是这样做的。使用正确的功能。
标签: laravel phpmailer attachment