how to call php page to work in background? - php

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).

Related

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.

Sending large number of emails with attachments in Cakephp

I am using CakeEmail to sent about 7000 emails in a loop. But when I sent emails with small attachments of about (1kb) its working fine. But when I use bigger attcahment files such as of size 800kb it just quite after sending some emails may be 23 or 60. But not completing the process.
The Page quits by giving a message "This webpage is not available"
The code is in a loop where each CakeEmail is initialized to sent to an email address
I tried using
set_time_limit(0);
But it didnt work.
Can anyone helps me out why its not able send using an attachment of 800kb ?
Some shared servers do not allow to run longer scripts, thus not allowing to override the time limit.
Might be you can do something to send 10 emails at a time and then redirect the browser to the same page to send another 10. Just fetch the specific 10 emails using DB LIMIT clause.
Or you can also create a cron job to run the script through shell.
PS - kindly provide the code you are using so I can help in modifying it

SMTP mail really slow

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.

PHPmailer and serve limit

I'm having some problem with a script that send bulk email through PHPmailer class because i exced email per hour limit of my hosting.
I didn't find any option to schedule email, could it be possible to set a delay or time when the email should be send?
Which is the best way, or work around, to send email on scheduled time?
Download a solution specially for newsletters and stuff , in PHP , the most common one is called PHPList , this solution have alot of options that will do schedule and the timeout time automatically for you .

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