【问题标题】:Send HTML Form using SMTP使用 SMTP 发送 HTML 表单
【发布时间】:2022-01-07 01:56:10
【问题描述】:

我们有一个 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 电子邮件帐户而不进入垃圾邮件

【问题讨论】:

  • 良好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您调试代码Take a quick look at a coding standard 为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
  • 是的,也许可以通过 GSuite SMTP 而不是本地邮件服务器发送。在 PHP 中使用 PHPMailer 可以让这更容易。
  • $msg = "First Name: " . $first_name . "\r\n" . 错字,那只狗不会打猎
  • $first_name = $_REQUEST['first_name'] ; PHP 标签外,另一只不会打猎的狗?
  • 如何使用 SMTP 而不是本地邮件服务器,这是我们的问题。当我们按照github.com/PHPMailer/PHPMailer中的例子,代码不起作用

标签: php html email


【解决方案1】:

我们只能通过 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}";

}

【讨论】:

    【解决方案2】:

    我建议您看看 PHPMailer 或任何您可以更好地控制身份验证和服务器响应的 SMTP 类。 php函数mail太基础了。

    PHPMailer 是一个经典之作,它在我所有的应用程序上都运行良好。如果你想看看,这里有一个链接:

    https://github.com/PHPMailer/PHPMailer

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

    【讨论】:

    • 我们试图这样做,但它给了我们以下错误“此页面无法正常工作”链接:rentersshield.org/mailer.php
    • 我去看了,现在好像可以用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2016-12-18
    • 2016-01-13
    • 2016-11-11
    相关资源
    最近更新 更多