通过 PHP 在电子邮件中发送 HTML

新手上路,请多包涵

如何使用 PHP 发送带有图片的 HTML 格式的电子邮件?

我想要一个包含一些设置和 HTML 输出的页面,该页面通过电子邮件发送到一个地址。我应该怎么办?

主要问题是附加文件。我怎样才能做到这一点?

原文由 Abadis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 267
2 个回答

这很简单。将图像留在服务器上并将 PHP + CSS 发送给它们……

 $to = 'bob@example.com';

$subject = 'Website Change Request';

$headers  = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';

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

正是这一行告诉寄件人和收件人电子邮件包含(希望)格式良好的 HTML,它需要解释:

 $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

这是我从中获得信息的链接…( 链接

不过你需要安全……

原文由 Chris 发布,翻译遵循 CC BY-SA 4.0 许可协议

您需要使用图像的绝对路径对 HTML 内容进行编码。通过 绝对 路径,我的意思是您必须将图像上传到服务器,并且在图像的 src 属性中,您必须提供直接路径,就像这样 <img src="http://yourdomain.com/images/example.jpg">

以下是供您参考的 PHP 代码:取自 _邮件_:

 <?php
    // Multiple recipients
    $to  = 'aidan@example.com' . ', '; // Note the comma
    $to .= 'wez@example.com';

    // Subject
    $subject = 'Birthday Reminders for August';

    // Message
    $message = '
      <p>Here are the birthdays upcoming in August!</p>
    ';

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

    // Additional headers
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";

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

原文由 Subhajit 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏