我正在构建电子邮件验证脚本,并且我使用的是Ajax,因此每当用户填写虚假或无效的电子邮件时,都会显示带有错误名称的错误栏,并且当电子邮件为真且属于该电子邮件时如果是有效帐户,则会向用户发送一封带有说明的电子邮件,错误栏显示已发送电子邮件。 问题是,即使邮件功能无法正常工作,错误栏也会一直显示已发送电子邮件。 因此,我不会在错误栏中显示一些行来检测是否未发送电子邮件以显示“未发送电子邮件”。
这是我的php文件:
<?php
include '../../app/init.php';
/* Clean email */
$recover_email = $_POST['recover_email'];
/* check if email is valid */
if(!filter_var($recover_email, FILTER_VALIDATE_EMAIL)) {
/* email not valid */
echo 3;
/* Check if email exist */
} else if(Database::exists('user_id', 'users', ['email' => $recover_email])) {
/* Define some variables */
$user_id = Database::simple_get('user_id', 'users', ['email' => $recover_email]);
$lost_password_code = md5($recover_email + microtime());
/* Update the current activation email */
$database->query("UPDATE `users` SET `lost_password_code` = '{$lost_password_code}' WHERE `user_id` = {$user_id}");
/* Send the email */
sendmail($recover_email, $settings->contact_email, $language->lost_password->email->title, sprintf($language->lost_password->email->content, $settings->url, $recover_email, $lost_password_code));
/* email exist */
echo true;
} else if(!Database::exists('user_id', 'users', ['email' => $email])) {
/* email not exist */
echo false;
}
?>
这是我的邮件功能:
function send_mail($to, $from, $title, $message) {
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $title, $message, $headers);
}
function sendmail($to, $from, $title, $message) {
global $settings;
if(!empty($settings->smtp_host) && !empty($settings->smtp_port) && !empty($settings->smtp_user) && !empty($settings->smtp_pass)) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->isHTML(true);
$mail->Host = gethostbyname('tls://'.$settings->smtp_host);
$mail->Port = $settings->smtp_port;
$mail->Username = $settings->smtp_user;
$mail->Password = $settings->smtp_pass;
$mail->setFrom($from, $settings->title);
$mail->addReplyTo($from, $settings->title);
$mail->addAddress($to);
$mail->Subject = $title;
$mail->Body = $message;
if (!$mail->send()) {
send_mail($to, $from, $title, $message);
}
} else {
send_mail($to, $from, $title, $message);
}
}
我只需要在php文件上添加几行即可将其返回作为对ajax文件的响应
答案 0 :(得分:1)
mail()
如果成功接受了邮件发送,则返回TRUE,否则返回FALSE。因此,您需要运行if语句来检查邮件是否被接受发送。
<?php
function send_mail($to, $from, $title, $message)
{
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $title, $message, $headers)) {
return true;
} else {
return false;
}
}
function sendmail($to, $from, $title, $message)
{
global $settings;
if (!empty($settings->smtp_host) && !empty($settings->smtp_port) && !empty($settings->smtp_user) && !empty($settings->smtp_pass)) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->isHTML(true);
$mail->Host = gethostbyname('tls://' . $settings->smtp_host);
$mail->Port = $settings->smtp_port;
$mail->Username = $settings->smtp_user;
$mail->Password = $settings->smtp_pass;
$mail->setFrom($from, $settings->title);
$mail->addReplyTo($from, $settings->title);
$mail->addAddress($to);
$mail->Subject = $title;
$mail->Body = $message;
if (!$mail->send()) {
if (!send_mail($to, $from, $title, $message)) {
echo "Error sending email, please try again later";
}
}
} else {
if (!send_mail($to, $from, $title, $message)) {
echo "Error sending email, please try again later";
}
}
}
答案 1 :(得分:-2)
只需在if条件中使用mail(),并在if和else块中打印适当的消息。它会解决问题
if(mail($to, $title, $message, $headers)){
//success message
}
else{
//error message
}