PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 21, 2014 12:57 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Question on using Swift
PostPosted: Tue Mar 08, 2011 1:10 pm 
Offline
Forum Newbie

Joined: Tue Mar 08, 2011 1:01 pm
Posts: 3
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);
                        }
 


Top
 Profile  
 
PostPosted: Tue Mar 08, 2011 1:38 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
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)


Top
 Profile  
 
PostPosted: Tue Mar 08, 2011 2:38 pm 
Offline
Forum Newbie

Joined: Tue Mar 08, 2011 1:01 pm
Posts: 3
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?


Top
 Profile  
 
PostPosted: Tue Mar 08, 2011 2:41 pm 
Offline
Forum Newbie

Joined: Tue Mar 08, 2011 1:01 pm
Posts: 3
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.


Top
 Profile  
 
PostPosted: Tue Mar 08, 2011 8:08 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
Cool, let us know how that improves your performance.


Top
 Profile  
 
PostPosted: Thu Apr 21, 2011 1:52 am 
Offline
Forum Newbie

Joined: Thu Apr 21, 2011 1:36 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group