如何使用PHP发送带图片的HTML格式电子邮件?
我想要一个包含一些设置和HTML输出的页面,并通过电子邮件发送到某个地址。我该怎么做?
主要的问题是如何附加文件。我应该怎么做?
这很简单。把图片留在服务器上,然后将 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";
这里是我获取信息的链接... (链接)
但你需要考虑安全问题...
html
、head
或者body
元素,会在遵循(X)HTML标准的基于Web的邮件系统中造成绝对的混乱。此外,如果发送HTML电子邮件,则绝不要发送比h2
更大的标题级别,因为在页面上,您的电子邮件主题(在正确的系统上)将作为单个h1
元素呈现。如果这听起来不太清楚,请记住,Google试图将您的页面解释为报纸,其中只有一个主标题(例如,“我们赢得了战争!”)和许多较小的标题(例如,“被恶意麋鹿救出的小猫”)。 - Johntext = text.replace('\r\n', '\n')
这将把所有Windows风格的换行符替换为Unix风格的换行符。 - luky您需要使用图像的绝对路径编写HTML内容。在这里,绝对路径指的是您必须将图像上传到服务器,并在图像的src
属性中提供直接路径,例如:<img src="http://yourdomain.com/images/example.jpg">
。
以下是PHP代码,供您参考。它来自于mail:
<?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);
}
如果需要更改图片,可以使用图像URL函数。只需更改一次该函数即可完成更改。我有很多邮件功能,例如忘记密码或创建用户等。因此,我正在使用图像URL函数。您可以直接设置路径。
function imageUrl()
{
return "http://" . $_SERVER['SERVER_NAME'] . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/") + 1) . "images/capacity.jpg";
}
<?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);
?>
<?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;
?>
源代码和现场演示可以在这里找到 - 使用PHP发送美观的HTML电子邮件
mail
函数,但需要更多关于如何附加图片的知识。mail()
就可以了。如果您需要更可靠的解决方案或者有复杂的情况或需求,最好依赖于已经通过数十个 RFC 文档和多年的错误修复的框架或库。如果您能找到一个专门的库或框架,让您选择所需的功能,那么没有理由依赖于自己来解决所有问题。 - Martijn在构建HTML正文时,关键是要知道图像MIME部分的内容id。
简而言之,就是使img标签为—<img src="cid:输入内容id" />
请查看函数buildMimeMessage以获取一个可工作的示例。
HTML
代码吗?还是总的来说使用 PHP 发送电子邮件? - Adi