Page 1 of 1

Question on using Swift

Posted: Tue Mar 08, 2011 12:10 pm
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);
                        }

Re: Question on using Swift

Posted: Tue Mar 08, 2011 12:38 pm
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)

Re: Question on using Swift

Posted: Tue Mar 08, 2011 1:38 pm
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?

Re: Question on using Swift

Posted: Tue Mar 08, 2011 1:41 pm
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.

Re: Question on using Swift

Posted: Tue Mar 08, 2011 7:08 pm
by John Cartwright
Cool, let us know how that improves your performance.

Re: Question on using Swift

Posted: Thu Apr 21, 2011 1:52 am
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