SMTP mail really slow - php

So I have a problem with SMTP mail, I have a Zend Framework 2 application, and when the user signs up on the site I send him an confirmation email.
The problem is that when the user click on submit it takes about 3-5 seconds on page load, and that's because of the smtp email that is sending, if I take the part out which sends the email, the answer is instant.
I'm using SMTP from gmail, do you guys have any tips how to solve this?

Actually, PHP documentation doesn't recommend to use the PHP's mail() function to send an email on page load. Instead, it would be much faster to send emails in background. For example, create the outgoing_mail table in your database, and save your messages there when page is loaded. Then, create a cron scheduled task which runs your PHP script that would enumerate all pending messages and send them in turn. This way, you will have quick page load time and mail sent.

Related

how to call php page to work in background?

I am working on an email system , All things go fine , I can send email without any error, I have more than 3000 email address and I want to send email to them , but when I press on send button the operation took more than one hour , so I decided to use pear queue mail package, the package now insert the emails queue inside database, but how can I run send function after inserting it in database in background so user will not get confused from a long time that he had to wait .
using loop is a long process to send email one by one it is better to send bulk email. SwiftMailer is best to do that.
use SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (there are very low chances that your email will go into spam).
also, I would like to tell you when you run a long process in PHP it gets time out after some time. you have to increase the execution time in php.ini file.
if you want to stuck with your process(loop).

PHP timed script execution

I am not quite sure how to put my question into words, but this is what I am trying to build. I want to send email using PHPMailer through my gmail account. Now, Gmail has a limit of 500 emails in one day. Another thing to consider is, when you send a bulk email at once, there is a chance that my email is sent back to me, as my bulk email is considered to be a spam. I have around 200 in my contact list. So a limit of 500 is not a problem. Now my question is, is there any native PHP function that will only execute the send function of PHPMailer in a certain amount of time duration? What I want to do is, I want to send 10 emails from my contact list every 30mins.. So if I have 200 in my contact list, there is an interval of 30 mins in every 10 contacts until I send the email to everyone. I have no problems creating the email script using PHPMailer. I just need to know if there is a way in PHP to 'time' the execution of my functions. I would appreciate any kinds of help. Thank you!
I think you should really look for third party solutions. Sending bulk emails via Gmail or any other provider will create problems for you.
I am using MailGun (with 200 mails you can use FREE version) for years and it does the job.
Don't forget to check mailgun for PHP.

trigger and execute a request in back end php

I'm using Amazon web server.. I have a function for mail like below,
function mail_send($to){
require_once("class.phpmailer.php");
require_once("class.smtp.php");
$mail->AddAddress($to);
$mail->Send();
}
Whenever I want to send mail, I'm just calling this function by,
mail_send("example#xyz.com");
So, if I'm trying to send mail in loop, this will take more time to send mail.. approximately it will take 5 seconds for sending each mail.. But in aws, maximum execution time is 60 seconds in browser, after that it shows empty response... So, I need to trigger the mail and it need to be executed in backend. So, if I'm sending mail to 10 members, It need to only trigger the mail function for 10 times and the page loading should be stopped..
I have tried CURL, But it also waits for response of sending each mail so, it will taking same time to execute
Send from a cron script or other scheduled task that does not have a timeout - search on here for how to do that.
Send more efficiently - see the mailing list example provided with PHPMailer.
Get your local mail server to work for you - submit messages to it (which will be very fast) and let it deal with slow deliveries - it's what mail servers are for.
I can see you've based your code on an obsolete example and are using an old version of PHPMailer, so get the latest version.

How to Ensure Instant Email Sending Through PHP

I have made a Registration Page for the Users to get registered and When User Will be registered a confirmation Email will be sent to the User Email Address. But The PHP mail() function some times sends email instantly and some times after several minutes and The Mail also goes to the SPAM Box. I have seen so many forums where Mail is sent instantly and does not go to SPAM Box and goes directly to Inbox.
How can I solve my above mentioned Problems.
You can't speed up the process of sending mail with just PHP. As for the spam issue, this is one of many issues with the PHP mail() function. You can take a look at the headers of the emails that are being marked as spam and usually it should give some kind of indication as to why it's spam. Also, check the headers in the email you're sending. As I said, the mail() function can be tough to work with. Go with a better solution:
PHP Mailer or Swift Mailer
Regarding your lag: you could save the message in a database and use a cron every minute to send the mails which are in the queue. The user will usually get the message within 1 minute.
Regarding your SPAM problem: use a SaaS like http://www.sendgrid.com
Mailchimp would be better for this : http://mailchimp.com/

Slow mail() function workarounds

I have a php script which sends user a mail if his purchase is successful. The problem is it the page loads slow due to mail(). I know there are ways like putting the mails in a database table and then using a cron job to send them but the frequency of purchase can be high and I want the mail to be sent right away, so that doesn't look like a good option.
The purchase request gets processed by the same page from where he purchased and he can only do so once. The user doesn't control any part of mail other than the purchase details. I thought of using Ajax, the script would send the data to client side and call a ajax function which then calls another mail script but this would let user know what is being sent and can be tampered. Is there any other way I can use Ajax safely without letting user know whats being sent and where? And are there any better workarounds?
mail() should return immediately upon queuing the message to your mail server, which (generally) should take no time at all, so I think your problem here is definitely the mail server and not your PHP. I'm guessing that the mail server you're submitting to is running some anti-spam checks, like reverse DNS lookups. It might also be throttling you based on usage. Can you try sending through another mail server to verify?
Also, if you have shell access, try sending a message from the shell (e.g., echo test | mail -v -s test me#whatever.com) to see what it's doing and how long it takes.
Edit: Just noticed your comment re: Windows. In this case, at least you can try a telnet from the PHP server to port 25 on the mail host to see how long it takes to connect and get the 220 greeting header. (I bet you'll connect immediately but won't get the 220 header for a while.)
You're on to the right idea. Here's what I would do in this scenario:
The user makes a purchase (no ajax unless you want to at this point)
In processing that purchase an email is inserted into the email table with an "id" & "sent" column plus all the other stuff.
User is brought to a success page
The success page kicks off ajax in the background to send the email with that id from the db - and doesn't care about the result
The php page in charge of emailing sends the email and marks the sent column
If someone ever makes a call to your database with an id that's not sent, that email needed to get sent out anyway, so it's ok. If the id doesn't match or the sent column is marked true, you can ignore it. No worries about people trying to send spam using your system.

Categories