Reputation: 131
I want to send email notifications on every new upload, new comment, new post etc.
Right now I'm calling a function:
notify($user_id, $submitter_id, $post_id);
And notify()
processes these ids and calls mail()
, the mail is sent to the 'submitter' and people who've commented earlier. The problem is, the script is taking too long and since i'm sending an AJAX request to this script to save the comments also, the user ends up waiting for notify()
to complete. it's like a chaining process.
Can anyone suggest me a better way to do this? I don't want the Ajax script to wait for:
And also I can't use a cron since I want this to be instant, kind of like FB's.
Upvotes: 1
Views: 2729
Reputation: 640
If you're sending 100s of email at a time, I'd recommend a third party service like Amazon SES or, my personal favorite Postmark. You should be able to ping those services and continue the remainder of your functions without waiting for a callback. Additionally, their latency is low, so your mail would be delivered almost instantaneously.
Upvotes: 2
Reputation: 5207
In FB and so on. it's created like I think you may:
when you need update notify
, you insert to table outbounds
some message
insert into `outbounds` (`email`, `status`, `subject`, `message`, `created`, `sent`)
value ('[email protected]', STATUS::created, 'Some thing is happened', 'Message here', NOW(), NULL);
And script which is always running, or execute like by cron
, or, you can curl this script with set up timeout=1 for send messages
select * from `outbounds` where status = STATUS::created
Don't forget after successfuly sent
update `outbounds` set `status` = STATUS::sent, sent = NOW() where id = $message_id
Upvotes: 3
Reputation: 151720
Use a queue to store e-mails that are due to be sent, and let a script (for example a cron
job) send out those mails. This way, the user only has to wait for the insertion of the mail in the queue, which may not take long.
There are various pitfalls when sending large quantities of mail though, and mail()
really isn't the best solution here.
Upvotes: 1