我无法用我的 phpmailer 发送 pdf 附件
I cannot send pdf attachment with my phpmailer
我的 php 邮件程序出现附件问题。所有信息(电子邮件、姓名、pdf_link 等)都是正确的,我可以收到电子邮件,但附件中没有 pdf。
能否请你检查我的代码并告诉我我的错误是什么,我希望你能帮助我。
//mailing starts
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Timeout = 20;
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = 'my-host-information';
$mail->Port = 465;//587
$mail->SMTPSecure = 'ssl';//tls
$mail->SMTPAuth = true;
$mail->Username = "myemail@mywebsite.com";
$mail->Password = "****";
//Set who the message is to be sent from
$mail->setFrom('myemail@mywebsite.com', 'Name here');
$mail->addAddress($member_email, '');
$mail->AddAttachment($pdf_link, $name = $image_name, $encoding = 'base64', $type = 'application/pdf');
$message_it = 'Grazie per il tuo interesse. La tua fattura proforma è stata predisposta e la trovi in allegato. Puoi visualizzarlo sul nostro sistema .<br><br>Cordiali Saluti,<br>My Company';
$message_en = 'Thanks for your interest. Your proforma invoice has been prepared and you can find it attached. You can view it on our system. <br> <br> Best Regards, <br> My Company ';
$combined_name_it = 'Caro ' .$member_name.' '. $member_company;
$combined_email = $email;
$combined_message_it = $message_it;
$combined_name_en = 'Dear ' .$member_name.' '. $member_company;
$combined_email = $email;
$combined_message_en = $message_en;
if($email_lang == 'it'){
$mail->isHTML(true);
$mail->Subject = 'La tua proforma è pronta '; // Email subject
$mail->Body = "$combined_name_it,$combined_email<br /><br />$combine_message_it"; // Mail Content
$mail->CharSet = 'utf-8';
}else{
$mail->isHTML(true);
$mail->Subject = 'Your proforma is ready.';
$mail->Body = "$combined_name_en<br />$combined_email<br /><br />$combined_message_en";
$mail->CharSet = 'utf-8';
}
$result = $mail->send();
if($result){
$session = \Config\Services::session();
$session->setFlashdata('success', 'Proforma updated');
return redirect()->to($_SERVER['HTTP_REFERER']);
}else{
$data['validation'] = $this->validator;
$data['title'] = 'Proforma Update';
echo view('admin/assets/header', $data);
return redirect()->to($_SERVER['HTTP_REFERER']);
}
//mailing ends
我不确定您习惯使用哪种语言,但这很奇怪 PHP:
$mail->AddAttachment($pdf_link, $name = $image_name, $encoding = 'base64', $type = 'application/pdf');
尝试将其重写为:
$mail->addAttachment($pdf_link, $image_name, 'base64', 'application/pdf');
接下来,您没有检查该调用的 return 值,因此您不知道它是否成功,所以这样做:
if (!$mail->addAttachment($pdf_link, $image_name, 'base64', 'application/pdf')) {
echo 'Attachment failed';
}
如果确实失败了,你需要找出原因。通常的原因是:
- 文件不存在
- 文件路径不正确
- 您没有读取权限
- 文件的所有权错误
您需要检查它是哪一个。
总的来说,您的代码中几乎没有错误检查或异常处理,因此如果出现问题,它只会中断,您不会知道原因。查看 PHPMailer 提供的示例,并确保您使用的是最新版本。
解决问题后,我可以说问题出在 PhpMailer 预期会看到 url 的文件夹部分,而不是基本文件夹部分。当我包含包含 http:// mywebsites.com 等部分的 base_url() 时,它会继续发送没有附件的电子邮件。但是,当我用文件夹部分替换它时,假设“uploads/invoices/invoice-1.pdf”有效。
感谢@Synchro
我的 php 邮件程序出现附件问题。所有信息(电子邮件、姓名、pdf_link 等)都是正确的,我可以收到电子邮件,但附件中没有 pdf。
能否请你检查我的代码并告诉我我的错误是什么,我希望你能帮助我。
//mailing starts
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Timeout = 20;
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = 'my-host-information';
$mail->Port = 465;//587
$mail->SMTPSecure = 'ssl';//tls
$mail->SMTPAuth = true;
$mail->Username = "myemail@mywebsite.com";
$mail->Password = "****";
//Set who the message is to be sent from
$mail->setFrom('myemail@mywebsite.com', 'Name here');
$mail->addAddress($member_email, '');
$mail->AddAttachment($pdf_link, $name = $image_name, $encoding = 'base64', $type = 'application/pdf');
$message_it = 'Grazie per il tuo interesse. La tua fattura proforma è stata predisposta e la trovi in allegato. Puoi visualizzarlo sul nostro sistema .<br><br>Cordiali Saluti,<br>My Company';
$message_en = 'Thanks for your interest. Your proforma invoice has been prepared and you can find it attached. You can view it on our system. <br> <br> Best Regards, <br> My Company ';
$combined_name_it = 'Caro ' .$member_name.' '. $member_company;
$combined_email = $email;
$combined_message_it = $message_it;
$combined_name_en = 'Dear ' .$member_name.' '. $member_company;
$combined_email = $email;
$combined_message_en = $message_en;
if($email_lang == 'it'){
$mail->isHTML(true);
$mail->Subject = 'La tua proforma è pronta '; // Email subject
$mail->Body = "$combined_name_it,$combined_email<br /><br />$combine_message_it"; // Mail Content
$mail->CharSet = 'utf-8';
}else{
$mail->isHTML(true);
$mail->Subject = 'Your proforma is ready.';
$mail->Body = "$combined_name_en<br />$combined_email<br /><br />$combined_message_en";
$mail->CharSet = 'utf-8';
}
$result = $mail->send();
if($result){
$session = \Config\Services::session();
$session->setFlashdata('success', 'Proforma updated');
return redirect()->to($_SERVER['HTTP_REFERER']);
}else{
$data['validation'] = $this->validator;
$data['title'] = 'Proforma Update';
echo view('admin/assets/header', $data);
return redirect()->to($_SERVER['HTTP_REFERER']);
}
//mailing ends
我不确定您习惯使用哪种语言,但这很奇怪 PHP:
$mail->AddAttachment($pdf_link, $name = $image_name, $encoding = 'base64', $type = 'application/pdf');
尝试将其重写为:
$mail->addAttachment($pdf_link, $image_name, 'base64', 'application/pdf');
接下来,您没有检查该调用的 return 值,因此您不知道它是否成功,所以这样做:
if (!$mail->addAttachment($pdf_link, $image_name, 'base64', 'application/pdf')) {
echo 'Attachment failed';
}
如果确实失败了,你需要找出原因。通常的原因是:
- 文件不存在
- 文件路径不正确
- 您没有读取权限
- 文件的所有权错误
您需要检查它是哪一个。
总的来说,您的代码中几乎没有错误检查或异常处理,因此如果出现问题,它只会中断,您不会知道原因。查看 PHPMailer 提供的示例,并确保您使用的是最新版本。
解决问题后,我可以说问题出在 PhpMailer 预期会看到 url 的文件夹部分,而不是基本文件夹部分。当我包含包含 http:// mywebsites.com 等部分的 base_url() 时,它会继续发送没有附件的电子邮件。但是,当我用文件夹部分替换它时,假设“uploads/invoices/invoice-1.pdf”有效。
感谢@Synchro