php - PHPMailer 无法在虚拟主机上运行
问题描述
我已经能够使用 xampp 发送电子邮件,但是当我尝试在我的在线服务器中使用它时,它似乎不起作用。我尝试更改写在我的电子邮件帐户信息上的值。这似乎不起作用,我希望为遇到此问题的任何人提供任何指导或帮助。
这是电子邮件帐户的详细信息
那么这是我的 mailer.php 的代码
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->setFrom('email', 'Test Message');
$mail->addAddress("email");
$mail->isHTML(true);
$mail->Subject = 'This is the Subject';
$mail->Body = 'This has been sent';
$mail->AltBody = 'This has been sent';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: <br>', $mail->ErrorInfo;
}
我收到错误SMTP 错误:无法连接到服务器:连接被拒绝 (111) SMTP 连接()失败。
我对此还是有点陌生,所以提前对任何事情感到抱歉。我觉得我离成功很近了哈哈!
提前感谢您的帮助!:D
解决方案
它是如此简单......使用此代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$name = 'Name to be displayed!';
$message = 'Never Give Up!';
$subject = 'Test mail!';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'yourmail@gmail.com';
$mail->Password = 'youpass';
$mail->SMTPSecure = 'tls';
$mail->addReplyTo($to, $name);
$mail->setFrom($to, $name);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHtml($message);
$mail->send();
?>
希望您在根目录中添加 PHPMailer 库,如果没有,请检查路径
推荐阅读
- c++ - C++ 模板:不能与以前的“类型名称”声明说明符结合
- java - 套接字通道。无效的流标头:00000000
- javascript - RXJS 条码扫描器
- azure - Azure Api 管理:如何计算没有订阅标头的请求数量
- mule - 如何在 MUlesoft 中使用自定义数据源配置数据库
- python - 如何在 Python 中使用 Selenium Webdriver 提取总搜索结果?
- amazon-sagemaker - 使用带有端点的 sagemaker 托管 spacy 模型
- python - 如何使用pypdf在pdf中添加相对文件路径
- sed - sed:如果分隔符之间存在字符串,则在分隔符之间删除
- arrays - 附加到 bash 中的数组,为什么这不起作用?