How to handle Send Email Limit using Cron in Cpanel? - php

I have developed a custom newsletter sending module using php in which admin can send newsletter to subscribers. Newsletter can be designed within the module and can be send by selecting the category of subscribers that contain thousands of emails with one click. Now issue is i have a restriction of sending 500 emails per hour. Using sleep() cause expiry of max execution script time.
After some research it came to me that this can be done via cron job. I created a cron job in cpanel that is set to every 5 minutes but i am not able to figure out that how this cron job will be triggered when admin click on send newsletter button in administrative panel. I am using swiftmailer library and querying subscriber table on some category id.
Any Help will be greatly appreciated.

This is what I would do:
Have two tables. One for the email being sent and one for the emails that the newsletter is going to with what newsletter the email is receiving.
Structure examples
table newsletter :
id - Auto INC
email_subject - Subject of newsletter
email_html - HTML version of the email
email_plain_text - Plain text version of email
created_datetime - datetime newsletter was created
table newsletter_recipients:
id - Auto INC
newsletter_id - ID of what newsletter is being sent out (create a foreign key referring to the id field on the newsletter table
email - HTML version of the email
name - Name of client
sent - Whether the email was sent off or not (this isn't required, see below)
When the administrator sends the email create the newsletter row with the html/plain text version (you can use only one column, just better to have a html and plain text version of email) and also add the recipients to the newsletter_recipients table referring the newsletter_id to the id of the newsletter you added.
Now you have your PHP cron job file run every hour. This file will check if any clients need a newsletter sent out and if so send it out. Once sent out you can either delete the recipient row or mark it as sent. Personally I would delete them to keep data from backing up. Only run the php job up till 500 or whatever amount of emails you want to send out then have the script end. Next hour it will run and pick up the next 500.
With this system the newsletter wont be sent out immediately, it will be done when the cron job runs. But this is the best solution I can quickly think up for you. That or get a mail server that lets you send out all the emails you want and use something like gearman to handle load balancing.
NOTE: Storing the newsletter in a database can be handy so the admin can look back and see when they sent out their newsletter. Also you can step it up another level and create a sent_date column on the newsletter_recipient table that stores when the email was sent (if you are simply marking the recipient as sent or not instead of deleting) that way you know when the emails finished sending, who received the emails at what time, etc. This is totally up to you).

Related

Laravel email conditional send - checking when last email of this type was sent to a user

I run a scheduled task each day to send emails to customers who have outstanding invoice and need to add a condition to check whether the email has been sent to this customer within the last 72 hours.
I guess I would need to:
Send the mail.
Log the message type and user to the database
Check the database for when the last mail was sent.
I have done a fair bit of reading and wonder if anything like this is already included in Laravel? I have not been able to find much so far.
This isn't built in to the platform, but it should be fairly straightforward to add. Sending an email using Laravel's mail functions triggers a MessageSent event. Attach a listener to that event and you can do whatever you like with it. The event will include the message object as well as any data passed to it.

Send a campaign more than once in MailChimp

I am trying to send an email to users based on their status. The first email needs to be sent after 2 hours, and a second one after 24 hours.
I am running a PHP script that checks for users who belong to a state and I sent the first email to them using the send API call, the problem now, is that when the script runs again (a crobtab that runs every 20 mins), I cannot send the campaign again since apparently you can send a campaign only once.
I am putting the cid directly in my PHP code, so I cannot go and edit the code everytime I want to run the crontab!
Do I have to use the campaigncreate()method everytime the crontab runs?
https://apidocs.mailchimp.com/api/2.0/campaigns/create.php
It would be better if there is an easier way, since an account cannot have more than 32,000 campaigns.
Mailchimp is a bulk email service and isn't designed to send transactional emails.
You should instead try to use Mandrill, a transactional email service built by the same company. And their API is quite simple to use.
Then, you can indeed run your PHP script every hour (or x minutes) to send emails based on user status.

PHP / MySQL Ticket Response - Store E-mail repsonse in database?

I am building a basic support request system where the customer can log in and ask a question and an admin can go in and reply and it will set the status to "Responded" and e-mail the customer to let them know someone has responded.
My question is.. I have a "comments" section which is a log of the interaction between the admin and the customer. If I e-mail the customer the initial response from the admin, then I have a feeling they will just hit "Reply" from their email and start communicating through there, and the logs won't be stored.
I could either e-mail the customer and say "Log in to view the response", or maybe if the customer does hit reply I can somehow track it and insert that in the comments table like they did it from the website. If that is even possible?
Just wondering if there is a standard way to do this and any suggestions you may have.
Thanks!
When sending the email to the user you can have it sent from an email address created for that specific ticket. Something that can identify it with your email system to help you route it back to the php ticketing system.
support(ticketnumber)#domain
support12345#mydomain.com
Then it depends on your email server how to go from there.
There are several useful tips at this question that may help or get your started.
How to get email and their attachments from PHP
If you want their reply to be automatically inserted into the DB, you'll have a assign a cron job in your server to run a php script to detect whether there's a reply from a customer (you need a table listing the customers' email and names.
Each time a customer uses the ticket system their email and name goes into this table).
You'll need to connect to your Inbox too via imap or SMTP, and there are scripts to do this (phpmailer, swiftmailer, etc) and "walk" through each email and see if the sender email matches any in your customers table. Then so an INSERT to the comments table.
Anther way is to read through the emails each time the comments page is loaded, but this will cause the page to take longer to load. However, the data will always be more "real-time" compared to cron jobs.
You could use email piping (if your server supports it).
In the subject, you'd have a unique identifier which contains the ticket ID or something unique to the ticket. Example: "How do I eat food [Question: #1234]", where 1234 is the ticket ID.
In your control panel, you would set up an email forwarder to your email piping script.
This tutorial offers the basics to email piping, and I used it as the base for my piping script: http://www.damnsemicolon.com/php/parse-emails-in-php-with-email-piping-part-1

What's the best way to send email to a large list where each email is to be personalized with information from a database and other sources?

I run a daily deals aggregator. I'd like to send daily emails to users who sign up for them. The site itself is PHP/MySQL. What should I use to send emails? Each email will be specific to each user depending on the kind of deals that user wants to received. What do sites like Quora or other daily deal sites use to send emails?
I think you can use loops to loop through the emails that are stored in an array where the keys of the array are the respective user ids, and then send emails accordingly. You can use the foreach loop.
You could create a table in your database to temporarily store the addresses that have not yet received a mail and create a cronjob that sends 200 email per 30 minutes or so. Like this you can avoid clicking some button and wait for hours until your script is done.
Remove every address that has received the mail until your table is empty. Use another cronjob that runs every week or month (cycle to send newsletter) that fills the temporary table with your recipient addresses.

PHP Bulk Email - Dedicated IP Max?

Is there any way i can send out about 3000+ emails from one php script request without overloading a dedicated IP... the max would be 500 per hour?
If you dont get me.. here is detailed :)
I can only send out 500 emails via the mail() function in PHP per hour via my dedicated IP, is there any way i could send out for example 3000 rows of emails pulled from an email address but stagger the mail() functions out for 500 per hour...
Thanks already!
Create 2 tables, one for the email message and one for the list of recipients.
Then create a script to be run by cron that checks if there is a new message in the message table and if so sends a batch of email to the next set of recipients. Marking each recipient after the mail is sent.
Then you create a web interface for your client to create a message and attach recipients to the message once the user marks the message as ready to go your cron job picks it up and processes it.
If there aren't any messages to be sent your cron job doesn't do anything.
You could sleep between the calls, or, if they're already in a database, put a field in there that says when they were sent. Then you select the ones that haven't been sent, and start from there.
I would put a field in the DB to show when the last email was sent to each user and what email it was. I would also have another DB table to show each email you sent and if it has been sent to all users yet.
User Table:
Id, UserName, Email, etc, DateTimeOfLastEmail, LastEmailId
Email Table:
Id, EmailSubject, EmailContent, DateTimeSent, SentToAll(True/False), DateTimeOfFinish
Thanks for all the answers!
The best way i found was actually to simply sleep() between calls using the sleep() as i tested 400 mails, this took 17 seconds :)
It is unlikely the user will send more than the 450 limit ... but if they do i have an if statement before the while() ends checking if there are more than 450 rows, if so it will sleep between each... this works without fiddly databases :)
Thanks!
well after doing some math you could send an email every 8.3 seconds (498/hr) but it doesn't solve the problem. I think another approach would be to use a DB, query for the 500 and have a CRON job run the script every hour.
So in the DB table you could have the script update a field after the email has been sent so the next cron job will query and get the next 500 emails that need to be sent.

Categories