php - 使用 Swift Mailer 发送多部分/替代邮件

标签 php email swiftmailer multipart-alternative

我不能用 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”的附件。
有人可以帮帮我吗?谢谢

最佳答案

解决方案(来自 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');

关于php - 使用 Swift Mailer 发送多部分/替代邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16904617/

相关文章:

php - php 脚本中 ssl 的潜在问题

php - Symfony6如何正确配置sendmail从本地主机发送电子邮件

php - 加载自定义 Yii 组件

php - 如何保存 Swiftmailer 发送的所有电子邮件

php - 如何使用 swiftmailer 发送多封不相关的电子邮件?

php - 如何避免在 php 刷新时重新发送数据

php - sql - 使用多个 WHERE 条件进行计数

php - 调用脚本时数据库中的增量值

html - 电子邮件模板,适用于除 Outlook 以外的所有客户端

email - LDAP 获取用户邮箱