gpt4 book ai didi

php - PHP Mail() 中的 Base 64 附件不起作用

转载 作者:行者123 更新时间:2023-12-05 03:11:52 24 4
gpt4 key购买 nike

enter image description here

我有一个脚本,它会在函数运行时自动发送电子邮件。我希望能够发送 HTML 电子邮件和 PDF 附件。我知道我需要将文件编码为 Base64,但是我只是将 base64 代码附加到我的电子邮件底部。我认为这与 mime 相关。有人看到这个问题吗?

    $to = 'example@example.com';

$subject = 'test!';

$file = file_get_contents("files/CAPS-Standing-Order.pdf");
$encoded_file = chunk_split(base64_encode($file));

// message
$boundary = md5("sanwebe");

$message = 'Hello';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: CAPS Consortium <contact@capsconsortium.com>' . "\r\n";

$message .= "--$boundary\r\n";
$message .="Content-Type: pdf; name=\"CAPS-Standing-Order.pdf\"\r\n";
$message .="Content-Disposition: attachment; filename=\"CAPS-Standing-Order.pdf\"\r\n";
$message .="Content-Transfer-Encoding: base64\r\n";
$message .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$message .= $encoded_file;

// Mail it
mail($to, $subject, $message, $headers);

最佳答案

<?php

$file = file_get_contents("files/CAPS-Standing-Order.pdf");
$encoded_file = chunk_split(base64_encode($file));

$attachments[] = array(
'name' => 'CAPS-Standing-Order.pdf', // Set File Name
'data' => $encoded_file, // File Data
'type' => 'application/pdf', // Type
'encoding' => 'base64' // Content-Transfer-Encoding
);

$this->sendMail("example@example.com", "Hello", "test!", $attachments);
// Send the actual mail and include the attachments

我做的发送更干净邮件的功能

<?php
function sendMail($email = "", $text = "", $subject = "", $attachments = array()) {
if(!$email || !$text) {
return false;
}

$headers = array();
$headers[] = "To: {$email}";
$headers[] = "From: CAPS Consortium <contact@capsconsortium.com>";
$headers[] = "Reply-To: CAPS Consortium <contact@capsconsortium.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

$headers[] = "MIME-Version: 1.0";

if(!empty($attachments)) {
$boundary = md5(time());
$headers[] = "Content-type: multipart/mixed;boundary=\"".$boundary."\"";
// Have attachment, different content type and boundary required.
} else {
$headers[] = "Content-type: text/html; charset=UTF-8";
}

$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CAPS Consortium</title>
<style>table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }</style>
</head>
<body style="font-family: arial;" width="100%">
[text]
</body>
</html>';

$generated = date('jS M Y H:i:s');
$subject = ($subject ? $subject : 'Default Subject');
$message = $html;

$message = str_replace("[text]", $text, $message);

if(!empty($attachments)) {
$output = array();
$output[] = "--".$boundary;
$output[] = "Content-type: text/html; charset=\"utf-8\"";
$output[] = "Content-Transfer-Encoding: 8bit";
$output[] = "";
$output[] = $message;
$output[] = "";
foreach($attachments as $attachment) {
$output[] = "--".$boundary;
$output[] = "Content-Type: ".$attachment['type']."; name=\"".$attachment['name']."\";";
if(isset($attachment['encoding'])) {
$output[] = "Content-Transfer-Encoding: " . $attachment['encoding'];
}
$output[] = "Content-Disposition: attachment;";
$output[] = "";
$output[] = $attachment['data'];
$output[] = "";
}
return mail($email, $subject, implode("\r\n", $output), implode("\r\n", $headers));
} else {
return mail($email, $subject, $message, implode("\r\n", $headers));
}

}

希望这对您有所帮助。不需要太多解释,因为它几乎就是您所拥有的,只是更清洁且更易于维护。

关于php - PHP Mail() 中的 Base 64 附件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com