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

Question on using Swift

Postby AlBunch » Tue Mar 08, 2011 1:10 pm

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

Re: Question on using Swift

Postby John Cartwright » Tue Mar 08, 2011 1:38 pm

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)
Code: Select all
if ($toBe || $notToBe) echo 'That is the question'; 

NEW HERE?: Please read the Forum Rules, and take the Forum Tour before posting!
User avatar
John Cartwright
Site Admin
 
Posts: 11470
Joined: Tue Dec 23, 2003 3:10 am
Location: Toronto

Re: Question on using Swift

Postby AlBunch » Tue Mar 08, 2011 2:38 pm

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 1:01 pm

Re: Question on using Swift

Postby AlBunch » Tue Mar 08, 2011 2:41 pm

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

Re: Question on using Swift

Postby John Cartwright » Tue Mar 08, 2011 8:08 pm

Cool, let us know how that improves your performance.
Code: Select all
if ($toBe || $notToBe) echo 'That is the question'; 

NEW HERE?: Please read the Forum Rules, and take the Forum Tour before posting!
User avatar
John Cartwright
Site Admin
 
Posts: 11470
Joined: Tue Dec 23, 2003 3:10 am
Location: Toronto

Re: Question on using Swift

Postby ishanipatel » Thu Apr 21, 2011 1:52 am

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


Return to Swift Mailer

Who is online

Users browsing this forum: No registered users and 1 guest