Question on using Swift

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
AlBunch
Forum Newbie
Posts: 3
Joined: Tue Mar 08, 2011 12:01 pm

Question on using Swift

Post by AlBunch »

Help me streamline (or troubleshoot) this piece of code. Loading the file for each message when it's not going to change looks inefficient. Is there a way to load the file once and just attach it/re-use it for to/for each individual message?

Right now when I use this to send a 1.5Mb PDF to 200 recipients, it bails about half-way through. I've got php execution timeout and input timeout turned up to ridiculous levels and I believe it's b/c of the file attach that it's having issues as the script works well w/o a file attachment or when the attachment is around 200k (which is the normal size).

Code: Select all

                        foreach($ToList as $User)
                        {
                                if ($_POST['attachment'] != 0)
                                {
                                        $Attachment     = Swift_Attachment::fromPath('attach/'.$Files[$_POST['attachment']]);
                                        $Transport      = Swift_MailTransport::newInstance();
                                        $Mailer         = Swift_Mailer::newInstance($Transport);
                                        $Message        = Swift_Message::newInstance($_POST['subject'])
                                                        ->setFrom(array($From))
                                                        ->setTo(array($User))
                                                        ->setBody($_POST['message'])
                                                        ->attach($Attachment)
                                                        ;
                                } else {
                                        $Transport      = Swift_MailTransport::newInstance();
                                        $Mailer         = Swift_Mailer::newInstance($Transport);
                                        $Message        = Swift_Message::newInstance($_POST['subject'])
                                                        ->setFrom(array($From))
                                                        ->setTo(array($User))
                                                        ->setBody($_POST['message'])
                                                        ;
                                }
                                $Result         = $Mailer->send($Message);
                        }
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question on using Swift

Post by John Cartwright »

Yes, that is not efficient. If you want to use the same message for multiple recipients, then you simply want to add the users to the recipient array, something like:

Code: Select all

$Attachment = Swift_Attachment::fromPath('attach/'.$Files[$_POST['attachment']]);
$Transport  = Swift_MailTransport::newInstance();
$Mailer     = Swift_Mailer::newInstance($Transport);
$Message    = Swift_Message::newInstance($_POST['subject'])
                    ->setBody($_POST['message'])
                    ->setFrom(array($From));

if ($_POST['attachment'] != 0) {
    $Message->attach($Attachment);
}
                    
foreach($ToList as $User)
{   
    $Message->addTo($User);    
}

$Mailer->batchSend($Message);
> (untested)
AlBunch
Forum Newbie
Posts: 3
Joined: Tue Mar 08, 2011 12:01 pm

Re: Question on using Swift

Post by AlBunch »

I'm not hugely familiar with Swift and I'm using it primarily to handle sending attachments. My chief concern is that each person receives a single email addressed directly to them - no additional addresses in the To or CC fields. Will batchSend accomplish that?
AlBunch
Forum Newbie
Posts: 3
Joined: Tue Mar 08, 2011 12:01 pm

Re: Question on using Swift

Post by AlBunch »

Skip that last question. I must have read the documentation a dozen times and missed this: "The batchSend() method of the Swift_Mailer class sends a separate message to each recipient in the To: field. Each recipient receives a message containing only their own address in the To: field. "

Sounds like batchSend is exactly what I need. Thanks for the point in the right direction.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question on using Swift

Post by John Cartwright »

Cool, let us know how that improves your performance.
ishanipatel
Forum Newbie
Posts: 1
Joined: Thu Apr 21, 2011 1:36 am

Re: Question on using Swift

Post by ishanipatel »

I have used swift mailer for sending mass emails with pdf attachments.but i m not able to send mass email
i have use send() method something like this

Code: Select all

for($i=0;$i<$count;$i++)
{
 $message->addTo($aResultFetchUser[$i]["subs_email"]);
}
i have sets pdf and subject out side the loop but only one email is sent not more than one is sent

please give me solutions...

thanks
Post Reply