4

I can't send multipart/alternative with Swift Mailer (I haven't found any reference, so maybe I can't use this function), this is my code:

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

Usually I receive an uncomprensible mesage or an attached file named 'noname'.
Can someone help me? Thanks

Razorphyn
  • 1,314
  • 13
  • 37
  • 2
    You're grossly abusing swiftmailer. There is absolutely NO need to set your own headers. `->setBody('html content here', 'text/html')` and `->addPart('plaintext alternative here', 'text/plain');`. – Marc B Jun 03 '13 at 19:55
  • I haven't understood the use of the `->addPart`, so thank you. For spam reasons is better to set the default content type to text/plain and after attach the html part? Thanks – Razorphyn Jun 03 '13 at 20:01
  • 1
    most people use the alternate text for non-html mailers, so the html-deprived can still read at least the content of your email. as for spam purposes, I couldn't say for sure. I highly doubt the relative position of alt v.s. main body is relevant, but that all comes down to individual spam filters/scoring systems. – Marc B Jun 03 '13 at 20:04
  • possible duplicate of [Multipart email with swift](http://stackoverflow.com/questions/10589518/multipart-email-with-swift) – flu Jun 19 '14 at 16:58

1 Answers1

8

A solution (from Marc B's comment):

$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');
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Razorphyn
  • 1,314
  • 13
  • 37