Attaching File in SwiftMailer

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
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Attaching File in SwiftMailer

Post by dude81 »

Hello Chris,

This is the code for my attachfile Message.

Code: Select all

 
 public function attachFile($path_to_file,$file_type, $message,$subject = ''){
 
                if( get_class($message) == 'Swift_Message'){
 
                        $messageObj = $message;
                        $message->attach(new Swift_Message_Attachment( new Swift_File($path_to_file),$path_to_file,$file_type));
                }else{
 
                        if(!empty($subject)){
                                $messageObj = new Swift_Message($subject);
                        }else{
                                $messageObj = new Swift_Message($this->subject);
                        }
                        $message->attach(new Swift_Message_Attachment( new Swift_File($path_to_file),$path_to_file,$file_type));
                }
                return $message;
        }
 
 
This how I send the message for attached file

Code: Select all

 
  $message2 = "<html>" ."<head><title>OptionIT<title></head>"."<body>" .
                                        "<div style='float:justify;font-size:.8em;font-face:Arial,Verdana;'><span>Hi Sai,</span></div>" .
                                        "<div><p align='center'><div style='float:justify;font-size:.8em;font-face:Arial,Verdana;'><span>This is a test mail for single plain html mail</span></div></p>" .
                                        "<p><div style='float:justify;font-size:.8em;font-face:Arial,Verdana;'>Thank You</div><p></div></body><html>";
 
                $to_email2 = "xxxxxxxxxx@azzx.com";
                $from_email2 = "yyyyyyyyy@yyy.com";
 
                $msgObj2 = $this->message('Single Email with Attachment',$message2);
 
                $attached_msg = $this->attachFile(CONTROLLER_DIR."/files/file.jpg", "image/jpeg",$msgObj2);
                if($this->sendMail($attached_msg,$to_email2,$from_email2)){
                        echo "Message to Single Email Sent";
                }else{
                        echo "Message to Single Email Failed";
                }
 
 
 
 
On doing this it throws following error

Code: Select all

 
 [mimeWarning:protected] => This is a message in multipart MIME format.  Your mail client should not be displaying this. Consider upgrading your mail client to view this message correctly
 
Could you tell me where I'm doing wrong...?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Attaching File in SwiftMailer

Post by dude81 »

okay it was working, I was sending of series of emails using just send method of Swift. The connection was getting lost
Post Reply