PHP Developers Network
http://forums.devnetwork.net/

Question on using Swift
http://forums.devnetwork.net/viewtopic.php?f=52&t=128285
Page 1 of 1

Author:  AlBunch [ Tue Mar 08, 2011 1:10 pm ]
Post subject:  Question on using Swift

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).

Syntax: [ Download ] [ Hide ]
                        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);
                        }
 

Author:  John Cartwright [ Tue Mar 08, 2011 1:38 pm ]
Post subject:  Re: Question on using Swift

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:

Syntax: [ Download ] [ Hide ]
$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)

Author:  AlBunch [ Tue Mar 08, 2011 2:38 pm ]
Post subject:  Re: Question on using Swift

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?

Author:  AlBunch [ Tue Mar 08, 2011 2:41 pm ]
Post subject:  Re: Question on using Swift

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.

Author:  John Cartwright [ Tue Mar 08, 2011 8:08 pm ]
Post subject:  Re: Question on using Swift

Cool, let us know how that improves your performance.

Author:  ishanipatel [ Thu Apr 21, 2011 1:52 am ]
Post subject:  Re: Question on using Swift

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
Syntax: [ Download ] [ Hide ]
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

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/