Page 2 of 2

Re: Body or Attachment but Not Both

Posted: Tue Oct 07, 2008 6:20 pm
by jmasson
Actually I was wrong, I went back through my old emails and this has never worked... it's always been a case of a body, or attachment, but not both...

Anyway thing I've almost found the solution, it seems when you do an attachment, it is basically then ignoring the previous specification of the email contents as you're now producing a multipart email? So re specifying this as per below includes the content as well as the attachment... Next problem I have though is that no matter what I do, it's coming through as plain text showing all html tags, rather than rendering as html... So if anyone has any tips on this glad to hear them while I'm reading up on it :)

Code: Select all

 
$mailBody = $this->getPartial('emailPartial', array('order_page1' => $order_page1, 'order_page2' => $order_page2, 'order_page3' => $order_page3), 'text/html');
 
//send the above $mailbody in an email using the SwiftMail plugin
try
{
  // Create the mailer and message objects
  $mailer = new Swift(new Swift_Connection_SMTP('smtp.XXX', '', ''));
  //build the message
  $message = new Swift_Message('New Order', $mailBody, 'text/html');
  //cehck for attachment and attach to message
  if (trim($filename) != ""){
    $file_location = sfConfig::get('sf_upload_dir').'/'.$filename;
    $mime_type = 'excel/ms-excel';
    $message->attach(new Swift_Message_Part($mailBody));
    $message->attach(new Swift_Message_Attachment(new Swift_File($file_location), $filename, $mime_type));          
 
    //Change file permissions so that we can delete it
    if(chmod($file_location,0777)) {
      $this->logMessage("Sucessful chmod on ".$filename, 'debug');
    }
    else{
      $this->logMessage("Could not chmod ".$filename, 'err');
    }              
}
etc..
 

Re: Body or Attachment but Not Both

Posted: Tue Oct 07, 2008 9:52 pm
by jmasson
Ok just worked this out, I'll leave my (now 3!) posts in case it helps someone else, but in the above, line 15 just needs to be changed to

Code: Select all

$message->attach(new Swift_Message_Part($mailBody, 'text/html'));
and it works... I thought in a variation of my code I had tried specifying the type there, but obviously I didn't....

Hope this helps the original poster...