【问题标题】:Getting SMTP error while using PHpmailer to send mail through my website使用 PHpmailer 通过我的网站发送邮件时出现 SMTP 错误
【发布时间】:2023-04-01 19:20:01
【问题描述】:

我正在尝试通过 PHP 中的 PHP Mailer 类从我的网站发送电子邮件。 下面给出了代码,但是当我尝试发送电子邮件时,我收到了这两个错误:

SMTP 错误:无法连接到 SMTP 主机。无法发送消息。

邮件程序错误:SMTP 错误:无法连接到 SMTP 主机。

出了什么问题。请帮忙?

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("lib/PHPMailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_myemail@host.com";  // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("myemail@host.com", "namit pathak");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

【问题讨论】:

    标签: php html forms email


    【解决方案1】:

    您需要在此行中包含您的电子邮件用户名和密码,目前您使用的是原始虚拟设置:

    $mail->Username = "send_from_myemail@host.com";  // SMTP username
    $mail->Password = "password";
    

    【讨论】:

      【解决方案2】:

      您一次只能将主机设置为 localhost,您必须配置本地 smtp 服务器

      $mail->Host = "localhost";
      

      如果你有谷歌帐户,那就试试吧

      $mail->Host = "smtp.google.com";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-03
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 2014-02-21
        • 2019-05-06
        • 1970-01-01
        相关资源
        最近更新 更多