php - 如何在 PHP 中使用 SMTP 发送电子邮件?
问题描述
我正在努力使用 SMTP PHP 发送电子邮件。我正在使用 gmail SMTP。谁能告诉我这段代码的问题在哪里?我正在 cpanel 中尝试此代码,我无法找到此代码中的问题所在。cpanel 错误日志中没有 PHP 错误消息。
<?php
require "config/config.php"; //include config file
// GOOGLE GOODNESS
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (isset($_POST['submit'])) {
$userIP = $_SERVER["REMOTE_ADDR"];
$recaptchaResponse = $_POST['g-recaptcha-response'];
$secretKey = $yoursecretkey;
$request = file_get_contents("https://www.google.com/recaptcha /api/siteverify?secret={$secretKey}&response={$recaptchaResponse}&remoteip={$userIP}");
if (!strstr($request, "true")) {
echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong>There was a problem with the Captcha, you lied to us! you are a robot! or you just didnt click it :)</div>';
} else {
// echo "WORKS MOTHERFUCKER CONGRATS!";
if (isset($_POST['submit'])) {
$message = 'Full Name: ' . $_POST['fullname'] . '<br />
Subject: ' . $_POST['subject'] . '<br />
Phone: ' . $_POST['phone'] . '<br />
Email: ' . $_POST['emailid'] . '<br />
Comments: ' . $_POST['comments'] . '
';
require "PHPMailer-master/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "xxxxxxx"; // Your full Gmail address
$mail->Password = "xxxxxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxxxxx", "xxxxxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? '<div class="alert alert-success" role="alert"><strong>Success! </strong>Message Sent Successfully!</div>' : '<div class="alert alert-danger" role="alert"> <strong>Error!</strong>There was a problem delivering the message.</div>';
unset($mail);
}
}
}
?>
The only error i get is: There was a problem delivering the message on contact form. class.phpmailer.php is included in PHPMailer-master folder. I will appreciate your assistance.
强文本
解决方案
谢谢大家,我使用了我的网络托管电子邮件地址、SMTP 主机和 SMTP 端口。有用。我不明白为什么它不使用 gmail SMTP 发送。
推荐阅读
- c++ - 更好的方法来存储指针的地址和值,并在不使用动态内存分配的情况下重用相同的指针变量
- python - 如何从对应于特定键的字典中获取特定值,并将该值添加到变量中?
- json - 如何在 ASP.Net Core 中将大型 json 对象流式传输到浏览器
- reactjs - 堆栈溢出紧急情况!REACT-ROUTER-DOM 安装失败
- snowflake-cloud-data-platform - 我如何弄清楚为什么这个多面体在雪花中无效?
- dns - systemd-resolved - 如何启用单标签域
- angular - 如何在角度范围内定义样式?
- css - 使用 Shinymanager R 嵌入图像
- spring-boot - 在 application.properties 中通过服务名称为 @FeignClient 提供 URL,而不使用负载均衡器
- azure - 如何将多个 Blob 容器从一个存储帐户复制到另一个存储帐户(不同的订阅)