【问题标题】:Is there any additional requirements to proceed php mailer进行 php mailer 是否有任何其他要求
【发布时间】:2019-10-28 05:05:04
【问题描述】:

我像这样在 Ubuntu 18.04 中安装了 phpmailer

composer require phpmailer/phpmailer

将这些函数添加到contactForm.php

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include './../env.php';

require '../vendor/autoload.php';
require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/phpmailer/src/SMTP.php';
require '../vendor/phpmailer/phpmailer/src/Exception.php';
//require '../vendor/phpmailer/phpmailer/src/OAuth.php.php';



$mail = new \PHPMailer\PHPMailer\PHPMailer;
$msg_admin=file_get_contents('../mailTemplates/mailAdmin.html');
$msg_client=file_get_contents('../mailTemplates/mailUser.html');
//$mail = new \PHPMailer;

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

$main_category = $data->main_category;
$user_name = $data->user_name;
$company_name = $data->company_name;
$country_id = $data->country;
$user_address = $data->address;
$email = $data->email;
$contact_no = $data->contact_no;
$description = $data->description;
$headers = '';

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include './../env.php';

require '../vendor/autoload.php';
require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/phpmailer/src/SMTP.php';
require '../vendor/phpmailer/phpmailer/src/Exception.php';
//require '../vendor/phpmailer/phpmailer/src/OAuth.php.php';



$mail = new \PHPMailer\PHPMailer\PHPMailer;
$msg_admin=file_get_contents('../mailTemplates/mailAdmin.html');
$msg_client=file_get_contents('../mailTemplates/mailUser.html');
//$mail = new \PHPMailer;

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

$main_category = $data->main_category;
$user_name = $data->user_name;
$company_name = $data->company_name;
$country_id = $data->country;
$user_address = $data->address;
$email = $data->email;
$contact_no = $data->contact_no;
$description = $data->description;
$headers = '';

if
(empty($main_category) || empty($user_name) || empty($user_address) || empty($country_id) || empty($email) || empty($contact_no) || empty($description)) {
    echo json_encode(['success' => false, 'input' => 'invalid']);
} else {

    //Do insert query
    $sql = " INSERT INTO enquiry
 (main_category,user_name,company_name,country_id ,user_address,email,contact_no ,description ) VALUES
  ('" . $main_category . "','" . $user_name . "','" . $company_name . "','" . $country_id . "','" . $user_address . "','" . $email . "','" . $contact_no . "','" . $description . "')";
    if (!mysqli_query($conn, $sql)) {
        echo json_encode(['success' => false,'message' => 'Some thing went wrong']);

    } else {
//        echo json_encode(['success' => true,'message' => 'all ok']);

        $query = mysqli_query($conn,"SELECT enquiry.enquiry_id, MainCategory.name,enquiry.main_category FROM enquiry
        LEFT JOIN MainCategory ON MainCategory.category_id=enquiry.main_category
        ORDER BY enquiry.enquiry_id DESC LIMIT 1 ");
        $result =mysqli_fetch_row($query);

        $msg_admin = str_replace('{{enquiry_id}}', $result[0], $msg_admin);
        $msg_admin = str_replace('{{main_category}}', $result[1], $msg_admin);
        $msg_admin = str_replace('{{name}}', $user_name, $msg_admin);
        $msg_admin = str_replace('{{email}}', $email, $msg_admin);
        $msg_admin = str_replace('{{description}}', $description, $msg_admin);
        //Server settings
        $mail->SMTPDebug =4;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.zoho.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'anton@xpl.com';                 // SMTP username
        $mail->Password = '*************!';  // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port =587;                                    // TCP port to connect to
        //Recipients
        //Admin
        $mail->setFrom($email,$user_name);
        $mail->addAddress('noreply@xpl.com','xpl');                 // Add a recipient
        $mail->addReplyTo($email,$user_name);
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'xpl Enquiry';
        $mail->MsgHTML($msg_admin);
        $sent=$mail->send();

        $mail->ClearAllRecipients();
        $mail->ClearReplyTos();
        //$mail->ClearSetFrom();

        //client

        $mail->setFrom('enquiry@xpl.com','xpl');
        $mail->addAddress($email,$user_name);
        $mail->addReplyTo('noreply@xpl.com','xpl');               // Add a recipient
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'xpl Enquiry';
        $mail->MsgHTML($msg_client);
        $sent=$mail->send();

        if (!$sent) {
            //   echo "Mailer Error: " . $mail->ErrorInfo;
            echo json_encode( ['success'=>false,'email'=>'Some thing went wrong', $mail->ErrorInfo]);
        } else {

            echo json_encode( ['success'=>true,'email'=>'send success']);
        }

    }
}

邮件验证和env.php 是准确的
邮件服务器 - smtp.zoho.com

但是,当用户提交表单时,我可以看到上述错误:

以下发件人地址失败:noman@gmail.com : MAIL FROM command failed,,,SMTP server error: MAIL FROM command failed
{"success":false,"email":"出了点问题","0":"以下发件人地址失败: noman@gmail.com : MAIL FROM 命令失败,,,SMTP 服务器错误: MAIL FROM 命令失败SMTP服务器错误:MAIL FROM 命令失败“}2019-10-28 03:22:20 SMTP 注意:在检查是否连接时捕获 EOF
2019-10-28 03:22:20 连接:关闭

我该如何解决这个问题?

【问题讨论】:

  • 您在这个脚本中发生的事情太多,无法说出问题出在哪里。一次测试一个。检查表单的输入,检查数据库查询,检查 SMTP 设置。 SMTPDebug = 4 太冗长了; 3 就足够了,但看起来您还没有发布完整的调试输出 - 失败的 MAIL FROM 应该说 为什么 它失败了。
  • 在哪里可以了解我的 SMTP 设置? ,这可能是问题,邮件凭据是正确的

标签: php phpmailer production-environment


【解决方案1】:

尝试更改 $mail->setFrom($email,$user_name);到$mail-&gt;setFrom("{$email}", "{$user_name}"); 和 $mail->addReplyTo($email,$user_name);到$mail-&gt;addReplyTo("{$email}", "{$user_name}");,我建议现在发送一封邮件以检查一切是否正常 这是我的工作 phpMailer 代码,虽然我使用的是谷歌邮件服务器,

  $mail->isSMTP();                     // Enable TLS encryption, `ssl` also accepted
    $mail->SMTPAuth = true;                            // TCP port to connect to




   //Recipients 
   $mail->setFrom("{$email}", "{$name}");
   $mail->addAddress('AAAA@XXX.com', 'XXXX');              
   $mail->addReplyTo("{$email}", "{$name}");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多