通过 PHP 在电子邮件中发送 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11238953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send HTML in email via PHP
提问by Abadis
How can I send an HTML-formatted email with pictures using PHP?
如何使用 PHP 发送带有图片的 HTML 格式的电子邮件?
I want to have a page with some settings and HTML output which is sent via email to an address. What should I do?
我想要一个包含一些设置和 HTML 输出的页面,该页面通过电子邮件发送到某个地址。我该怎么办?
The main problem is to attach files. How can I do that?
主要问题是附加文件。我怎样才能做到这一点?
回答by Chris
It is pretty simple, leave the images on the server and send the PHP + CSS to them...
很简单,将图像留在服务器上,然后将 PHP + CSS 发送给它们...
$to = '[email protected]';
$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: [email protected]\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);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well formed HTML that it will need to interpret:
正是这一行告诉邮件发送者和收件人,电子邮件包含(希望)它需要解释的格式良好的 HTML:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the info.. (link...)
这是我得到信息的链接..(链接...)
You will need security though...
不过你需要安全...
回答by Subhajit
You need to code your html using absolute path for images. By Absolute path means you have to upload the images in a server and in the srcattribute of images you have to give the direct path like this <img src="http://yourdomain.com/images/example.jpg">.
您需要使用图像的绝对路径对 html 进行编码。绝对路径意味着您必须在服务器中上传图像,并且在src图像的属性中您必须提供这样的直接路径<img src="http://yourdomain.com/images/example.jpg">。
Below is the PHP code for your refference :- Its taken from http://www.php.net/manual/en/function.mail.php
以下是供您参考的 PHP 代码:- 取自http://www.php.net/manual/en/function.mail.php
<?php
// multiple recipients
$to = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';
// 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 <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
回答by Jalpesh Patel
I have a this code and it will run perfectly for my site
我有一个这个代码,它可以完美地运行在我的网站上
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:[email protected]" target="_blank">[email protected]</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);
}
mail function
邮件功能
function sendmail($body,$to,$subject)
{
//require_once 'init.php';
$from='[email protected]';
$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 function is use for if you want to change the image you have change in only one function i have many mail function like forgot password create user there for i am use image url function you can directly set path.
图像 url 函数用于如果你想更改你只更改一个函数的图像,我有很多邮件功能,比如忘记密码在那里创建用户,因为我正在使用图像 url 函数,你可以直接设置路径。
function imageUrl()
{
return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
}
回答by Joseph
Sending an html email is not much different from sending normal emails using php. What is necessary to add is the content type along the header parameter of the php mail() function. Here is an example.
发送 html 电子邮件与使用 php 发送普通电子邮件没有太大区别。需要添加的是 php mail() 函数的 header 参数中的内容类型。这是一个例子。
<?php
$to = "[email protected]";
$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);
?>
You can also check herefor more detailed explanations by w3schools
您也可以在此处查看 w3schools 的更详细解释
回答by JoyGuru
You can easily send the email with HTML content via PHP. Use the following script.
您可以通过 PHP 轻松发送带有 HTML 内容的电子邮件。使用以下脚本。
<?php
$to = '[email protected]';
$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<[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Source code and live demo can be found from here - Send Beautiful HTML Email using PHP
可以从这里找到源代码和现场演示 -使用 PHP 发送漂亮的 HTML 电子邮件
回答by Martijn
Simplest way is probably to just use Zend Framework or any of the other frameworks like CakePHP or Symphony.
最简单的方法可能是只使用 Zend Framework 或任何其他框架,如 CakePHP 或 Symphony。
You can do it with the standard mailfunction too, but you'll need a bit more knowledge on how to attach pictures.
您也可以使用标准mail功能完成此操作,但您需要更多有关如何附加图片的知识。
Alternatively, just host the images on a server instead of attaching them. Sending HTML mail is documented in the mailfunction documentation.
或者,只需将图像托管在服务器上,而不是附加它们。发送 HTML 邮件记录在mail函数文档中。
回答by Rohitashv Singhal
Use PHPMailer,
使用 PHPMailer,
To send HTML mail you have to set $mail->isHTML() only, and you can set your body with HTML tags
要发送 HTML 邮件,您必须仅设置 $mail->isHTML(),并且您可以使用 HTML 标签设置您的正文
Here is a well written tutorial :
这是一个写得很好的教程:
rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/
rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/
回答by Ralf Lang
The trick is to know the content id of the Image mime part when building the html body part. It boils down to making the img tag
诀窍是在构建 html body 部分时知道 Image mime 部分的内容 id。归结为制作 img 标签
https://github.com/horde/horde/blob/master/kronolith/lib/Kronolith.php
https://github.com/horde/horde/blob/master/kronolith/lib/Kronolith.php
Look at the function buildMimeMessage for a working example.
查看函数 buildMimeMessage 以获得一个工作示例。

