我这样在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命令失败,SMTP服务器错误:MAIL FROM命令失败
{“成功”:false,“电子邮件”:“某些地方出了问题”,“ 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连接:已关闭
我该如何解决?
答案 0 :(得分:0)
尝试更改$ mail-> setFrom($ email,$ user_name);到$mail->setFrom("{$email}", "{$user_name}");
和$ mail-> addReplyTo($ email,$ user_name);到$mail->addReplyTo("{$email}", "{$user_name}");
,我建议暂时只发送一封邮件,以检查每件事是否正常
这是我工作的phpMailer代码,尽管我使用的是Google邮件服务器
$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}");