通过PHP发送电子邮件中的HTML
如何使用PHP发送带有图片的HTML格式电子邮件 我想有一些设置和HTML输出是通过电子邮件发送到一个地址的页面。我该怎么办通过PHP发送电子邮件中的HTML,php,html,email,send,Php,Html,Email,Send,如何使用PHP发送带有图片的HTML格式电子邮件 我想有一些设置和HTML输出是通过电子邮件发送到一个地址的页面。我该怎么办 主要问题是附加文件。我该怎么做呢?非常简单,将图像留在服务器上,然后将PHP+CSS发送给它们 $to = 'bob@example.com'; $subject = 'Website Change Reqest'; $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; $headers .=
主要问题是附加文件。我该怎么做呢?非常简单,将图像留在服务器上,然后将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);
这是我得到信息的链接。。()
不过,您需要安全性…最简单的方法可能是只使用Zend Framework或任何其他框架,如CakePHP或Symphony 您也可以使用标准的
mail
功能来完成,但是您需要更多关于如何附加图片的知识
或者,只需将映像托管在服务器上,而不是附加它们。发送HTML邮件在
邮件
功能文档中有说明。您需要使用图像的绝对路径对HTML进行编码。绝对路径意味着您必须将图像上传到服务器中,并且在图像的src
属性中,您必须给出这样的直接路径
下面是供参考的PHP代码:-取自
我有一个基于此代码的应用程序,它将在我的站点上完美运行
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);
}
图像url功能用于如果您想更改图像,您只需更改一个功能我有许多邮件功能,如忘记密码创建用户,因为我使用图像url功能,您可以直接设置路径
function imageUrl()
{
return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
}
使用PHPMailer
要发送HTML邮件,您只需设置$mail->isHTML(),并且可以使用HTML标记设置正文
以下是一个编写良好的教程:
诀窍是在构建html主体部分时,要知道图像mime部分的内容id。 它归结为制作img标签
查看函数buildMimeMessage以获得一个工作示例。您可以通过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;
?>
可以在这里找到源代码和实时演示-发送html电子邮件与使用php发送普通电子邮件没有太大区别。需要添加的是php mail()函数头参数中的内容类型。这里有一个例子
<?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);
?>
您还可以查看w3schools提供的更详细的解释您在发送
HTML
代码时有问题吗?或者一般使用PHP发送电子邮件?css格式化、图片和html文件需要的其他文件(如js)如何?@UmerHayat同意,但我们都必须从某个地方开始,即使只是学习如何使用Google stuff…@Abadis-css可以通过任何方式完成,嵌入到文件中,也可以像往常一样使用html文件从外部完成。图像可以打包在我认为(我从来没有这样做过)或留在服务器上并链接到。如果您不确定HTML图像是如何工作的,请查看它们:请不要使用HTML
、head
或body
元素,否则您将在实际遵循(X)HTML标准的基于web的电子邮件系统上造成绝对混乱。此外,如果发送HTML电子邮件,则永远不要发送超过h2
的标题级别,因为您的电子邮件主题(在适当的系统上)将位于页面上的单个h1
元素中。如果这不合理,请记住谷歌试图将您的页面解释为只有一个主标题(如“我们赢得了战争!”)和许多小标题(如“被骚扰的麋鹿救了小猫”)。这一部分中有一个拼写错误“$message.=”。这个变量以前没有声明过。谢谢,这就是我想要的needed@Jalpesh那么对于CSS文件,你也会这么做吗?引用绝对路径应该会起作用,对吗?@FernandoSilva您必须设置绝对路径,还必须将类或id设置为元素,以便css能够基于此设置work@Jalpesh我想我还得试试别的东西,因为Hotmail删除了所有浮动css规则,Gmail只是清除了我的类。不确定我是否遗漏了什么,但我已经为数据准备好了HTML,css负责布局。我正在使用PHPMailer类发送电子邮件,我找不到任何关于它导致的这种行为的参考,但我不确定tbh。你知道该调查什么吗?顺便说一句,@Gonate根据您想要的质量级别,使用内嵌样式正确显示图像。除了最基本的情况外,公认的答案不会适用于所有情况。如果您只需要基本案例,那么使用mail()
就可以了。如果您需要一个更可靠的解决方案,或者有复杂的情况或需求,那么最好依赖一个框架或库,该框架或库已经经历了几十个RFC文档和多年的错误修复。如果你能找到一个专门的库或框架,让你挑选并选择你想要的功能,那么没有什么理由依靠我们自己来计算它。你的问题回答了我的问题,谢谢你的深入!
function imageUrl()
{
return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
}
<?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
$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);
?>