Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "batch email"

Sending batch emails

Sending a large volume of emails using symfony's built-in email support can be problematic because of memory overhead involved in calling the sendEmail action for every email to be sent. In order to overcome this, we will have to use another way. In essence, instead of making multiple symfony mail calls, we will grab the presentation for the email action, then use sfMail directly to send our emails.

In an action

// Get the presentation only for the actual email.
// email/newsletter is the module/action pair that will
// generate the template for the actual email in this example.
$raw_email = $this->getPresentationFor('email', 'newsletter');
 
require_once sfConfig::get('sf_symfony_lib_dir') . '/addon/sfMail/sfMail.class.php';
$mail = new sfMail();
$mail->setMailer('sendmail');
$mail->setCharset('utf-8');
$mail->setContentType('text/html');
 
$mail->setFrom($sender_email, $sender_name);
$mail->setSubject($newsletter_title);
$mail->setBody($raw_email);
 
// To further ensure that we don't choke up on the
// emails, we introduce a throttle. In this example,
// we use 100, which we will use to sleep for a period
// of time for every 100 emails sent.
 
$throttle = 100;
$i = 0;
 
// Now, run a loop through all the email addresses in the mailing list.
 
foreach($email_addrs as $email_addr)
{
    if ($i % $throttle == 0)
    {
        sleep(10); // Here, we sleep for 10 seconds for every $throttle emails sent.
    }
 
    $mail->addAddress($email_addr);
    $mail->prepare();
    $mail->send();
    $mail->clearAllRecipients();
    $i++;
}

That's it! Now, just go ahead and set up the email/newsletter pair which will set up the template for the actual email.

Update: In order to beat the maximum execution time limit in PHP, I also use

set_time_limit(0);

to remove the limit at the start of my actual script. In addition, it may also be a good idea to run the action as a batch script in the background so that the mass mailing can occur in the background.

by Ron Lim on 2006-06-26, tagged batch  email 
(3 comments)

Sending email from a batch script

It can be tricky sending email from a batch script (e.g. for cron use), here's how to do it. Start with your usual batch setup:

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'app_name');
define('SF_ENVIRONMENT', 'environment');
define('SF_DEBUG',       true);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
sfContext::getInstance();

You can set attributes directly from the batch script if you wish:

sfContext::getInstance()->getRequest()->setAttribute('key', $value);

Then forward on to another module/action to handle processing and forwarding to mail as usual:

sfContext::getInstance()->getController()->forward('action', 'module');
by James McGlinn on 2006-06-22, tagged batch  cli  email