将html模板添加到swift电子邮件正文中

时间:2016-07-13 02:43:32

标签: php html email swiftmailer email-integration

我是swiftmailer的新手,我可以使用它成功发送电子邮件。我需要在电子邮件正文中添加一个html模板。我不知道怎么做? 这是我以前的代码

  function sendMail($destinationEmail){

    // Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();

$html = "<html>
<head>
<title>Alerting System</title>
<style type=\"text/css\">
<!--
table {
font-family: Verdana, Arial, Helvetica, sans-serif;
border: thin solid;
}
th {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #f0f0f0;
background-color: #ff0000;
font-size: 12px;
}
td {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000000;
font-size: 10px;
border: thin solid;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
-->
</style>
<head>
<body>
<CENTER>
<TABLE width=\"50%\">
<TBODY>
<tr>
<th >test</th>
</tr>
</TBODY>
</TABLE>
<br>
Alerting and Reporting System
</CENTER></BODY></HTML>
";


// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
  $destinationEmail => "test",
  $destinationEmail => "test mail"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody("You're our best client ever.");
$message->setFrom("test@testcom.com", "Alerts");

$message->attach($html, "text/html");


// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
}

如何将此$ html变量添加为电子邮件模板?任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用此

$html = "<html>
<head>
<title>Alerting System</title>
<style type=\"text/css\">
<!--
table {
font-family: Verdana, Arial, Helvetica, sans-serif;
border: thin solid;
}
th {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #f0f0f0;
background-color: #ff0000;
font-size: 12px;
}
td {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000000;
font-size: 10px;
border: thin solid;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
}
-->
</style>
<head>
<body>
<CENTER>
<TABLE width=\"50%\">
<TBODY>
<tr>
<th >test</th>
</tr>
</TBODY>
</TABLE>
<br>
Alerting and Reporting System
</CENTER></BODY></HTML>
";


    $message->setBody($html,'text/html' // Mark the content-type as HTML);