Mail body doesn't show when a file attached

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
onaydin
Forum Newbie
Posts: 1
Joined: Fri Nov 14, 2008 9:03 am

Mail body doesn't show when a file attached

Post by onaydin »

Hi I am trying to send an email with swift mailer. It works well but when I add an attachment
the mail succesfully reach to the recipients and I can see the attachment but the mail body doesn't
displayed. Mail source is like this

Code: Select all

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="_=_swift-28890491d90ef4655a1.67157877_=_"
Content-Transfer-Encoding: 7bit
Message-ID: <20081114145335.2052.33931755.swift@localhost>
 
<p>Ingilizce bey,</p>
 
<p>mail details</p>
 
--_=_swift-28890491d90ef4655a1.67157877_=_
Content-Type: application/octet-stream; name=ingiltere-ogrenci-rehberi.doc
Content-Transfer-Encoding: base64
Content-Description: ingiltere-ogrenci-rehberi.doc
Content-Disposition: attachment; filename=ingiltere-ogrenci-rehberi.doc
 
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAEAAAA5AEAAAAA
AAAAEAAA5gEAAAEAAAD+////AAAAAOABAADhAQAA4gEAAOMBAAD/////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
 
PHP code is like that

Code: Select all

 
<?php
        require_once "lib/Swift.php";
        require_once "lib/Swift/Connection/SMTP.php";
        $smtp =& new Swift_Connection_SMTP("mail.example.com", 25);
        $smtp->setUsername("site@example.com");
        $smtp->setpassword("utf");
        $smtp =& new Swift($smtp);
        $message =& new Swift_Message($subject, $message, "text/html");
        $message->setCharset("utf-8");
        if(is_array($header["attach"])) {
            foreach($header["attach"]  as $key => $value) {
                $message->attach(new Swift_Message_Attachment(
                     new Swift_File("dokuman/".$value["ad"]), $value["ad"]),'application/msword');
                    
            }
        }
        if ($smtp->send($message, $email, "site@example.com")){
            //echo "Message sent";
        }
 
?>
 
Another problem is, as you see I am trying to attach more than one document, I think I am doing sth wrong because
in that way only the last element of array sending with mail.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Mail body doesn't show when a file attached

Post by Chris Corbyn »

You have to set the body as a mime part (like you're sending a multipart message) if you want to send attachments ;)
Post Reply