use phpmailer to send email from localhost to gmail: SMTP connect() failed - php

i want to send an email from localhost to gmail, but it says "SMTP connect() failed"
here is my code. I've also tried to change configuration php.ini and sendmail.ini but no luck.
Thank you.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'asd#gmail.com';
$mail->Password = 'password';
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('asd#gmail.com');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Email'
$mail->Body = "This is body";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

Try out the following :
Ensure that the PHPMailer library is included in the code.
Ensure that you are internet connection from localhost is not inhibited by any proxy.
Happy Coding :)

check the line $mail->Subject = 'Email' for a missing ;

Related

"SMTP connect() failed". When configuring it to send emails from remote Host

I am new at PHP. I am using PHPMailer to send emails but unable to do so. I tried configuring it for gmail and it worked But not working when tried to send msg from remote host and for the given domain.
Here is the code.
<?php
require("PHPMailerAutoload.php"); // path to the PHPMailerAutoload.php file.
$mail = new PHPMailer();
//$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "175.000.000.000"; //Sample Host
$mail->SMTPDebug = 1;
$mail->Port = "25"; // 8025, 587 and 25 can also be used. Use Port 465 for SSL.
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'ssl';
$mail->Username = "maaz#mcbah.com";
$mail->Password = "password";
$mail->From = "maaz#mcbah.com";
$mail->FromName = "Maaz Khan";
$mail->AddAddress("mcbah.all#mcbah.com", "all");
$mail->AddReplyTo("fundprices#mcbah.com", "Fund");
$mail->Subject = "Hi!";
$mail->Body = "Hi! How are you?";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';
exit;
} else {
echo 'Message has been sent.';
}
This code may help you
$mail->Host = "smtpout.secureserver.net";
Your host name should be like mydomain.com
$mail->Port = 465;

I am receiving this error from phpmailer Message could not be sent.Mailer Error: SMTP connect() failed?

I am working on phpmailer but I am receiving this error, I dont know what is wrong. I search and did many changes. but noting helped me.
I am testing both on live server and on localhost.
on both I am receiving the error
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Please have a look on my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
require("PHPMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'stockholm85#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'agohar1985#gmail.com';
$mail->FromName = 'Mailer';
//$mail->addAddress('zia_gt#yahoo.com', 'Zia Ullah'); // Add a recipient
$mail->addAddress('zia_gt#yahoo.com'); // Name is optional
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
I have folder on name PHPMailer and mail.php is the file.
I will appreciate your support because I am new to php
Thank you in advance
open your cmd and type the following to get php-mailer,
wget https://github.com/PHPMailer/PHPMailer/archive/master.zip
unzip the master. mention the file path clearly in your code.
Here
<?php
require 'PHPMailer-master/PHPMailerAutoload.php'; //file path
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = '_password_';
$mail->SMTPSecure = 'tls';
$mail->From = 'sender#example.com';
$mail->FromName = 'Your Name';
$mail->addAddress('recipient#example.com');
$mail->isHTML(true);
$mail->Subject = 'Test Mail Subject!';
$mail->Body = 'This is SMTP Email Test';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
test it

phpmailer Error after sending Email

Code: -
$mail = new PHPMailer();
$body="<b>This mail is sent using PHP Mailer</b>";#HTML tags can be included
$mail->IsSMTP();
$mail->SMTPAuth = true; #enable SMTP authentication
$mail->SMTPSecure = "tls"; #sets the prefix to the server
$mail->Host = "smtp.gmail.com"; #sets GMAIL as the SMTP server
$mail->Port = 587; #set the SMTP port
$mail->Username = "morelifelondon#gmail.com"; #your gmail username
$mail->Password = "***********"; #Your gmail password
$mail->From = $email;
$mail->FromName = $name ;
$mail->Subject = "Enquiry";
$mail->Body = $messages;
$mail->AddAddress("morelifelondon#gmail.com","contact");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Mail has Been Sent.';
}
}
I want that morelifelondon#gmail.com is replaced by abhishek#gmail.com. Is this possible ?
$mail->AddAddress("abhishek#gmail.com","contact");
or use
$mail->AddCC("abhishek#gmail.com","contact");

sending mail from xampp using phpmailer :Error occured

i am using xampp version 1.8.1 and i have also removed semicolon from php.ini file in extension=php_openssl.dll but still I am getting this error stream_socket_enable_crypto(): this stream does not support SSL/crypto and Message could not be sent.Mailer Error: SMTP connect() failed.
My php mailer code is:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'urusername#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->From = 'urusername#gmail.com';
$mail->FromName = 'Example';
$mail->addAddress('receivermail#gmail.com', 'xxxxx');
$mail->addReplyTo('receivermail#gmail.com', 'xxxxxx');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
kindly help me
Set the port to:
$mail->Port = 587;
Google SMTP uses port 587.

Sending mail with hotmail smtp in php

I am trying to send email through PHPMailer. And while sending it gives me error as
SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.
I really can't figure it out what's wrong. Is it the server configuration problem or code configuration problem. If anyone knows the answer please let me know.
Code below:
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(); //Initialize a new PHPMailer object;
$body = eregi_replace("[\]",'',$body); //Replace unwanted characters of the content
$mail->CharSet ="ISO-8859-1";
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->From = 'email';
$mail->FromName = 'name';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = 'smtp.live.com';
$mail->Port = '465';
$mail->Username = 'email ';
$mail->Password = 'password';
$mail->SetFrom('email', 'who');
$mail->AddReplyTo('email','who');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, '');
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Message sent successfully!";
}

Categories