使用PHP发送多个附件的电子邮件可以通过以下步骤实现:
mail()
或者使用第三方库如PHPMailer来发送邮件。$to = "recipient@example.com";
$subject = "多个附件的邮件";
$message = "这是一封包含多个附件的邮件。";
$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";
multipart/mixed
类型的邮件可以同时包含文本和附件。例如:$body = "--boundary\r\n";
$body .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message."\r\n";
// 添加附件
$attachment1 = "/path/to/attachment1.pdf";
$attachment2 = "/path/to/attachment2.jpg";
$body .= "--boundary\r\n";
$body .= "Content-Type: application/pdf; name=\"attachment1.pdf\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment\r\n\r\n";
$body .= chunk_split(base64_encode(file_get_contents($attachment1)))."\r\n";
$body .= "--boundary\r\n";
$body .= "Content-Type: image/jpeg; name=\"attachment2.jpg\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment\r\n\r\n";
$body .= chunk_split(base64_encode(file_get_contents($attachment2)))."\r\n";
$body .= "--boundary--\r\n";
在上面的示例中,我们使用了两个附件,一个是PDF文件,另一个是JPEG图片。你需要将$attachment1
和$attachment2
替换为实际的附件路径。
mail()
函数或者PHPMailer发送邮件。例如:if (mail($to, $subject, $body, $headers)) {
echo "邮件发送成功!";
} else {
echo "邮件发送失败!";
}
如果你使用的是PHPMailer,可以参考其文档来发送带有附件的邮件。
总结: 通过以上步骤,你可以使用PHP发送包含多个附件的电子邮件。确保你的PHP环境已经配置好了邮件发送功能,并按照上述步骤设置邮件参数、创建邮件主体部分和发送邮件。
领取专属 10元无门槛券
手把手带您无忧上云