【问题标题】:Make PHP send an email with an inline image attachment让 PHP 发送带有内联图像附件的电子邮件
【发布时间】:2011-11-22 06:29:00
【问题描述】:

不使用 PHPMailer、Swiftmailer、PEAR、Zend_mail 或任何其他库,我想发送一封内嵌图片附件的电子邮件。

这里的重要部分是将它内联:我已经能够做其他所有事情了。

内联意味着它可以被电子邮件中的HTML在图像标签中使用。

我真的不想使用 PHPMailer 或类似的东西——我不是唯一一个试图弄清楚如何在 stackoverflow 上做到这一点的人,到目前为止,我所看到的所有问题都只是关于他们为什么应该使用 PEAR 或 Zend_mail 或其他东西的争论。我不想那样做,也不想为此争论。

【问题讨论】:

  • 好吧,你可以这样做。这只是一大堆毫无意义的努力。有一些用户提供了完整的胡言乱语来完成它。更努力地搜索。 (此处您的问题中没有显示任何代码:NARQ 来自我的投票。)
  • 首先阅读 MIME 邮件格式,然后从那里开始。这些库简单、易于使用且可靠。如果您想经历从头开始构建自己的 mime 消息的痛苦,那就去做吧,但当它不起作用时不要哭泣。
  • 谢谢。我在使用他们的代码时遇到了问题,给出了:解析错误:语法错误,代码末尾出现意外的 $end,这不是因为大括号不匹配或缺少“;”
  • 这可能是由于heredoc语法。确保在 EOBODY 之前没有任何空格;线。见这里:php.net/manual/en/…

标签: php email


【解决方案1】:

这是你要找的吗??

<?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";
?> 

【讨论】:

    【解决方案2】:

    我不知道这是否是您想要的,但是使用 PHP 中的 mail() 函数以 HTML 格式发送电子邮件很容易:

    http://php.net/manual/en/function.mail.php

    在示例中,有一个带有 HTML

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 2020-11-20
      • 2014-02-28
      • 2017-09-06
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多