【问题标题】:how to send smtp mail from localhost如何从本地主机发送 smtp 邮件
【发布时间】:2015-09-20 08:28:41
【问题描述】:

尝试从本地主机发送电子邮件时,我总是收到此消息。

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("class.phpmailer.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 = "project@reliable.com.pk";  // SMTP username
$mail->Password = "Nov112014"; // 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("mani9418@gmail.com", "Usman Ali Siddiqui");

// 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 Etutionhub!";

// $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";
?>

【问题讨论】:

  • 您安装了邮件服务器?
  • 邮件服务器?不。我只是在我的本地主机上运行此代码。这不可能吗?
  • 读码cmets
  • 如何安装邮件服务器?
  • 我认为这是由于 smtp 端口号配置不正确。

标签: php email smtp


【解决方案1】:
  1. 打开php.ini。对于 XAMPP,它位于C:\XAMPP\php\php.ini。确定您使用的是 WAMP 还是 LAMP 服务器。
    注意:备份 php.ini 文件。

  2. 在php.ini文件中搜索【邮件功能】。

    You can find like below.
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    SMTP = localhost
    ; http://php.net/smtp-port
    smtp_port = 25
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = postmaster@localhost
    

    将 localhost 更改为 ISP 的 smtp 服务器名称。无需更改smtp_port。将其保留为 25。将 sendmail_frompostmaster@localhost 更改为您的域电子邮件地址,该地址将用作发件人地址。

    所以对我来说,会变成这样。

    [mail function]
    ; For Win32 only.
    SMTP = smtp.example.com
    smtp_port = 25
    ; For Win32 only.
    sendmail_from = info@example.com
    
  3. 重新启动 XAMPP 或 WAMP(apache 服务器),以便更改开始工作。

  4. 现在尝试使用mail() 函数发送邮件。

    mail("example@example.com","Success","Great, Localhost Mail works");
    

邮件将从 localhost 发送到 example@example.com,主题行“Success”,正文“Great, Localhost Mail works”。

【讨论】:

    【解决方案2】:

    您可以添加此行
    $mail-&gt;SMTPSecure = "ssl";

    之后

    $mail->SMTPAuth = true;
    

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 1970-01-01
      • 2011-06-16
      • 2012-05-25
      • 2013-08-19
      • 2015-05-18
      • 2017-02-20
      • 2020-02-07
      • 2014-06-02
      相关资源
      最近更新 更多