【问题标题】:Send multipart/alternative mail with Swift Mailer使用 Swift Mailer 发送多部分/替代邮件
【发布时间】:2013-05-30 01:39:15
【问题描述】:

我无法使用 Swift Mailer 发送 multipart/alternative(我没有找到任何参考资料,所以我可能无法使用此功能),这是我的代码:

$file[1]=html_entity_decode($file[1]);
//Prepare plain/html body
foreach($rep as $find => $sost)
    $file[1]=str_replace($find,$sost,$file[1]);
//Prepare plain/text body
$plain=strip_tags(str_replace('&nbsp;',' ',str_replace('<br/>',"\n",$file[1])));
$boundary=uniqid('n_=_p');
//Prepare mail body                 
$body = "--".$boundary."\r\n";
$body .= "Content-type: text/plain;\r\ncharset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n";
$body .= $plain;
$body .= "\r\n--".$boundary."\r\n";
$body .= "Content-type: text/html;\r\ncharset=utf-8\r\nContent-Transfer-Encoding: 7bit\r\n";
$body .= "<html><body>".$file[1]."</body></html>";
$body .= "r\n--".$boundary ."--";
//Send Mail
$message = Swift_Message::newInstance();
$message->setFrom(array($stmp[2]=>$stmp[1]));
$message->setReplyTo(array($stmp[2]=>$stmp[1]));
$message->setSubject($file[0]);
$message->setContentType("multipart/alternative");  
$message->setBody($body);
$message->setTo($mail);
$message->setBoundary($boundary);

if($stmp[0]==0)
    $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -t');
else if($stmp[0]==1){
    if($stmp[5]==0)
        $transport = Swift_SmtpTransport::newInstance($stmp[2],$stmp[4]);
    else
        $transport = Swift_SmtpTransport::newInstance($stmp[2],$stmp[4],'ssl');

    if($stmp[6]==1){
        $transport->setUsername($stmp[7]);
        $transport->setPassword($stmp[8]);
    }
}

$mailer = Swift_Mailer::newInstance($transport);

if(!$mailer->send($message,$failure))
    file_put_contents('send_mail_Send_error',print_r($failure,true));

通常我会收到无法理解的消息或名为“noname”的附件。
有人可以帮助我吗?谢谢

【问题讨论】:

  • 你在严重滥用 swiftmailer。绝对不需要设置自己的标题。 -&gt;setBody('html content here', 'text/html')-&gt;addPart('plaintext alternative here', 'text/plain');.
  • 我还没有理解-&gt;addPart的用法,谢谢。出于垃圾邮件的原因,最好将默认内容类型设置为 text/plain 并在附加 html 部分之后?谢谢
  • 大多数人使用非 html 邮件的替代文本,因此缺乏 html 的人仍然可以至少阅读您的电子邮件内容。至于垃圾邮件的目的,我不能肯定地说。我高度怀疑 alt 与 alt 的相对位置。主体是相关的,但这一切都归结为各个垃圾邮件过滤器/评分系统。
  • 好的,完美,再次感谢您
  • Multipart email with swift 的可能重复项

标签: php email swiftmailer multipart-alternative


【解决方案1】:

一个解决方案(来自 Marc B 的评论):

$message = Swift_Message::newInstance();
$message->setFrom($stmp[2])
  ->setReplyTo($stmp[2])
  ->setSubject($file[0])
  ->setContentType("text/plain; charset=UTF-8")
  ->setBody($plain,'text/plain')                
  ->addPart($file[1],'text/html');

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多