hi, i'm trying to have a mail function using gmail server but i keep getting the error :
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

this is the code i got

<?php
include("class.phpmailer.php");
include("class.smtp.php");

if(isset($_POST['send'])){
    $body=$_POST[email];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth   = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // use ssl
$mail->Host = "ssl://smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port  = 465; // SMTP port used by GMAIL server
$mail->Username   = "myaccount@gmail.com"; // GMAIL username
$mail->Password   = "mypassword"; // GMAIL password
$mail->AddReplyTo("account@domain.com","FirstName LastName"); // Reply email address
$mail->From = "myaccount@gmail.com";
$mail->FromName = "myname"; // Name to appear once the email is sent
$mail->Subject = "testing 123"; // Email's subject
//$mail->Body = "Hello World,<br />This is the HTML BODY<br />"; //HTML Body
$mail->AltBody = "This is a test email"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body); // [optional] Send body email as HTML
$mail->AddAddress("account@domain.com");  // email address of recipient
//$mail->AddAttachment("files/files.zip"); // [optional] attachment
$mail->IsHTML(true); // [optional] send as HTML
if(!$mail->Send())
echo "Mailer Error: " . $mail->ErrorInfo;
else
echo "Message sent!";
}
?>

Have you tried using any other SMTP server to send out mail? I know it is like taking 2 steps forward and 3 back but it may help you to determine whether it is the Host or the Port that is the problem (perhaps try both a non-encrypted server and an encrypted one and see the result??)
I dont know if this will be any help but good luck anyways.
ParkeyParker
;-D

i dont know what to do. i have a similar problem just like your's.

I assume your code is perfect, so the only problme may exist port 465 not open on your server.

Ask your host about this port status...

Hi.

Try removing the "ssl://" from the host URI. The PHPMailer code should add that for you in the background.

Hi,

After removing the "ssl://" from the host URL but I still keep getting the error.

why not use other smtp servers. try hotPop
or try to search google.

This code works for me.

<?php
// Fetch the PHPMailer classes
include("phpMailer/class.phpmailer.php");
include("phpMailer/class.smtp.php");

// Your email info
$yourMail = '<gmail_user>@gmail.com';
$yourPass = '<gmail_password>';
$yourName = '<your_name>';

// Recipent list
$recipients = array(
    'recipient1@example.com',
    'recipient2@example.com'
);

// Mail contents
$mailSubject = 'Testing GMail SMTP';
$mailHtmlBody =<<<HTML
<h1>Testing GMail SMTP</h1>
<p>This is just to test if sending via GMail SMTP works.</p>
HTML;

// Create a new SSL PHPMailer instance
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";

// Set the GMail SMTP settings
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = $yourMail;
$mail->Password = $yourPass;

// Add all the recipients
foreach($recipients as $_recipient) {
    $mail->AddAddress($_recipient);
}

// Create the email
$mail->AddReplyTo($yourMail,$yourName);
$mail->From = $yourMail;
$mail->FromName = $yourName;
$mail->Subject = $mailSubject;
$mail->Body = $mailHtmlBody;

$mail->IsHTML(true); // Or FALSE, if it is not a HTML email
$mail->WordWrap = 50;

// Send the mail
if($mail->Send()) {
    echo "Message sent!";
}
else {
    echo "Failed to send message: " . $mail->ErrorInfo;
}
?>

Try that. See if it works for you.

hey got other codes?

the codes work just right.

I try this but it raised an error: Invalid address: @gmail.comSMTP Error: Could not authenticate. Failed to send message: SMTP Error: Could not authenticate.

Member Avatar for rajarajan2017

Hi littlebear330,

The code you written is correct and you must try this from server, and not from the localhost, from localhost it will not work.

Please copy all your files to the hosting server, and run the php file it will work.

When you have a hosting server, they already configured smtp for you. From there only your code will execute perfectly, it is not possible to check the mail send from local machine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.