0

i'm working on a platform to send emailing campaign in php. i've just a problem, how can i set php mail Sender function to work at an hour define by th user. I was thinking to use Cron job but that's seems to be difficult to write a Cron job that send an url request (because i need email parameters so i can't just execute php script). I hope you will can help me to find a solution to that problem. Thank you for your answers !

  • 5
    Please specify what you already have done to complete this task. i will give you a little hint you are on the right track for cronjob but also think databases. I am sorry to tell you but we are not here to write your code we are here to help – Eningly Feb 28 '19 at 08:28
  • i'm using sendinblue api i can write an email, and create campaign. – hardy charles Feb 28 '19 at 08:30
  • i'm not here to have the code i just want to find a way to do this i really don't know how i can do – hardy charles Feb 28 '19 at 08:31
  • 3
    Eningly was right without seeing code we can't help but if you want to schedule emails to send at specific time then you have to store email parameters in database like to,from,subject,content,filepath(if any attachment) and cron job read from db and send email – bob_1982 Feb 28 '19 at 08:33
  • i really have done nothing about cron job i'm just thinking about how i can do. do you have some documentation about how to write cron job in php. i would want to create a cron job automaticaly when user enter emailing parameters – hardy charles Feb 28 '19 at 08:41
  • You should check with your host to see if they have any emailing restrictions (eg max number per day, max number per hour, etc – SpacePhoenix Feb 28 '19 at 09:36

1 Answers1

1
  1. Do your homework. You should research on cron and PHP

How to create cron job using PHP?

https://www.a2hosting.com/kb/developer-corner/php/run-php-scripts-from-cron-jobs

https://packagist.org/packages/peppeocchi/php-cron-scheduler

  1. You will have to save the schedules in the database, so you can edit, remove and reschedule then in case your cron table is lost.

  2. Since PHP exit after it is ran by the server, you can use the system's cron table to run your Sender script at the scheduled time. For this you have 2 options:

Option 1

If you have your server running at localhost :9091

Persist in the cron job the schedule ID from the database, so your Sender script can get the settings from the database to send the e-mail.

# replace * * * * * with the schedule from the user
* * * * * curl localhost:9091/sendEmail?id=123

You can also put the information directly, but you will lost the schedule if you lose the cron table if you don't save at the database, so it is better to save it.

# replace * * * * * with the schedule from the user
* * * * * curl localhost:9091/sendEmail?addr=name@email.com&subscription=pets,house,food

Option 2

If your don't expect to have many users/schedules, you can create cron jobs at every time the users request it, then you call your Sender script and this script will get from the database (SELECT query) all the schedules and send the e-mail to it.

# Every hour for example
0 * * * * php path/to/script.php
jpenna
  • 8,426
  • 5
  • 28
  • 36