-2

I am creating a script for sending bulk email with different-different subject and dynamic message. I am using for loop and php mail function for seding mail. when i am running script it will take long time to send all emails and also consume high CPU usage. Do you any suggestion or any script which run fast and sending 500 emails email at once without high CPU load.

Thanks

A. Jain
  • 167
  • 8
  • 1
    did you tried https://github.com/PHPMailer/PHPMailer ? – icy Oct 04 '17 at 15:04
  • yes, i have used phpmailer, php mail and wp_mail functions, all of the consume high CPU load. – A. Jain Oct 04 '17 at 15:06
  • 1
    At a certain level of scale, you're always going to need to outsource that email sending to a separate service. That could be an API, another PHP script processing jobs from a database or from a queuing system, or any number of other things. Fundamentally, sending one email takes time, and sending 500 takes 500 times longer. – iainn Oct 04 '17 at 15:07

2 Answers2

1

From PHP Docs

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

I suggest to use PHPMailer.

You also should consider to process the script in a separate thread.

icy
  • 1,468
  • 3
  • 16
  • 36
1

What ever the mailer you use you cannot scale your solution if you are not using a queue system. Processing time consuming tasks specially sending emails should be a background task and handled by a queue.

If your app is in a early stage of development its better to use a framework like Laravel which has an in-build queue system. I highly recommend it.

If not better to follow these posts where they show how to incorparate queue system to basic php application.

As @icy2k suggest below, PHPMailer would be a better alternative than inbuilt mail() function. So hat-of to him for point that out. And If you're planning on using Laravel, it has an in-built powerful mailer system.

Gayan
  • 3,614
  • 1
  • 27
  • 34