php 如何在特定日期发送电子邮件?

vbopmzt1  于 2023-05-12  发布在  PHP
关注(0)|答案(1)|浏览(102)

我有一个发送电子邮件的PHP表单*但是我想使用指定的日期作为参数来发送电子邮件,例如 *

?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.
    }
}

?>
sdnqo3pr

sdnqo3pr1#

有很多方法。但它们通常需要某种外部工作人员。在PHP上运行无限循环不是一种有效的方法,并且会不必要地消耗内存。

Cronjobs是实现此目的的一种方法,您可以每隔几分钟运行一次脚本,看看是否达到了所需的时间。

This is a great resource for Cronjobs - Crontab.guru
要输入cronjob,请在基于Linux的系统中编写crontab -e
然后,您可以按时间间隔运行脚本(在本例中为每1分钟):

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

您也可以使用像Laravel这样的框架来管理使用这些cronjob的任务,以更简单的方式。
https://laravel.com/docs/8.x/scheduling

相关问题