2010-04-29 92 views
2

我为客户开发了一个比赛页面,他们希望客户收到的电子邮件不仅仅是文本。我使用的教程只在“发送正文消息”内提供了简单的文本。我需要添加html来感谢客户输入,并将图像引入此电子邮件。如何将HTML格式添加到'基于PHP邮件的Swift邮件教程'?

的代码是:

//send the welcome letter 
function send_email($info){ 

    //format each email 
    $body = format_email($info,'html'); 
    $body_plain_txt = format_email($info,'txt'); 

    //setup the mailer 
    $transport = Swift_MailTransport::newInstance(); 
    $mailer = Swift_Mailer::newInstance($transport); 
    $message = Swift_Message::newInstance(); 
    $message ->setSubject('Thanks for entering the competition'); 
    $message ->setFrom(array('[email protected]' => 'FromEmailExample')); 
    $message ->setTo(array($info['email'] => $info['name'])); 

    $message ->setBody('Thanks for entering the competition, we will be in touch if you are a lucky winner.'); 

    $result = $mailer->send($message); 

    return $result; 

} 

这function.php片是否工作正常,客户recieving他们的电子邮件好吧,我只是需要改变

('感谢参加比赛, 我们会联系你的,如果你是一个幸运的 赢家。“)

有HTML,而不是...

如果可以的话,请给我提供一个如何将HTML集成到这个函数中的例子。

回答

0

相同的文字会保留还是应该以某种方式进行设计? 编辑您的电子邮件中的HTML需要内嵌CSS样式,例如:

('<p style="font-size:1.2em; color:#f0f0f0;">Thanks for entering the competition, we will be in touch if you are a lucky winner.</p>') 

,如果你需要一个表只需添加:

('<table style="font-size:1.2em; color:#f0f0f0;"><tr><td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td></tr></table>') 

或使其更简单

$message_body' 
<table style="font-size:1.2em; color:#f0f0f0;"> 
<tr> 
<td>Thanks for entering the competition, we will be in touch if you are a lucky winner.</td> 
</tr> 
</table> 
'; 

$message ->setBody($message_body); 

我知道,当我需要发送HTML电子邮件时,我需要将内容类型设置为HTML,我相信你在以下行上做了

$body = format_email($info,'html'); 

我希望这是你要找的。如果不是让我知道

+0

Krike嗨, 感谢回去我,我有尝试了这种在表格代码中输入函数的方法,基本上我收到的电子邮件然后向我显示标签,而不是使用标签来设置文本的样式。你知道其他方式吗? – Daniel 2010-04-29 15:52:26

+0

没关系,我搞定了。非常感谢你的帮助克里克。 :-) – Daniel 2010-04-29 16:36:54

+0

很高兴你可以使它的工作,我认为内容类型已全部设置为HTML,但我想它没有看到galens回复。 – Christophe 2010-04-29 16:42:51

1
$message = Swift_Message::newInstance(); 
$message->setContentType('text=html'); 
+0

嗨盖伦,感谢您的快速回复,我不确定我应该在哪里插入?我应该删除任何行并将其替换为您的建议吗? – Daniel 2010-04-29 15:54:35

+0

编辑我的答案,你应该看看现在在哪里添加它 – Galen 2010-04-29 16:14:54

+0

伟大的盖伦,非常感谢。 :-) – Daniel 2010-04-29 16:36:35

4

你也可以添加'text/html的'到setBody(ref):

->setBody($this->renderView('YouBundleName:Default:email.html.twig'), 'text/html');