【问题标题】:Adding html template to swift email body将 html 模板添加到 swift 电子邮件正文
【发布时间】:2016-07-13 02:43:31
【问题描述】:

我是 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 变量添加到电子邮件模板?任何建议将不胜感激。

【问题讨论】:

    标签: php html swiftmailer email


    【解决方案1】:

    使用这个

    $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);
    

    【讨论】:

    • 我试过了,但没有添加内容类型。感谢您的帮助
    猜你喜欢
    • 2015-05-29
    • 2014-04-24
    • 1970-01-01
    • 2015-04-06
    • 2013-12-17
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多