我是編程新手,所以我真的需要我的專案指南。我需要在用戶輸入提醒的前幾天或同一天向用戶發送一封電子郵件作為提醒。我知道我需要一個 cron 作業才能自動發送電子郵件,但我想知道我的編碼是否正確以及我需要添加什么才能讓 cron 作業知道如何發送電子郵件. 我缺乏這么多,所以我很感激任何幫助。
<?php
session_start();
error_reporting(0);
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");
if(mysqli_connect_errno()){
echo "Unable to connect".mysqli_connect_error();
}
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");
if(mysqli_connect_errno()){
echo "Unable to connect".mysqli_connect_error();
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$current_date = date("Y-m-d");
$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");
if(isset($res))
{
$mail = new PHPMailer(true); //create instance of phpmailer
$mail -> isSMTP();
$mail -> Host = 'smtp.gmail.com';
$mail -> SMTPAuth = true;
$mail -> Username = '[email protected]';
$mail -> Password = '*mypassword*';
$mail -> SMTPSecure = 'tls';
$mail -> Port = 587;
$mail -> setFrom('[email protected]');
$mail -> addAddress($email);
$mail -> isHTML(true);
$mail -> Subject = "Reminder!";
$mail -> Body = "test";
$mail -> Send();
}
更新:我更正了代碼并嘗試運行,但收到??此錯誤訊息。p/s:我已經配置了 Gmail 和 App 密碼。
致命錯誤:未捕獲的 TypeError:trim():引數 #1 ($string) 必須是字串型別,mysqli_result 在 C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php:1080 中給出 堆疊跟蹤:#0 C :\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php(1080): trim(Object(mysqli_result)) #1 C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php(1014): PHPMailer\ PHPMailer\PHPMailer->addOrEnqueueAnAddress('to', Object(mysqli_result), '') #2 C:\xampp\htdocs\belajar\send_email.php(37): PHPMailer\PHPMailer\PHPMailer->addAddress(Object(mysqli_result) ) #3 {main} 在第 1080 行的 C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php 中拋出
<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");
if(mysqli_connect_errno()){
echo "Unable to connect".mysqli_connect_error();
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$current_date = date("Y-m-d");
$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");
if(isset($res))
{
$mail = new PHPMailer(true); //create instance of phpmailer
$mail -> isSMTP();
$mail -> Host = 'smtp.gmail.com';
$mail -> SMTPAuth = true;
$mail -> Username = '[email protected]';
$mail -> Password = '*mypassword*';
$mail -> SMTPSecure = 'tls';
$mail -> Port = 587;
$mail -> setFrom('[email protected]');
$mail -> addAddress($email);
$mail -> isHTML(true);
$mail -> Subject = "Reminder!";
$mail -> Body = "test";
$mail -> Send();
}
uj5u.com熱心網友回復:
此行回傳 mysqli_result 而不是電子郵件。
$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
您需要獲取結果并獲取如下值:
$result = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
$array = mysqli_fetch_assoc($result);
$email = $array['email'];
此外,我不知道您為什么使用兩個幾乎相同的查詢。洗掉這一行:
$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");
并替換:
if(isset($res))
和:
if(isset($email) && $email)
更新
如果您想將通知發送給多個收件人,您需要獲取所有電子郵件地址并將它們添加到 foreach 中,如下所示:
<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");
if(mysqli_connect_errno()){
echo "Unable to connect".mysqli_connect_error();
}
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$current_date = date("Y-m-d");
$emails = array();
$result = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
while ($row = mysqli_fetch_assoc($result)) {
$emails[] = $row['email'];
}
if(count($emails) > 0)
{
$mail = new PHPMailer(true); //create instance of phpmailer
$mail -> isSMTP();
$mail -> Host = 'smtp.gmail.com';
$mail -> SMTPAuth = true;
$mail -> Username = '[email protected]';
$mail -> Password = '*mypassword*';
$mail -> SMTPSecure = 'tls';
$mail -> Port = 587;
$mail -> setFrom('[email protected]');
foreach($emails as $email) {
$mail -> addAddress($email);
}
$mail -> isHTML(true);
$mail -> Subject = "Reminder!";
$mail -> Body = "test";
$mail -> Send();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/530192.html
標籤:php电子邮件cron
上一篇:如何在add_subplot中設定col和rows?
下一篇:有特殊情況發郵件
