【发布时间】:2012-06-29 15:17:48
【问题描述】:
如何使用 PHP 发送带有图片的 HTML 格式的电子邮件?
我想要一个包含一些设置和 HTML 输出的页面,通过电子邮件发送到一个地址。我该怎么办?
主要问题是附加文件。我该怎么做?
【问题讨论】:
-
您的问题是发送
HTML代码吗?还是一般使用 PHP 发送电子邮件?
如何使用 PHP 发送带有图片的 HTML 格式的电子邮件?
我想要一个包含一些设置和 HTML 输出的页面,通过电子邮件发送到一个地址。我该怎么办?
主要问题是附加文件。我该怎么做?
【问题讨论】:
HTML 代码吗?还是一般使用 PHP 发送电子邮件?
这很简单,把图片留在服务器上,然后将 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";
这是我得到信息的链接.. (link...)
不过,你需要安全……
【讨论】:
html、head 或 body 元素,否则您将在实际上遵循 (X)HTML 标准的基于 Web 的电子邮件系统上造成绝对混乱。另外 if 发送 HTML 电子邮件 never 发送大于 h2 的标头级别,因为您的电子邮件主题(在适当的系统上)将在页面上的单个 h1 元素中.如果这没有意义,请记住 Google 试图将您的页面解释为只有一个主要标题(例如“我们赢得了战争!”)和许多较小标题(例如“被打扰的驼鹿拯救的小猫”)的报纸。
您需要使用图像的绝对路径对 html 进行编码。通过绝对路径意味着您必须将图像上传到服务器中,并且在图像的src 属性中您必须提供像这样<img src="http://yourdomain.com/images/example.jpg"> 这样的直接路径。
以下是供您参考的 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);
?>
【讨论】:
我有这个代码,它可以完美地运行在我的网站上
public function forgotpassword($pass,$name,$to)
{
$body ="<table width=100% border=0><tr><td>";
$body .= "<img width=200 src='";
$body .= $this->imageUrl();
$body .="'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
$body .='<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
$body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
$body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:support@pms.com" target="_blank">support@pms.com</a></td></tr>';
$body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
$subject = "Forgot Password";
$this->sendmail($body,$to,$subject);
}
邮件功能
function sendmail($body,$to,$subject)
{
//require_once 'init.php';
$from='testing@gmail.com';
$headersfrom='';
$headersfrom .= 'MIME-Version: 1.0' . "\r\n";
$headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headersfrom .= 'From: '.$from.' '. "\r\n";
mail($to,$subject,$body,$headersfrom);
}
image url 函数用于如果你想改变你只在一个函数中改变的图像我有很多邮件功能,比如忘记密码在那里创建用户,因为我使用 image url 函数你可以直接设置路径。
function imageUrl()
{
return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
}
【讨论】:
发送 html 电子邮件与使用 php 发送普通电子邮件没有太大区别。需要添加的是 php mail() 函数的 header 参数中的内容类型。这是一个例子。
<?php
$to = "toEmail@domain.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>A table as email</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Fname</td>
<td>Sname</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
$headers .= 'From: name' . "\r\n";
mail($to,$subject,$message,$headers);
?>
您也可以查看here 了解 w3schools 的更详细说明
【讨论】:
您可以通过 PHP 轻松发送带有 HTML 内容的电子邮件。使用以下脚本。
<?php
$to = 'user@example.com';
$subject = "Send HTML Email Using PHP";
$htmlContent = '
<html>
<body>
<h1>Send HTML Email Using PHP</h1>
<p>This is a HTMl email using PHP by CodexWorld</p>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: CodexWorld<info@codexworld.com>' . "\r\n";
$headers .= 'Cc: welcome@example.com' . "\r\n";
$headers .= 'Bcc: welcome2@example.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
源代码和现场演示可以从这里找到 - Send Beautiful HTML Email using PHP
【讨论】:
最简单的方法可能是使用 Zend Framework 或任何其他框架,如 CakePHP 或 Symphony。
您也可以使用标准的mail 函数来做到这一点,但您需要更多关于如何附加图片的知识。
或者,只需将图像托管在服务器上,而不是附加它们。发送 HTML 邮件记录在 mail 函数文档中。
【讨论】:
mail() 就可以了。如果您需要更可靠的解决方案或有复杂的情况或需求,最好依赖已经通过数十个 RFC 文档和多年错误修复的框架或库。如果您能找到一个专门的库或框架让您挑选您想要的功能,那么就没有理由依赖我们自己来解决所有问题。
使用 PHPMailer,
要发送 HTML 邮件,您只需设置 $mail->isHTML(),您可以使用 HTML 标签设置正文
这是一个写得很好的教程:
rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/
【讨论】:
诀窍是在构建 html 正文部分时知道 Image mime 部分的内容 id。 归结为制作img标签
https://github.com/horde/horde/blob/master/kronolith/lib/Kronolith.php
查看函数 buildMimeMessage 以获得一个工作示例。
【讨论】: