Actually I'm using PHPmailer for sending e-mails to my client's. I'm looking for solution, how to speedup mail sending by script. Actually my speed is 80 msg/ per minute. Is there any way to speedup sending?
PHPMailer will be slow, there isn't really a way around it. If you are sending the emails from your local mail server you can instead submit it to that locally and make the process almost instant. You can also read more at this (similar) question:
Speed up PHPmailer with office 365 smtp
Related
When an order is placed, the order details are sent via email to the customer and seller but it takes too long to send the email due to which the order placement takes time. What should be done to make the process faster?
I guess you should implement a Queue, for example [https://laravel.com/docs/5.2/queues]
Install a local mail server (I'd recommend postfix) and send to it from PHPMailer using SMTP. That is the fastest way of getting a message on its way. It means that your script does not have to deal with DNS lookups, remote connections, authentication, and it hands off control to the mail server that deals with all those things asynchronously, including queuing, retrying, bouncing etc.
You can implement queuing in PHP, but it's really the wrong tool for the job. Regardless of the implementation, it will be much, much slower and more error-prone than using a mail server, and more to the point, it's software that you will need to write, test and maintain.
I have a system that sends lots of emails reports ( about 500+ emails per day). I am not a spammer :) It's not really big amount of e-mails, but they are send in a loop and I often get this error "PHP Warning: mail(): Could not execute mail delivery program". I know there is PEAR::Mail_Queue package for this issue. But can you please let me know if that package is really useful thing, or do I need to look for something else. Thanks a lot
You can use PEAR's Mail_Queue package to send mails through an SMTP server directly - also it will work in the background and so won't delay or increase your script execution time.
Another advantage of using the Mail_Queue package is that you can retrieve the esmtp id of each mail sent for logging purposes - I don't know if this is possible with the Zeta Mail component or any other one.
I've developed a number of public-facing, and also intranet only, solutions that use this component and haven't had any major problems with it.
"PHP Warning: mail(): Could not execute mail delivery program"
...means exactly what it says on the tin. PHP will just hand the email off to an external program on a Linux/Unix/POSIX system (as defined by sendmail_path in php.ini). And in some cases that is returning an error. It's not a fault within the PHP code.
Unless you've got a really badly configured MTA, then the problem will not be resolved by using an SMTP connection instead.
You need to look at the logs from your MTA to see why its failing to send the mail - or wrap the mail executable in a logging script.
You gotta use sleep() between mails, that solves this.
An alternative could also be to send mails through an SMTP server directly. This saves you the round trip of PHP calling sendmail (or whatever MTA is used) "through the shell" in order to deliver the mail. For example the Zeta Mail component allows you to send mails directly through an SMTP server without needing a special extension for this.
I'm thinking about how to handle sending large amounts of email from my web applications, and whether there are any best practices for doing so. StackOverflow is already labeling this as 'subjective', which it may be to an extent, but I need to know the most successful way to implement this system, or whether any standardized approach exists.
In my webapp, there are users who are heads of groups of 1 to 10,000 users. This user must be able to email send a message to all of these users through my system. Therefore, my system is responsible for sending up to 10,000 emails to individual users for each group head.
As far as I can tell, there is no rate limit in GMail for sending messages to individuals (although there is a 500 recipient max).
Right now, my current setup is:
When a message is sent through the system, it enters an email queue.
A cron script grabs messages from the queue every few minutes, and sends out those emails.
All email is taking place through GMail's SMTP server.
The actual application doing the mailing is PHPMailer.
This setup, as the user base grows, will probably not suffice. The questions I have are:
Should I be using a local SMTP server instead?
Should I use a mail binary on the local machine instead? I this case, I could probably skip the queue altogether?
Is there an accepted way to do this?
Thanks!
Google App Engine
I would write this in Google app engine (python) because:
It scales well.
It has a good email api.
It has a taskqueue with a good api to access it.
Because python is a real nice language.
It is (relatively) cheap.
PHP
If I would implement it in PHP I would
Find yourself a good SMTP server which allows you to sent this volume of mails because Gmail will not allow you to sent this kind of volume. I am for sure that this will cost you some money.
Find yourself a decent PHP email library to sent messages with like for example PHPMailer like you said.
Use a message queue like for example beanstalkd to put email messages in queue and sent email asynchronously. First because with this the user will have snappier page load. Second with a message queue like beanstalkd you can regulate speed of sending better which will prevent from overloading your pc with work. You will need to have ssh access to the server to compile(install) beanstalkd. You can find beanstalkd at beanstalkd
You would also need ssh access to run a PHP script in the background which will process the message queue. You can find a beanstalkd-client at php beanstalkd-client
from php/apache/webpage
This is the page from which you will sent messages out to user. From this page you will sent message to beanstalkd by coding something in the lines of this:
// register Pheanstalk class loader
require_once('pheanstalk_init.php');
$pheanstalk = new Pheanstalk('127.0.0.1');
$message = ""; // This would contain your message
$pheanstalk->put(json_encode($message);
You have to put messages in message queue using put command
From long running PHP script in background:
The code would look something like this:
// register Pheanstalk class loader
require_once('pheanstalk_init.php');
$pheanstalk = new Pheanstalk('127.0.0.1');
while(true) {
$job = $pheanstalk->reserve();
$email = json_decode($job->getData());
// Sent email using PHP mailer.
$pheanstalk->delete($job);
}
Like I am saying it is possible with both PHP and Google app engine but I would go for app engine because it is easier to implement.
With an email count as "high" as 10.000 a day, I wouldn't rely on GMail (or any other) SMTP. Not that they can't handle it, obviously they can handle A LOT more. But they possibly don't want to.
Having a local SMTP server is IMO the way to go :
It's pretty easy to set up (just DON'T let people use it without a strong authent scheme)
Most modern MTA handle sending queues very well
You won't have to deal with GMail (or others) deciding to block your account someday for quota reasons
Gmail and Google Apps limits you to around 500 emails a day. I'm not sure how that combines with the 500 recipient max, but if you want to send 10 000 emails you'll probably want to find another mail server. I personally use a local server or the ISP or datacenter's SMTP.
If you are sending that many emails, I would recommend using the queue so the user's isn't sitting there waiting for the email to be sent.
Be very careful that your domain doesn't get blacklisted as a spam domain. If it does, you can expect most of your emails to be blocked, support, sales, etc. Which could in turn be very expensive.
You may instead want to use a service like AWeber. Not only are they setup to handle these amounts of emails, but they can probably give you more metrics than you can implement on your own.
I'm not sure if it's publishe anywhere, but from experience I can tell you that Gmail will put a fifteen minute or so freeze on your account if you start sending hundreds of messages at a time. This happened to me last week. I think you should host your own SMTP server. Using the mail() function often will put your mail in someone's spam folder.
Just install Postfix on the local machine, or a machine on the same LAN for maximum access speeds. Make sure it is well secured from the outside, and quickly accessible from the inside.
Then code your PHP script to directly inject the emails to the Postfix queue. That shall dramatically increase the processing speed of mail delivery.
I am using php sendmail() function in my projects. When I sent above 3 or more mails processing gets too slow. What is the problem? If I use PHPMailer, will this problem solve?
There are a few things that can be causing this to be slow.
You may be waiting for each email to be fully sent before sendmail() returns. This will mean opening a SMTP socket, talking to another mail server, sending the email and closing up the socket again.
We have a system that sends quite a few emails out in batches and we use PHPMailer for that. We open the socket once, send lots of messages then close it up again, and we get good performance on this (it can send several hundred emails in a single run).
If your message is identical to all recpients, I would recommend sending them all in a single email and BCC'ing everyone, as this takes a lot of pressure away from your script.
Another thing to check is local virus scanners. We used to have problems with the outgoing email scanner grinding the whole thing to a halt. Worth looking into.
Seems, your mail() functions has some restrictions applied to sendmail or some problems. Try sending mail via SMTP for example using this :http://swiftmailer.org/ and see if it will solve your problem.
I have php script to send 1,00,000 mails everyday..
it just runs all emails in loop and invoke mail() function..
I can't use mailinglist manager because. each mail has different content, unique clickurls.
is it the proper way to send mailers?
thank you
Sending thousands of emails from a PHP script is a bit messy at best. One of the real problems is to ensure that the script keeps running as long as necessary to do the job. You can do that if you keep resetting the time out for PHP and you have permission to do that, but there's still going to be a host of other problems.
The best thing to do is to build the email message and the list of recipients in PHP then hand the task of mailing off to something else - either a shell script or some kind of application that gets spawned off and detached from PHP in some way.
Also have you tried using SwiftMailer etc?
It should be alright, as long as you provide users a link to unsubscribe from the mailing list. Make sure you're on good terms with your SMTP server provider. Otherwise, the server will get blacklisted.
EDIT: Many SMTP servers have limitations on the number of emails you can send out in an hour. Find out this limit for your SMTP server and put a small delay() in your loop after sending out each email.
php mail(...) function?