0

I have a php form that sends emails however I would like to use specified dates as a parameter to send the email for example

?php
$Cristimas = '2021-12-25'; 
$time = strtotime($Cristimas);
while(date('m-d') == date('m-d', $time)) {
    



if(isset($_POST['submit'])){
    $to =  $_POST['email']; // this is your Email address
    $from = "youremail@gmail.com"; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
 
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
}

?>

JhonPeter
  • 21
  • 5
  • you can get some idea from [here](https://stackoverflow.com/questions/24319734/automatic-mail-sending-on-specific-dates-in-php/24319811) or [there](https://stackoverflow.com/questions/24647797/how-to-send-a-mail-in-php-and-mysql-on-a-particular-date) – gretal Dec 21 '21 at 01:54
  • @gretal a question i have multiple actions on a single form what will happen when i put a date on the whole form? – JhonPeter Dec 21 '21 at 01:59
  • Do you mean It will work or not when you use date on the whole form? – gretal Dec 21 '21 at 02:03
  • Let's suppose you have a form that registers, lists, deletes, sends email, creates folder in dropbox all in the same button, when I put a specific date it won't do it only on that date? or not? – JhonPeter Dec 21 '21 at 02:10
  • You can send an email on a specific date...and with the same button too. – gretal Dec 21 '21 at 02:11
  • @gretal I know that but what you sent is the cron job that runs entire forms, you know? – JhonPeter Dec 21 '21 at 02:14
  • i don't know the way about that. – gretal Dec 21 '21 at 02:22

1 Answers1

0

Theres many ways. But they will generally require an external worker of some sort. Running an infinite loop on PHP would not be an effective method and can consume memory unnecessarily.

Cronjobs are a way to achieve this, you can run your script every few minutes and see if the desired time has been achieved yet.

This is a great resource for Cronjobs - Crontab.guru

To enter a cronjob, write crontab -e in a Linux based system.

Then you can run your script on an interval (in this case, every 1 minute):

*/1 * * * * /usr/bin/php /opt/test.php

You can also use frameworks like Laravel to manage tasks using these cronjobs, in an easier fashion.

https://laravel.com/docs/8.x/scheduling

Oliver Kucharzewski
  • 2,523
  • 4
  • 27
  • 51
  • a question let's say i use cro job or lavrel and use it in the contact form, when i send the email will it be sent on the date i want? – JhonPeter Dec 21 '21 at 02:43
  • Yes, you can store the dates of email sending in a database table, and you can make the cronjob loop through it to know when to send emails. A bit more learning to do but its good to know :) – Oliver Kucharzewski Dec 21 '21 at 03:10
  • so i can't use this form do i need to use another one that gets the value directly from the database? – JhonPeter Dec 21 '21 at 03:51
  • Yes. Your form inserts the record into the DB along with the intended send time, then your sender job/process checks for messages that are due to send and sends them. They are independent, connected via the DB. – Synchro Dec 21 '21 at 07:39
  • @Synchro bro I didn't get this one, so will the email be sent on the day I set the date in the cro job? – JhonPeter Dec 21 '21 at 09:33