使用 SMTP 发送 HTML 表单

Send HTML Form using SMTP

我们有一个 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 = "user@example.com";

$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+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $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 电子邮件帐户而不进入垃圾邮件

我建议您查看 PHPMailer 或任何 SMTP class,您可以在其中更好地控制身份验证和服务器响应。 php邮件功能太基础了

PHPMailer 是一个 classic,它在我的所有应用程序上都运行良好。如果您可能想看一看,这里是 link:

https://github.com/PHPMailer/PHPMailer

文档非常完整,因此您可以毫无问题地实施它。

我们只能通过 SMTP 发送来解决我们的问题,但我们必须先使用以下命令在 cPanel 上安装 PHPmailer 作曲家需要 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   = 'example@domain.com';                     // SMTP username
    $mail->Password   = 'emailpass';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

 
    //Recipients
    $mail->setFrom('example@domain.com', 'New Form submission on Rentersshield Website');
    $mail->addAddress('example@domain.com', 'JohnUser');     // Add a recipient
    $mail->addAddress('example@domain.com');               // Name is optional
    $mail->addReplyTo('example@domain.com', 'Information');
    $mail->addCC('example@domain.com');
    $mail->addBCC('example@domain.com');
 
$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}";

}