因此,使用PHPMailer是否可以在正文消息中嵌入一个变量,然后将该电子邮件的内容抓取到正文中?
所以基本上
像这样:$ mail-> Body = writeBody();
在这里:
//Begin of send email
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.server.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "user@server.com";
$mail->Password = "super_secret_password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = $emailAddress;
$mail->addAddress("name@server.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = writeBody();
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
然后将这样的内容调入身体:
function writeBody()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>THANK YOU</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 " />
<meta name="format-detection" content="telephone=no" />
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
<!--<![endif]-->
<style type="text/css">
body {
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
img {
border: 0 !important;
outline: none !important;
}
p {
Margin: 0px !important;
Padding: 0px !important;
}
table {
border-collapse: collapse;
mso-table-lspace: 0px;
mso-table-rspace: 0px;
}
td, a, span {
border-collapse: collapse;
mso-line-height-rule: exactly;
}
.ExternalClass * {
line-height: 100%;
}
span.MsoHyperlink {
mso-style-priority:99;
color:inherit;}
span.MsoHyperlinkFollowed {
mso-style-priority:99;
color:inherit;}
</style>
<style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
@media only screen and (min-width:481px) and (max-width:599px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_space]{
width:10px !important;
}
td[class=em_width55] {
width:80px !important;
text-align:center !important;
}
td[class=em_width75] {
width:100px !important;
}
td[class=em_font]{
font-size:14px !important;
}
td[class=em_font2] {
text-align:center !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
</style>
<style media="only screen and (max-width:480px)" type="text/css">
@media only screen and (max-width:480px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide], span[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_font]{
font-size:14px !important;
line-height:28px !important;
}
td[class=em_font1]{
font-size:14px !important;
line-height:18px !important;
}
td[class=em_font2]{
font-size:14px !important;
line-height:18px !important;
text-align:center !important;
}
td[class=em_space]{
width:10px !important;
}
span[class=em_br]{
display:block !important;
}
td[class=em_width55] {
width:55px !important;
font-size:15px !important;
line-height:19px !important;
text-align:center !important;
}
td[class=em_width75] {
width:75px !important;
font-size:15px !important;
line-height:19px !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
</style>
</head>
<body style="margin:0px; padding:0px;" bgcolor="#ffffff">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
<!-- === PRE HEADER SECTION=== -->
<tr>
<td align="center" valign="top" bgcolor="#30373b">
<table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
<tr>
<td style="line-height:0px; font-size:0px;" width="600" class="em_hide" bgcolor="#30373b"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" height="1" width="600" style="max-height:1px; min-height:1px; display:block; width:600px; min-width:600px;" border="0" alt="" /></td>
</tr>
此处的目标是将电子邮件的各个项目分解为自己的文件,以便更轻松地进行管理和编辑。
这可能吗?如果是的话,我正确地做到了吗?
答案 0 :(得分:2)
是的,这绝对是可能的,但是由于函数writeBody()是在另一个文件中定义的,所以这不起作用。您可以做的是使用电子邮件正文创建一个文件,例如email.html,然后使用php读取这些内容并将其存储在正文中。
就像这样,当然如果email.html在另一个目录中,你必须在文件名之前放置正确的路径:
//Begin of send email
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.server.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "user@server.com";
$mail->Password = "super_secret_password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = $emailAddress;
$mail->addAddress("name@server.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = file_get_contents('email.html');
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
您的email.html
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>THANK YOU</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 " />
<meta name="format-detection" content="telephone=no" />
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
<!--<![endif]-->
<style type="text/css">
body {
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
img {
border: 0 !important;
outline: none !important;
}
p {
Margin: 0px !important;
Padding: 0px !important;
}
table {
border-collapse: collapse;
mso-table-lspace: 0px;
mso-table-rspace: 0px;
}
td, a, span {
border-collapse: collapse;
mso-line-height-rule: exactly;
}
.ExternalClass * {
line-height: 100%;
}
span.MsoHyperlink {
mso-style-priority:99;
color:inherit;}
span.MsoHyperlinkFollowed {
mso-style-priority:99;
color:inherit;}
</style>
<style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
@media only screen and (min-width:481px) and (max-width:599px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_space]{
width:10px !important;
}
td[class=em_width55] {
width:80px !important;
text-align:center !important;
}
td[class=em_width75] {
width:100px !important;
}
td[class=em_font]{
font-size:14px !important;
}
td[class=em_font2] {
text-align:center !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
</style>
<style media="only screen and (max-width:480px)" type="text/css">
@media only screen and (max-width:480px) {
table[class=em_main_table] {
width: 100% !important;
}
table[class=em_wrapper] {
width: 100% !important;
}
td[class=em_hide], br[class=em_hide], span[class=em_hide] {
display: none !important;
}
img[class=em_full_img] {
width: 100% !important;
height: auto !important;
}
td[class=em_align_cent] {
text-align: center !important;
}
td[class=em_pad_top]{
padding-top:20px !important;
}
td[class=em_height]{
height: 20px !important;
}
td[class=em_aside]{
padding-left:10px !important;
padding-right:10px !important;
}
td[class=em_font]{
font-size:14px !important;
line-height:28px !important;
}
td[class=em_font1]{
font-size:14px !important;
line-height:18px !important;
}
td[class=em_font2]{
font-size:14px !important;
line-height:18px !important;
text-align:center !important;
}
td[class=em_space]{
width:10px !important;
}
span[class=em_br]{
display:block !important;
}
td[class=em_width55] {
width:55px !important;
font-size:15px !important;
line-height:19px !important;
text-align:center !important;
}
td[class=em_width75] {
width:75px !important;
font-size:15px !important;
line-height:19px !important;
}
td[class=em_align_cent1] {
text-align: center !important;
padding-bottom: 10px !important;
}
}
</style>
</head>
<body style="margin:0px; padding:0px;" bgcolor="#ffffff">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
<!-- === PRE HEADER SECTION=== -->
<tr>
<td align="center" valign="top" bgcolor="#30373b">
<table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
<tr>
<td style="line-height:0px; font-size:0px;" width="600" class="em_hide" bgcolor="#30373b"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" height="1" width="600" style="max-height:1px; min-height:1px; display:block; width:600px; min-width:600px;" border="0" alt="" /></td>
</tr>
另外请记得使用正确的结束标记关闭您的email.html,因为这可能会导致一些问题。
html文件也可以是PHP文件,它的工作方式稍有不同,然后你必须包含email.php文件,然后包含一个函数,你传递你想要使用的参数:
include 'email.php';
//Begin of send email
$mail = new PHPMailer;
// same code as before, but use:
// $mail->Body = writeBody();
然后将html包装在这样的函数中:
function writeBody($param1, $param2) {
return "email template here";
}