php - 进行 php mailer 是否有任何其他要求
问题描述
我像这样在 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 命令失败
{"success":false,"email":"Something went wrong","0":"以下发件人地址失败:noman@gmail.com:MAIL FROM 命令失败,,,SMTP 服务器错误:MAIL FROM 命令失败SMTP 服务器错误:MAIL FROM 命令失败”}2019-10-28 03:22:20 SMTP NOTICE:EOF在检查是否连接时被捕获
2019-10-28 03:22:20 连接:关闭
我该如何解决这个问题?
解决方案
尝试更改 $mail->setFrom($email,$user_name); 到$mail->setFrom("{$email}", "{$user_name}");
和 $mail->addReplyTo($email,$user_name); 我建议现在发送一封邮件以 $mail->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}");
推荐阅读
- xcode - 当我在 Apple TV 上安装 IPA 时,XCode 9.3“参数无效”
- python - Python 2.7 CSV 文件读/写 \xef\xbb\xbf 代码
- python - continue 的奇怪语法
- routing - TOR 路由和 TOR 与 VPN
- javascript - 自定义动画在按钮单击时开始?HTML、CSS、JS
- javascript - React Router v4 以前的状态组件
- c# - 使用 Windows 身份验证的 Asp.net 核心 web api - Cors 请求未经授权
- angular - 在另一个订阅中使用来自一个订阅的对象值
- c++ - 当当前不需要原子性时,使用构造函数代替 atomic.store()
- spring - 如何为 Spring REST Docs 编写自定义类型验证器?