提问者:小点点

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


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

我想有一个网页与一些设置和HTML输出,通过电子邮件发送到一个地址。我该怎么办?

主要问题是附加文件。我怎么能那样做?


共2个答案

匿名用户

这很简单,把图像留在服务器上,然后把PHP+CSS发送给他们...

$to = 'bob@example.com';

$subject = 'Website Change Reqest';

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

这是我得到的信息的链接。(链接...)

但你需要安全保障...

匿名用户

您需要使用图像的绝对路径对html进行编码。绝对路径意味着您必须在服务器中上传图像,并且在图像的src属性中,您必须给出如下的直接路径

下面是供您参考的PHP代码:-摘自http://www.PHP.net/manual/en/function.mail.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);
?>