使用HTML/CSS发送PHP电子邮件
我正在尝试用PHP发送电子邮件。我想添加一些CSS样式(无论是内联、内部还是外部文件) 我已经尝试过了,但它无法识别某些元素(如使用HTML/CSS发送PHP电子邮件,php,css,email,Php,Css,Email,我正在尝试用PHP发送电子邮件。我想添加一些CSS样式(无论是内联、内部还是外部文件) 我已经尝试过了,但它无法识别某些元素(如正文)、媒体查询(@media)和声明(最大宽度,内联块),等等。我现在正在尝试使用,但是在线文档没有提到任何关于样式的内容 为了清楚起见,下面是我想使用的代码片段: 用HTML/CSS发送PHP电子邮件有什么想法吗?这也取决于收件人的电子邮件客户端。并非所有客户端都接受所有css。使用某种石蕊测试来检查不同的电子邮件客户端可以使用哪些css。表通常与电子邮件客户端一起
正文
)、媒体查询(@media
)和声明(最大宽度
,内联块
),等等。我现在正在尝试使用,但是在线文档没有提到任何关于样式的内容
为了清楚起见,下面是我想使用的代码片段:
用HTML/CSS发送PHP电子邮件有什么想法吗?这也取决于收件人的电子邮件客户端。并非所有客户端都接受所有css。使用某种石蕊测试来检查不同的电子邮件客户端可以使用哪些css。表通常与电子邮件客户端一起工作,我相信其中很多都不接受div 将“普通”模板(类似于普通HTML页面)转换为电子邮件友好模板。这是必需的,因为邮件客户端无法识别单独的元素。
我目前使用的模板是基于Zurb提供的模板,我使用PHPMailer用PHP发送电子邮件,它从不忘记识别这些元素(对我来说) 要使用它,我创建了一个功能:
function sendMail($name, $from, $message, $to, $subject)
{
$mail = new PHPMailer();
$body = "<html><head></head><body style=\"background-color: #F2F1F0;\"><p>".$message."</p></body></html>";
$mail->SetFrom($from, $name);
$mail->AddReplyTo($from, $name);
// Only if you want to send an attachment, send a variable $object to the function
//$mail->AddAttachment($object);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($to, $name);
if(!$mail->Send()) {
$result = $mail->ErrorInfo;
}
else
{
$result = "Mail sent successfully";
}
return $result;
}
函数sendMail($name、$from、$message、$to、$subject)
{
$mail=new PHPMailer();
$body=“”$message。“”;
$mail->SetFrom($from,$name);
$mail->AddReplyTo($from,$name);
//仅当要发送附件时,才向函数发送变量$object
//$mail->AddAttachment($object);
$mail->Subject=$Subject;
$mail->AltBody=“若要查看邮件,请使用与HTML兼容的电子邮件查看器!”;//可选,注释并测试
$mail->MsgHTML($body);
$mail->AddAddress($to,$name);
如果(!$mail->Send()){
$result=$mail->ErrorInfo;
}
其他的
{
$result=“邮件已成功发送”;
}
返回$result;
}
以及一个使用它的示例:
// Preparation of the message
$message = '<p class="img"><img src="http://mywebsite.com/img/myImage.png" width="220px"></p>';
$message .= '<p class="txt">Hello Mr Dupond !</p>';
$message .= '<p class="txt">We are glad to have you among us</p>';
$message .= '<p class="txt">Se you soon on our Website !</p>';
$message = utf8_decode($message);
// variables needed
$name = "Mr Oktopuss";
$from = "contact@oktopuss.eu";
$to = "contactAddress@domain.ext";
$subject = "The subject of this mail !";
// Send the mail and receive the result
$result = sendMail($name, $from, $message, $to, $subject);
echo $result;
//消息的准备
$message=';
$message.='你好,杜邦先生
",;
$message.='我们很高兴您能加入我们的行列
;
$message.='很快就会在我们的网站上看到您
",;
$message=utf8\u解码($message);
//所需变量
$name=“Mr Oktopus”;
$from=”contact@oktopuss.eu";
$to=”contactAddress@domain.ext";
$subject=“这封邮件的主题!”;
//发送邮件并接收结果
$result=sendMail($name、$from、$message、$to、$subject);
回声$结果;
如果您想在HTML电子邮件中添加CSS,这可能会对您有所帮助 然后您必须使用内联CSS 因为基于浏览器的电子邮件应用程序,例如Gmail,默认情况下会去掉
和
标记,所以总是使用内嵌CSS而不是嵌入CSS
像这样:
$confirmMailContent = "Thank you for registering. Please click <a href='#link'
style='background-color: #D26510; color: #fff;'>here</a> to confirm your email
address";
$confirmMailContent=“感谢您的注册。请单击确认您的电子邮件
地址”;
您可以从中快速添加内联css
我还建议您使用phpMailer
您只需要3个文件来发送电子邮件(“PHPMailer.php”“SMTP.php”“Exception.php”)
您必须添加内联CSS以自定义邮件中的html元素您必须在设计中使用内联CSS并使用邮件()功能。你可以在这里查找电子邮件客户端中允许的CSS:这就是为什么围绕电子邮件活动有一个完整的商业模式的原因,如果你想做到100%正确,有很多知识需要获取。我认为这个问题太广泛了。但由于html邮件可能是一种真正的痛苦,因此值得在此处查看:
<?php
//change those variable only
$name_from = "organization";
$name_to = $username;
$email_sent_to = $email;
$email_sent_from = "yourmail@gmail.com";
$subject = "Your Email";
$smtp_username = "your user name";
$smtp_password = "**pass**";
$message = "
<!DOCTYPE html>
<html lang='en'>
**Your html code**
</html>
";
//Include required phpMailer files
require 'PHPMailer.php'; //add correct derectory
require 'SMTP.php'; //add correct derectory
require 'Exception.php'; //add correct derectory
//Define name spaces
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Create instance of phpMailer
$mail = new PHPMailer();
//Set mailer to use smtp
$mail->isSMTP();
//Define smtp host
$mail->Host = "smtp.gmail.com";
//Enable smtp authentication
$mail->SMTPAuth = "true";
//Set type of encryption (ssl/tls)
$mail->SMTPSecure = "tls";
//Set port to connect smtp
$mail->Port = "587";
//Set gmail userName
$mail->Username = $smtp_username;
//Set gmail Password
$mail->Password = $smtp_password;
//Set content type
$mail->isHTML(true);
//Set email Subject
$mail->Subject = $subject;
//Set sender email
$mail->setFrom($email_sent_from, $name_from);
//Email body
$mail->Body = $message;
//Add recipient
$mail->addAddress($email_sent_to, $name_to);
//Finally send mail
if ($mail->Send()){
header("Location: ../thankyou.php?email=$email");
exit();
}else{
header("Location: ../signup.php?error=xxx");
exit();
}
//Clossing smtp connection
$mail->smtpClose();
?>