使用PHP根据日期发送定期发送的电子邮件

时间:2013-09-28 20:52:40

标签: php email cron phpmailer recurring

我不知道从哪里开始设置自动重复发送电子邮件提醒应用。我目前有一个将数据插入msyql数据库的表单。

<form method="post" action="save.php">

<p>
    <label for="name">Full Name</label><br />
    <input type="text" name="name" data-h5-errorid="error-name" required />
    <span id="error-name" class="error">Please enter a full name</span>
</p>

<p>
    <label for="address">Address (Street Address Only)</label><br />
    <input type="text" name="address" data-h5-errorid="error-address" required />
    <span id="error-address" class="error">Please enter an address</span>
</p>

<p>
    <label for="email">Email</label><br />
    <input type="email" name="email" data-h5-errorid="error-email" required />
    <span id="error-email" class="error">Please enter a valid email address</span>
</p>

<p>
    <label for="date">Date of Service</label><br />
    <input id="date" type="text" data-h5-errorid="error-service" name="date" required />
    <span id="error-service" class="error">Please choose a date</span>
</p>

<p>
    <label for="reminder">Reminder</label><br />
    <select name="reminder">
        <option value="1">30 Days</option>
        <option value="2">60 Days</option>
        <option value="3">Semiannual</option>
        <option value="4">Annual</option>
    </select>
</p>

<p>
    <label for="notes">Notes on Service</label><br />
    <textarea name="notes" rows="15"></textarea>
</p>

<p>
    <input type="submit" value="Submit" />
</p>

</form>

我正在将所有字段存储在数据库中,并使用http://medoo.in/使其正常工作。如何根据服务日期和选定的提醒(每30天,每半天,每半年,每年)创建一个PHP脚本来发送定期发送的电子邮件?我正在考虑使用PHP邮件程序发送实际的电子邮件并运行每日的cron作业。

2 个答案:

答案 0 :(得分:0)

<?php

//first start with a MySQL query to gather the rows to send an email
//this is for 30 days but you can adjust the numbers for each case

$30_day_query = "SELECT * FROM users WHERE reminder = 1 AND date
BETWEEN DATE_SUB(NOW(),INTERVAL 29 DAY) AND DATE_SUB(NOW(), INTERVAL 30 DAY)";

$execute = mysql_query($30_day_query);

while($row = mysql_fetch_array($execute)){
//get info of user
$name = $row['name'];
$email = $row['email'];

//send the email however you want
}
//repeat for the rest of the reminder options
?>

如果您在每日cron作业中运行此功能,则不会发送重复的电子邮件。

答案 1 :(得分:0)

只需编写一段代码即可为您的cronjob配置时间间隔。(使用表单中收到的数据。) 如果您使用的是DirectAdmin,请按照DušanRadojević的Cronjob execute PHP script回答。 那差不多完了!