2
votes

currently I am trying to use PHPmailer to send email out. Here are the codes below

<?php     
require("phpmailer/class.phpmailer.php");   
$mail = new PHPMailer();   // ---------- adjust these lines ---------------------------    ------------   
$mail->Username = "([email protected])"; // your hotmail user name    
  $mail->Password = "password";
  $mail->AddAddress"([email protected])"; // 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";     

  ?> 

I've tried SSL, port 587 for smtp.live.com with PHPMailer, why doesn't it work?

The error is "SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host."

I cannot telnet smtp.live.com 25,587. smtp.gmail.com etc etc.. What should i do? :(

3
What do you mean by "I cannot telnet to smtp.live.com"? What error are you getting?icktoofay
Could not open connection to the host on port 25; Connect failed.user127886
@user127886, Your host is likely blocking those ports. It is common.Brad
Hi Brad, is there any other way I can get around it? But the weird thing is, my personal hotmail account can send to my company's email while my company's email also can send email to my personal hotmail account, this means that both incoming and outgoing emails aren't blocked.user127886
I tried telnet smtp.live.com 587 and it worked fine. Responded with 220 BLU0-SMTP166.phx.gbl Microsoft ESMTP MAIL Service.Nadh

3 Answers

2
votes
<?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   = "[email protected]";      // hotmail username
$mail->Password   = "useyourownpassword";           // hotmail password
$mail->SetFrom('[email protected]', 'First Last');
$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$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
votes

Talha answer is works for me. Try to comment $mail->IsSMTP(); and I also commented this part $mail->Port = 587;

0
votes

port 587 worked for me.

no need to run IsSMTP(). Comment it out as it will throw exceptions.

Dont forget to mark it as answer if it solves your problem :)