我們有一個 HTML 表單,用于收集資料并提交到 PHP 頁面,該頁面將表單資料發送到我們的電子郵件。問題是我們使用的是 AWS 服務器,而 AWS 在埠 25 上有一個塊,導致電子郵件無法正確發送。我們現在的情況是,當我們將表單發送到常規 Gmail 帳戶時,電子郵件會變成垃圾郵件,但是當我們將其發送到 Gsuite 帳戶時,該電子郵件從未收到,甚至不是垃圾郵件。我們正在考慮僅使用 SMTP 發送我們的電子郵件,而不會發送垃圾郵件或不接收它。
我們的 HTML 代碼:
<form name='form1' action="/wp-includes/phpmailer2/sendMail.php" >
<input id="first_name" name="first_name" required="required" type="text" value="" placeholder="" >
</form>
PHP代碼:
<?php
$webmaster_email = "[email protected]";
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "https://rentersshield.org/success/";
$msg = "First Name: " . $first_name . "\r\n" .
function isInjected($str) {
$injections = array('(\n )',
'(\r )',
'(\t )',
'(
)',
'(
)',
'( )',
'( )'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
if (!isset($_REQUEST['first_name'])) {
header( "Location: $feedback_page" );
} elseif (empty($first_name) ) {
header( "Location: $error_page" );
} elseif ( isInjected($first_name) || isInjected($comments) ) {
header( "Location: $error_page" );
} else {
mail( "$webmaster_email", "New Form Submission", $msg );
header( "Location: $thankyou_page" );
}
?>
$first_name = $_REQUEST['first_name'] ;
請讓我們知道是否需要采取措施將其發送到我們的 Gsuite 電子郵件帳戶而不進入垃圾郵件
uj5u.com熱心網友回復:
我建議您查看 PHPMailer 或任何可以更好地控制身份驗證和服務器回應的 SMTP 類。php函式mail太基礎了。
PHPMailer 是經典之作,它在我的所有應用程式上都運行良好。如果您想看一下,這里是一個鏈接:
https://github.com/PHPMailer/PHPMailer
該檔案非常完整,因此您可以毫無問題地實施它。
uj5u.com熱心網友回復:
我們只能通過通過 SMTP 發送來解決我們的問題,但我們必須首先使用以下命令在 cPanel 上安裝 PHPmailer composer require phpmailer/phpmailer
說明:https : //muftsabazaar.com/blog/post/how-to-install-the-phpmailer-in-cpanel 然后我們更新了我們的PHP代碼如下
<?php
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\Exception;
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/SMTP.php';
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'aaa@wa2020!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'New Form submission on Rentersshield Website');
$mail->addAddress('[email protected]', 'JohnUser'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "https://rentersshield.org/success/";
// Content
$first_name = $_REQUEST['first_name'] ;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body =
"First Name: " . $first_name . "\r\n" ;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header( "Location: $thankyou_page" );
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/370106.html
下一篇:JavaFX和Lucene
