0

I am trying to send mails from my gmail through PHPMailer, but i get the following error:

SMTP Error: Could not connect to SMTP host.

I downloaded the phpmailer class from: https://github.com/Synchro/PHPMailer.

I have tried for more than thirty hours, so i have tried the most combinations of port 25, 465, 587. I have even tried weird stuff like removing @gmail.com from my SMTP username, so please help me.

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require '../class.phpmailer.php';

try {
    $mail = new PHPMailer(true); //New instance, with exceptions enabled

    $body             = file_get_contents('contents.html');
    $body             = preg_replace('/\\\\/','', $body); //Strip backslashes

    $mail->IsSMTP();                           // tell the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;                    // set the SMTP server port
    $mail->Host       = "smtp.gmail.com"; // SMTP server
    $mail->Username   = "mymail@gmail.com";     // SMTP server username
    $mail->Password   = "mypass";            // SMTP server password

//  $mail->IsSendmail();  // tell the class to use Sendmail

    $mail->AddReplyTo("name@domain.com","First Last");

    $mail->From       = "name@domain.com";
    $mail->FromName   = "First Last";

    $to = "reciever@gmail.com";

    $mail->AddAddress($to);

    $mail->Subject  = "First PHPMailer Message";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->WordWrap   = 80; // set word wrap

    $mail->MsgHTML($body);

    $mail->IsHTML(true); // send as HTML

    $mail->Send();
    echo 'Message has been sent.';
} catch (phpmailerException $e) {
    echo $e->errorMessage();
}
?>

2 Answers2

0

I tried emailing through php mailer from my webpage and I got the same issue .. then I uploaded my code on a server and then it worked .. I think PHPMailer works from a webserver, It will not work from your localhost machine ... Try to upload your code on a webserver and then try ... if your code is uploaded on a server then use this ....

<?php

       require_once "../class.phpmailer.php";

        $from = "<from.gmail.com>";
        $to = "<to.gmail.com>";
        $subject = "Hi!";
        $body = "Hi,hows goin??";

        $host = "ssl://smtp.gmail.com";
        $port = "465";
        $username = "myaccount@gmail.com";  //<> give errors
        $password = "password";

        $headers = array ('From' => $from,
          'To' => $to,
          'Subject' => $subject);
        $smtp = Mail::factory('smtp',
          array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
          echo("<p>" . $mail->getMessage() . "</p>");
         } else {
          echo("<p>Message successfully sent!</p>");
         }

    ?> 
Tarun
  • 195
  • 10
0

The mistake I think you are making is that you are requiring the wrong class, your require should load the require_once '../PHPMailerAutoload.php'; The PHPMailer class relies on other sub classes to handle other aspects of sending mail. The class you required is more of a wrapper class, this should fix many of the errors or at least give you better error messages.

Joshua Klein
  • 189
  • 2
  • 13