0

I get two errors when trying to send mail:

  Warning: fsockopen() [function.fsockopen]: unable to connect to mail.localhost.com:25 (A
  connection attempt failed because the connected party did not properly respond after 
  a period of time, or established connection failed because connected host has failed 
  to respond. ) in C:\xampp\htdocs\euisample\class.smtp.php on line 122

 SMTP -> ERROR: Failed to connect to server: A connection attempt failed because 
 the  connected party did not properly respond after a period of time, or 
 established connection failed because connected host has failed to respond. 
 (10060)  Mailer
 Error: SMTP Error: Could not connect to SMTP host.

At the moment I am just using this test script

require("class.phpmailer.php");
$mail    = new PHPMailer();
$message = "Hello \n
      Thank you for registering with us. Here are your login details...\n
      User ID: $user_name
      Email: $usr_email \n
      Passwd: $data[pwd] \n";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host      = "mail.localhost.com"; // SMTP server
$mail->SMTPDebug = 2;
$mail->SMTPAuth  = true; // enable SMTP authentication
$mail->Host      = "mail.localhost.com"; // sets the SMTP server
$mail->Port      = 25; // set the SMTP port for the GMAIL server
$mail->Username  = "xxx@localhost.com"; // SMTP account username
$mail->Password  = "xxx"; // SMTP account password
$mail->AddReplyTo("xxx@localhost.com", "First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->MsgHTML($message);
$address = $usr_email;
$mail->AddAddress($address, "John Doe");

I need my system to send emails. If it is not possible with localhost, would it work with Gmail? Any other suggestions?

Pops
  • 30,199
  • 37
  • 136
  • 151
user1296762
  • 512
  • 2
  • 7
  • 21

1 Answers1

0

You need a real SMTP server in order to send emails like that. Since you obviously don't have an SMTP server installed on localhost your only option is to use a 3rd party server. You can use GMail for instance and here is how.

require_once('class.phpmailer.php');

try {
    $mail = new PHPMailer(true);
    $mail->IsSMTP(); // Using SMTP.
    $mail->CharSet = 'utf-8';
    $mail->SMTPDebug = 2; // Enables SMTP debug information - SHOULD NOT be active on production servers!
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true; // Enables SMTP authentication.
    $mail->Host = "smtp.gmail.com"; // SMTP server host.
    $mail->Port = 587; // Setting the SMTP port for the GMAIL server.
    $mail->Username = "EMAIL/USERNAME"; // SMTP account username (GMail email address).
    $mail->Password = "PASSWORD"; // SMTP account password.
    $mail->AddReplyTo('EMAIL', 'NAME'); // Use this to avoid emails being classified as spam - SHOULD match the GMail email!
    $mail->AddAddress('EMAIL', 'NAME'); // Recipient email / name.
    $mail->SetFrom('EMAIL', 'NAME'); // Sender - SHOULD match the GMail email.
    $mail->Subject = 'PHPMailer Test Subject via smtp, basic with authentication';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $mail->MsgHTML($message);
    $mail->Send();
} catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
}
  • I'm now getting these errors `Warning: fsockopen() [function.fsockopen]: unable to connect to tls://smtp.gmail.com:587 (Unable to find the socket transport "tls" - did you forget to enable it when you configured PHP?) in C:\xampp\htdocs\euisample\class.smtp.php on line 122 SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "tls" - did you forget to enable it when you configured PHP? ` – user1296762 Mar 30 '12 at 15:41
  • You need mod_ssl enabled in XAMPP in order to use TLS. –  Mar 30 '12 at 17:37
  • How do I do that? In httpd-ssl file? – user1296762 Mar 30 '12 at 17:49
  • http://stackoverflow.com/questions/2643462/setting-up-ssl-on-a-local-xampp-apache-server –  Mar 30 '12 at 18:05