Page 1 of 2

Body or Attachment but Not Both

Posted: Wed May 14, 2008 4:25 am
by dubh
Hello,
I've read the whole docs and searched the forums with no answer to my problem. The emails send successfully with a body and attached image.

Yet, when received the body missing and the attached image is corrupt (and has a very small file size). If there is no attachment when sent, then the body displays. If there is no body when sent, then the attachment is alright (and is it's real file size).

I have viewed the source of the emails to confirm the body is actually missing. Here is my code:

Code: Select all

// ...
 
require_once "../swift_mail/Swift.php";
require_once "../swift_mail/Swift/Connection/NativeMail.php";
 
$swift =& new Swift(new Swift_Connection_NativeMail());
 
$message =& new Swift_Message($subject);
$message->attach(new Swift_Message_Part($body));
$message->attach(new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
 
$success = $swift->send($message, $recipient, $sender);
$swift->disconnect();
 
// ...
I have tried every combination: embedding the image in html, plain text body, html body, no mime for the body, no mime for the image, passing the body to the Swift_Message constructor (second param)...

I have exhausted myself and re-read many relative docs and threads. Your help is really appreciated and thank you for a wonderful script complete with docs.

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 5:14 am
by vincent99
I've the same problem... :(
I tried to change paths, statement order...no way

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 6:33 am
by Chris Corbyn
Does the file exist and can PHP read it?

Code: Select all

var_dump(is_readable($file_path));

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 6:43 am
by vincent99
Yes

Code: Select all

bool(true)

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 7:00 am
by Chris Corbyn
Do you know how to view the email source in your mail client? If you do, could you have a look at it and see if there are blank lines on each consecutive line all the way through it? :)

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 7:17 am
by vincent99
There are some blank lines between some tags, like <tr> or <td>.
I'm using Thunderbird to view it.

After the </body> tag i have:

Code: Select all

 
</html>
--_=_swift-346269536482ad707e432f3.85331637_=_--
 
--_=_swift-401386617482ad707e42026.51788798_=_
Content-Type: application/pdf; name=attach1.pdf
Content-Transfer-Encoding: base64
Content-Description: attach1.pdf
Content-Disposition: attachment; filename=attach1.pdf
 
JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURl
Y29kZT4+CnN0cm
[some characters...]
--_=_swift-401386617482ad707e42026.51788798_=_--
 
The pdf file is well received, not the images embedded.

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 7:38 am
by Chris Corbyn
vincent99 wrote:The pdf file is well received, not the images embedded.
Could you post the code you're using to embed the images please? :)

Re: Body or Attachment but Not Both

Posted: Wed May 14, 2008 7:52 am
by vincent99

Code: Select all

 
require_once "swift/lib/Swift.php";
require_once "swift/lib/Swift/Connection/SMTP.php";
require_once "swift/lib/Swift/Plugin/FileEmbedder.php";
 
$body = file_get_contents("page.html");
 
try {
  $smtp =& new Swift_Connection_SMTP("smtp.mydomain.it", 25);
  $smtp->setUsername("user@mydomain.com");
  $smtp->setpassword("password");
  $swift =& new Swift($smtp);
  
  $plugin =& new Swift_Plugin_FileEmbedder();
  $plugin->setTagDefinition("td", "background", array("gif", "png", "jpg"));
  $plugin->setTagDefinition("img", "src", array("gif", "png", "jpg"));
  $swift->attachPlugin($plugin, "file_embedder");
 
  $message =& new Swift_Message("Email object");
  $message->attach(new Swift_Message_Part("Message body"));
  $message->attach(new Swift_Message_Part($body, 'text/html'));
 
  $attachPath = $_SERVER["DOCUMENT_ROOT"]."/attach1.pdf";
  $message->attach(new Swift_Message_Attachment(file_get_contents($attachPath), "attach1.pdf", "application/pdf"));
 
  $swift->send($message, "recipient@domain.com", "sender@domain.com");
  
  echo "Sent";
} catch (Swift_ConnectionException $e) {
  echo "There was a problem communicating with SMTP: " . $e->getMessage();
} catch (Swift_Message_MimeException $e) {
  echo "There was an unexpected problem building the email:" . $e->getMessage();
}
 
Images paths in the html page are absolute, so /var/www/..../imageName.jpg

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 1:59 am
by dubh
Thanks Chris.
Chris Corbyn wrote:Does the file exist and can PHP read it?

Code: Select all

var_dump(is_readable($file_path));
yes it prints "bool(true)".
Chris Corbyn wrote:Do you know how to view the email source in your mail client? If you do, could you have a look at it and see if there are blank lines on each consecutive line all the way through it? :)
No blank lines, here is the message container in the page source:

Code: Select all

<div id="remote-objects-message">To protect your privacy, remote images are blocked in this message.&nbsp;<a href="#loadimages" onclick="rcmail.command('load-images')">Display images</a></div>
<div  id="messagebody">
<div class="message-part"><div class="pre">
</div></div>
 
</div>
</div>
here is the email source:

Code: Select all

Return-path: <test@test.com>
Envelope-to: xxxxx@xxxxx.com
Delivery-date: Thu, 15 May 2008 07:46:49 +0100
Received: from [xxxxx] (helo=mx3.xxxxx.com)
    by xxxxx.com with esmtp (Exim 4.63)
    (envelope-from <test@test.com>)
    id 1JwXEn-0005iu-Pa
    for xxxxx@xxxxx.com; Thu, 15 May 2008 07:46:49 +0100
Received: from [xxxxx] (helo=moe.xxxxx.ca)
    by mx3.xxxxx.com with esmtp (Exim 4.63)
    (envelope-from <test@test.com>)
    id 1JwXEk-0001xE-AU
    for xxxxx@xxxxx.com; Thu, 15 May 2008 07:46:49 +0100
Received: from [xxxxx] (helo=robin.xxxxx.ca)
    by moe.xxxxx.ca with esmtp (Exim 4.50 (FreeBSD))
    id 1JwUff-000Dhg-47
    for xxxxx@xxxxx.com; Thu, 15 May 2008 04:02:23 +0000
Received: (qmail 1444 invoked by uid 48); 15 May 2008 00:46:45 -0600
To: xxxxx@xxxxx.com
Subject: xxxxx
From: test@test.com
Reply-To: test@test.com
Date: Thu, 15 May 2008 00:46:45 -0600
X-LibVersion: 3.3.2_4
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <20080515064645.28025.591121247.swift@xxxxx.com>
X-Spam-Score: ++ (2.0)
I hope my post is not too overwhelming as I catch up on a missed discussion. Thank you Chris.

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 4:49 am
by Chris Corbyn
~ dubh,

Are you using Gmail? It looks like it's blocking the images.

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 5:10 am
by dubh
Chris Corbyn wrote:...Are you using Gmail? It looks like it's blocking the images.
No it's email with my hosting account and tried with other email servers. It blocks the body too. The image will show without the body and vice versa.

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 5:14 am
by vincent99
Only the attachment is always well attach.
Maybe some bad mail header or wrong character encoding?

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 5:16 am
by dubh
When I embedded the image to html, the body displayed but the image was gibberish (about 300 characters, the same size as the corrupt file when attached).

Re: Body or Attachment but Not Both

Posted: Thu May 15, 2008 5:33 am
by dubh
ps. the mail client is RoundCube and the image is shown with a click of the "Display image" link, where it is corrupt.

Re: Body or Attachment but Not Both

Posted: Tue Oct 07, 2008 12:53 am
by jmasson
Hi All,

dubh did you ever solve this issue?? I had a perfectly working setup, but took my code away to work on it for a few days on another machine (although not the section handling email), brought it back, and now experience this same issue.

Code is below (I'm using Symfony), but basically I build an email with a body from a partial, then if there is a file to attach (depending on what the user has done) I attach it.

Basically , if the $message->attach... line is fired, then the body of my email is wiped out, in the email I receive it's just the attachment (not corrupt, it's there and can be opened), if there is no file to attach (or this line is just commented out) then the body of the email is there.... I assume I've done something to cause this, but really can't work out what it is....

Any pointers appreciated!

Thanks,
John

Code: Select all

 
//get responses from the session
$order_page1 = $this->getUser()->getAttribute('submit1');
$order_page2 = $this->getUser()->getAttribute('submit2');
$order_page3 = $this->getUser()->getAttribute('submit3');    
$filename = $this->getUser()->getAttribute('filename');       
    
//call the partial which forms the body of the email, passing in the above arrays to the partial
$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_Attachment(new Swift_File($file_location), $filename, $mime_type));
 
    //Change file permissions so it can be deleted later
    if(chmod($file_location,0777)) {
      $this->logMessage("Sucessful chmod on ".$filename, 'debug');
      }
      else{
        $this->logMessage("Could not chmod ".$filename, 'err');
      }       
        
  } 
 
  // Send
  //$mailer->send($message, $mailTo, $mailFrom); //put details in app.yml later TODO
    $mailer->send($message, 'my_email', 'my_email');
    $mailer->disconnect();
          
  //If file was attached, now delete it (as email hs been sent)
  if (trim($filename) != ""){
    if(unlink($file_location)){
      $this->logMessage("File ".$filename." was successfully deleted", 'debug');
    }else{
      $this->logMessage("File ".$filename." was not deleted", 'err');
    }
}