首页 > 解决方案 > 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

标签: phpphpmailer

解决方案


它是如此简单......使用此代码:

<?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 库,如果没有,请检查路径


推荐阅读