返回

如何通过 PHP Mail() 发送带有 PDF 附件的电子邮件?

php

通过 PHP Mail() 发送带附件的电子邮件:PDF 附件

简介

在电子通信时代,发送带有附件的电子邮件已成为日常工作的一部分。PHP Mail() 函数提供了一种简单的方法来发送电子邮件,但它并不原生支持附件。本指南将详细介绍如何使用 PHP Mail() 函数发送带有 PDF 附件的电子邮件。

前提条件

  • PHP 5.6 或更高版本
  • SMTP 服务器(例如 Gmail 或 Outlook)

步骤

1. 配置 SMTP 服务器

首先,需要配置 PHP Mail() 函数以连接到 SMTP 服务器。

ini_set('SMTP', 'smtp.gmail.com');
ini_set('smtp_port', '465'); // 可能为 25、465 或 587
ini_set('sendmail_from', 'your@email.address');
ini_set('SMTPAuth', 'true');
ini_set('SMTPUsername', 'your@email.address');
ini_set('SMTPPassword', 'yourpassword');

2. 设置邮件头信息

接下来,设置邮件头信息,包括收件人、主题和邮件正文。还要指定 MIME 类型为 multipart/mixed 以包含附件。

$to = "recipient@email.address";
$subject = "邮件主题";
$message = "邮件正文";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";

3. 创建附件

对于 PDF 附件,使用 PHP 的 file_get_contents() 函数读取 PDF 文件的内容。

$attachment = file_get_contents("/path/to/file.pdf");

4. 生成附件头信息

为附件生成头信息,包括文件名、MIME 类型和编码。

$attachment_headers = "Content-Type: application/pdf\r\n";
$attachment_headers .= "Content-Transfer-Encoding: base64\r\n";
$attachment_headers .= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\r\n";

5. 将附件添加到邮件中

使用边界将附件添加到电子邮件中。

$boundary = md5(uniqid(time()));
$message .= "--{$boundary}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
$message .= $message . "\r\n";
$message .= "--{$boundary}\r\n";
$message .= $attachment_headers;
$message .= chunk_split(base64_encode($attachment));
$message .= "\r\n\r\n--{$boundary}--";

6. 发送电子邮件

最后,使用 mail() 函数发送电子邮件。

mail($to, $subject, $message, $headers);

示例代码

以下示例代码演示了如何发送带有 PDF 附件的电子邮件:

<?php
ini_set('SMTP', 'smtp.gmail.com');
ini_set('smtp_port', '465');
ini_set('sendmail_from', 'your@email.address');
ini_set('SMTPAuth', 'true');
ini_set('SMTPUsername', 'your@email.address');
ini_set('SMTPPassword', 'yourpassword');
$to = "recipient@email.address";
$subject = "邮件主题";
$message = "邮件正文";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";
$attachment = file_get_contents("/path/to/file.pdf");
$attachment_headers = "Content-Type: application/pdf\r\n";
$attachment_headers .= "Content-Transfer-Encoding: base64\r\n";
$attachment_headers .= "Content-Disposition: attachment; filename=\"file.pdf\"\r\n\r\n";
$boundary = md5(uniqid(time()));
$message .= "--{$boundary}\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n";
$message .= $message . "\r\n";
$message .= "--{$boundary}\r\n";
$message .= $attachment_headers;
$message .= chunk_split(base64_encode($attachment));
$message .= "\r\n\r\n--{$boundary}--";
mail($to, $subject, $message, $headers);
?>

常见问题解答

1. 我可以在电子邮件中附加多个文件吗?
是的,通过使用附加边界,可以在电子邮件中附加多个文件。

2. 我可以使用 PHP Mail() 发送其他类型的附件吗?
是的,除了 PDF 之外,还可以使用 PHP Mail() 发送其他类型的附件,如图像、文档和视频。

3. 如果我的 SMTP 服务器要求 SSL 加密怎么办?
可以通过在 SMTP 端口前添加 "ssl://" 或在 ini_set() 中设置 "openssl.ssl.verify_peer" 为 false 来启用 SSL 加密。

4. 我收到 "邮件发送失败" 错误,该怎么办?
检查 SMTP 服务器设置是否正确,并确保你的电子邮件帐户已启用。

5. 我可以在发送电子邮件之前预览电子邮件吗?
可以使用 PHP 的 var_dump() 函数或邮件查看器来预览电子邮件。