【问题标题】:PHP to connect to Hotmail to send email?PHP连接Hotmail发送邮件?
【发布时间】:2015-07-14 03:09:42
【问题描述】:

目前我正在尝试使用 PHPmailer 发送电子邮件。下面是代码

<?php     
require("phpmailer/class.phpmailer.php");   
$mail = new PHPMailer();   // ---------- adjust these lines ---------------------------    ------------   
$mail->Username = "(username@hotmail.com)"; // your hotmail user name    
  $mail->Password = "password";
  $mail->AddAddress"(username@hotmail.com)"; // recipients email     
  $mail->FromName = "test"; // readable name     
  $mail->Subject = "Subject title";     
  $mail->Body    = "Here is the message you want to send to your friend.";     
  //-----------------------------------------------------------------------     
  $mail->Host = "smtp.live.com"; // GMail     
  $mail->Port = 25;     $mail->IsSMTP(); // use SMTP  
  $mail->SMTPAuth = true; // turn on SMTP authentication 
  $mail->From = $mail->Username;  
  if(!$mail->Send())     
      echo "Mailer Error: " . $mail->ErrorInfo;     
  else       
      echo "Message has been sent";     

  ?> 

我已经用 PHPMailer 尝试了 SSL,端口 587 用于 smtp.live.com,为什么它不起作用?

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

我无法远程登录 smtp.live.com 25,587。 smtp.gmail.com 等等等等。我该怎么办? :(

【问题讨论】:

  • “我不能将telnet 转为smtp.live.com”是什么意思?你遇到了什么错误?
  • 无法在端口 25 上打开与主机的连接;连接失败。
  • @user127886,您的主机可能阻止了这些端口。这很常见。
  • 嗨布拉德,我还有其他方法可以解决它吗?但奇怪的是,我的个人hotmail账号可以发到我公司的邮箱,而我公司的邮箱也可以发邮件到我的个人hotmail账号,也就是说收发邮件都不会被屏蔽。
  • 我试过telnet smtp.live.com 587,效果很好。回复220 BLU0-SMTP166.phx.gbl Microsoft ESMTP MAIL Service

标签: php html phpmailer


【解决方案1】:
<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 2;                      // enables SMTP debug information (for testing)
                                            // 1 = errors and messages
                                            // 2 = messages only
$mail->SMTPAuth   = true;                   // enable SMTP authentication
$mail->SMTPSecure = "tls";                  // sets the prefix to the servier
$mail->Host       = "smtp.live.com";        // sets hotmil as the SMTP server
$mail->Port       = 587;                    // set the SMTP port for the hotmail server
$mail->Username   = "useyourownemail@hotmail.com";      // hotmail username
$mail->Password   = "useyourownpassword";           // hotmail password
$mail->SetFrom('yourname@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject    = "PHPMailer Test Subject via smtp (hotmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);

$address = "anemail@domain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

【讨论】:

  • 感谢您的回复
【解决方案2】:

Talha 的答案对我有用。尝试评论 $mail->IsSMTP();我还评论了这部分 $mail->Port = 587;

【讨论】:

    【解决方案3】:

    587 端口为我工作。

    无需运行 IsSMTP()。注释掉它会抛出异常。

    如果它解决了您的问题,请不要忘记将其标记为答案 :)

    【讨论】:

    • 你的回答不真实
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 2014-12-15
    • 2017-05-30
    • 2015-10-20
    • 1970-01-01
    • 2015-05-11
    • 2016-02-22
    • 2013-07-02
    相关资源
    最近更新 更多