2

I am working on a functionality to send mass mail on product update.I want to improve code for performance and to avoid spam.

Code:

 $collection = Mage::getModel('newsletter/subscriber')->getCollection();
 $arr = array();

   foreach ($collection as $subscriber) 
   {
      $arr[] = $subscriber->getData();
   }

    $html = '<strong>Dear Subscriber,</strong><br>
                    Following product has been updated <br><br> ';

      for($j=0;$j<count($arr);$j++)
      {
         $mail = Mage::getModel('core/email')
             ->setToName($arr[$j]['subscriber_email'])
             ->setToEmail($arr[$j]['subscriber_email'])
             ->setBody($html)
             ->setSubject('Sarees Images Collection')
             ->setFromEmail(Mage::getStoreConfig('trans_email/ident_general/email', $storeId))
             ->setFromName(Mage::app()->getStore()->getName())
             ->setType('html');
             try{

             $mail->send();
             }
             catch(Exception $error)
             {
             Mage::getSingleton('core/session')->addError($error->getMessage());
             return false;
             }
         } 

Problem:

Though mails are being sent to susbscribers it is slowing the page and there is a fear of being spammed.

How can I tackle this?

I am using Magento ver. 1.9.2.4.

1 Answer 1

1

Using the webserver where you host your website for bulk mailing is not a very good idea. Spamfilters will add you to blacklists which could eventually harm your website in unexpected ways.

If you want to send bulk emails from your magento use an Email provider instead. They have dedicated teams to stay out of spamboxes so you don't have to worry about that anymore. There are plenty free solutions like Sendgrid, Mailchimp (trough Mandrill) and Mailgun. Mailgun is developers oriented, so that might be a good choice.

Check a roundup here.

4
  • ok..if I hv around 500 email addresses,how can I improve the code esp. loop part? Jul 27, 2016 at 13:11
  • If it is slowing pageload too much you could consider it moving it to solving it with a cronjob? Write code which does a check who should be email when cronjob is running.
    – Akif
    Jul 27, 2016 at 13:17
  • You mean in batch process? Jul 27, 2016 at 13:36
  • Yes, see example here: inchoo.net/magento/creating-cron-script-in-magento
    – Akif
    Jul 28, 2016 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.