php - (PHP 初学者帮助)如果您打算使用 SMTP,请在此行之后添加您的 SMTP 代码
问题描述
我是 PHP 和 HTML 的新手,在使用 PHP 发送电子邮件方面需要帮助。我使用的代码是一个带有良好文档代码的 web 模板,这可能是一个简单的修复。
问题:当我在 Live Server 中为联系表单发送测试电子邮件并按“发送”时,没有任何反应,没有电子邮件通过,也没有成功消息。我是否在我的 PHP 代码中遗漏了某些内容,或者我是否需要设置 SMTP?
*我的知识很基础,所以这个问题可能会被问错。感谢您的任何反馈!
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
// If you intend you use SMTP, uncomment next line
require 'phpmailer/src/SMTP.php';
// Set the recipient email address here
$recipients = array();
$recipients[] = array(
'email' => 'contact@westonborst.com',
'name' => 'Weston'
);
// Set the sender email address here
$sender = array(
'email' => 'contact@westonborst.com',
'name' => 'Weston'
);
// reCaptcha Secret Key - Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = '';
// PHPMailer Initialization
$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line
// End of SMTP
// Form Messages
$message = array(
'success' => 'Thank you for your message. It has been sent.',
'error' => 'There was an error trying to send your message. Please try again later.',
'error_bot' => 'Bot Detected! Message could not be send. Please try again.',
'error_unexpected' => 'There was an unexpected error trying to send your message. Please try again later.',
'recaptcha_invalid' => 'Captcha not Validated! Please Try Again.',
'recaptcha_error' => 'Captcha not Submitted! Please Try Again.'
);
// Form Processor
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$prefix = !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
$submits = $_POST;
$botpassed = false;
$message_form = !empty( $submits['message'] ) ? $submits['message'] : array();
$message['success'] = !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
$message['error'] = !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
$message['error_bot'] = !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
$message['error_unexpected'] = !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
$message['recaptcha_invalid'] = !empty( $message_form['recaptcha_invalid'] ) ? $message_form['recaptcha_invalid'] : $message['recaptcha_invalid'];
$message['recaptcha_error'] = !empty( $message_form['recaptcha_error'] ) ? $message_form['recaptcha_error'] : $message['recaptcha_error'];
// Bot Protection
if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = true;
}
if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = false;
}
if( $botpassed == false ) {
echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
exit;
}
// reCaptcha
if( isset( $submits['g-recaptcha-response'] ) ) {
$recaptcha_data = array(
'secret' => $recaptcha_secret,
'response' => $submits['g-recaptcha-response']
);
$rc_verify = curl_init();
curl_setopt( $rc_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
curl_setopt( $rc_verify, CURLOPT_POST, true );
curl_setopt( $rc_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
curl_setopt( $rc_verify, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $rc_verify, CURLOPT_RETURNTRANSFER, true );
$rc_response = curl_exec( $rc_verify );
$g_response = json_decode( $rc_response );
if ( $g_response->success !== true ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_invalid'] . '" }';
exit;
}
}
$html_title = !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
$forcerecaptcha = ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
$replyto = !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;
if( $forcerecaptcha ) {
if( !isset( $submits['g-recaptcha-response'] ) ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_error'] . '" }';
exit;
}
}
$mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'Form response from your website';
$mail->SetFrom( $sender['email'] , $sender['name'] );
if( !empty( $replyto ) ) {
if( count( $replyto ) > 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$replyto_n = $submits[ $replyto[1] ];
$mail->AddReplyTo( $replyto_e , $replyto_n );
} elseif( count( $replyto ) == 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$mail->AddReplyTo( $replyto_e );
}
}
foreach( $recipients as $recipient ) {
$mail->AddAddress( $recipient['email'] , $recipient['name'] );
}
$unsets = array( 'prefix', 'subject', 'replyto', 'message', $prefix . 'botcheck', 'g-recaptcha-response', 'force_recaptcha', $prefix . 'submit' );
foreach( $unsets as $unset ) {
unset( $submits[ $unset ] );
}
$fields = array();
foreach( $submits as $name => $value ) {
if( empty( $value ) ) continue;
$name = str_replace( $prefix , '', $name );
$name = ucwords( str_replace( '-', ' ', $name ) );
if( is_array( $value ) ) {
$value = implode( ', ', $value );
}
$fields[$name] = $value;
}
$response = array();
foreach( $fields as $fieldname => $fieldvalue ) {
$response[] = $fieldname . ': ' . $fieldvalue;
}
$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$body = implode( "<br>", $response ) . $referrer;
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
if( $autores && !empty( $replyto_e ) ) {
$send_arEmail = $autoresponder->Send();
}
echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
else:
echo '{ "alert": "error", "message": "' . $message['error'] . '<br><br><strong>Reason:</strong><br>' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}
?>
解决方案
不知道为什么它不会发送,这是我试过的?有什么建议吗?(我从代码中删除了我的密码)
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
// If you intend you use SMTP, uncomment next line
require 'phpmailer/src/SMTP.php';
// Set the recipient email address here
$recipients = array();
$recipients[] = array(
'email' => 'borstweston@gmail.com',
'name' => 'Weston'
);
// Set the sender email address here
$sender = array(
'email' => 'borstweston@gmail.com',
'name' => 'Weston'
);
// reCaptcha Secret Key - Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = '';
// PHPMailer Initialization
$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line
$mail->IsSMTP(); // enable SMTP
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com.";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "borstweston@gmail.com";
$mail->Password = "";
$mail->SetFrom("borstweston@gmail.com");
$mail->Subject = "";
$mail->Body ="";
$mail->AddAddress($sendTo);
// End of SMTP
// Form Messages
$message = array(
'success' => 'Thank you for your message. It has been sent.',
'error' => 'There was an error trying to send your message. Please try again later.',
'error_bot' => 'Bot Detected! Message could not be send. Please try again.',
'error_unexpected' => 'There was an unexpected error trying to send your message. Please try again later.',
'recaptcha_invalid' => 'Captcha not Validated! Please Try Again.',
'recaptcha_error' => 'Captcha not Submitted! Please Try Again.'
);
// Form Processor
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$prefix = !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
$submits = $_POST;
$botpassed = false;
$message_form = !empty( $submits['message'] ) ? $submits['message'] : array();
$message['success'] = !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
$message['error'] = !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
$message['error_bot'] = !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
$message['error_unexpected'] = !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
$message['recaptcha_invalid'] = !empty( $message_form['recaptcha_invalid'] ) ? $message_form['recaptcha_invalid'] : $message['recaptcha_invalid'];
$message['recaptcha_error'] = !empty( $message_form['recaptcha_error'] ) ? $message_form['recaptcha_error'] : $message['recaptcha_error'];
// Bot Protection
if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = true;
}
if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
$botpassed = false;
}
if( $botpassed == false ) {
echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
exit;
}
// reCaptcha
if( isset( $submits['g-recaptcha-response'] ) ) {
$recaptcha_data = array(
'secret' => $recaptcha_secret,
'response' => $submits['g-recaptcha-response']
);
$rc_verify = curl_init();
curl_setopt( $rc_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
curl_setopt( $rc_verify, CURLOPT_POST, true );
curl_setopt( $rc_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
curl_setopt( $rc_verify, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $rc_verify, CURLOPT_RETURNTRANSFER, true );
$rc_response = curl_exec( $rc_verify );
$g_response = json_decode( $rc_response );
if ( $g_response->success !== true ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_invalid'] . '" }';
exit;
}
}
$html_title = !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
$forcerecaptcha = ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
$replyto = !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;
if( $forcerecaptcha ) {
if( !isset( $submits['g-recaptcha-response'] ) ) {
echo '{ "alert": "error", "message": "' . $message['recaptcha_error'] . '" }';
exit;
}
}
$mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'Form response from your website';
$mail->SetFrom( $sender['email'] , $sender['name'] );
if( !empty( $replyto ) ) {
if( count( $replyto ) > 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$replyto_n = $submits[ $replyto[1] ];
$mail->AddReplyTo( $replyto_e , $replyto_n );
} elseif( count( $replyto ) == 1 ) {
$replyto_e = $submits[ $replyto[0] ];
$mail->AddReplyTo( $replyto_e );
}
}
foreach( $recipients as $recipient ) {
$mail->AddAddress( $recipient['email'] , $recipient['name'] );
}
$unsets = array( 'prefix', 'subject', 'replyto', 'message', $prefix . 'botcheck', 'g-recaptcha-response', 'force_recaptcha', $prefix . 'submit' );
foreach( $unsets as $unset ) {
unset( $submits[ $unset ] );
}
$fields = array();
foreach( $submits as $name => $value ) {
if( empty( $value ) ) continue;
$name = str_replace( $prefix , '', $name );
$name = ucwords( str_replace( '-', ' ', $name ) );
if( is_array( $value ) ) {
$value = implode( ', ', $value );
}
$fields[$name] = $value;
}
$response = array();
foreach( $fields as $fieldname => $fieldvalue ) {
$response[] = $fieldname . ': ' . $fieldvalue;
}
$referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$body = implode( "<br>", $response ) . $referrer;
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
if( $autores && !empty( $replyto_e ) ) {
$send_arEmail = $autoresponder->Send();
}
echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
else:
echo '{ "alert": "error", "message": "' . $message['error'] . '<br><br><strong>Reason:</strong><br>' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}
?>
推荐阅读
- android - 安装 apk 时出错:INSTALL PARSE_FAILED_NOT_APK
- cassandra - 在 Cassandra 中设置范围查询的主键
- excel - 如何基于一列导出多个excel
- google-coral - 带有 OpenCV 的 Coral USB 加速器
- node.js - 如何修复“我有作者和书籍架构,在 Postman 结果中,我想为一本书显示两个或多个作者”
- regex - 从末尾开始连续两个大写字母
- php - 二维json数组的回显部分
- macos - SwiftUI 对悬停事件不是很敏感
- javascript - 在shopify主题的产品页面上传图片字段
- batch-file - 如何使用正则表达式在 .txt 文件中查找字符串?