Flo*_*ler 11 html php email attachment
我遇到了一个问题:直到今天,我使用包含的标头发送了带有PHP的HTML邮件
Content-type: text/html;
Run Code Online (Sandbox Code Playgroud)
现在,我添加了添加附件的功能.为此,我不得不改变这一行
Content-Type: multipart/mixed;
Run Code Online (Sandbox Code Playgroud)
现在,multipart/mixed
其余的邮件,正常文本,就像文本/普通文本一样显示.我如何才能意识到附件是有效的,而mailtext仍然是HTML?
Bri*_*ian 27
我试了几个小时的答案1没有运气.我在这里找到了一个解决方案:http: //www.finalwebsites.com/forums/topic/php-e-mail-attachment-script
像魅力一样工作 - 不到5分钟!你可能想要改变(像我一样),第一个内容类型从text/plain到text/html.
这是我稍微修改过的版本来处理多个附件:
function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
foreach ($files as $filename) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
}
$header .= "--".$uid."--";
return mail($mailto, $subject, "", $header);
}
Run Code Online (Sandbox Code Playgroud)
小智 10
要发送带附件的电子邮件,我们需要使用multipart/mixed MIME类型,指定混合类型将包含在电子邮件中.此外,我们希望使用multipart/alternative MIME类型来发送电子邮件的纯文本和HTML版本.看看示例:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Run Code Online (Sandbox Code Playgroud)
如您所见,发送带附件的电子邮件很容易实现.在前面的示例中,我们有多部分/混合MIME类型,在其中我们有多部分/替代MIME类型,指定电子邮件的两个版本.要在我们的消息中包含附件,我们将指定文件中的数据读入字符串,使用base64对其进行编码,将其拆分为较小的块以确保它与MIME规范匹配,然后将其作为附件包含在内.
归档时间: |
|
查看次数: |
60627 次 |
最近记录: |