I am trying to send an email with Gmail smtp, but I dont send any mail.
Neither do I receive any errors, I just get an empty page.
Here is my code, I hope you can help me with this.
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "MyEmail";
$mail->Password = "Mypassword";
$mail->setFrom('MyEmail');
$mail->addReplyTo('MyEmail');
$mail->addAddress('MyEmail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Please note that I entered the correct emails and passwords.
By default PHPMailer will send mail using php's mail() function (sendmail). You need to include smtp class for phpMailer to able to send mail using SMTP
require 'class.phpmailer.php';
require 'class.smtp.php';
you can get it from github
Related
I am using wordpress locally on my laptop and I want to use smtp to send emails to users from the website page (not from the wordpress dashboard). I wrote the code in the page template and it tells me that the email was sent successfully but when I check the inbox and the junk or spam for the recipient email I do not find any emails received from the sender.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'me#gmail.com';
$mail->Password = 'gmail_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('me#gmail.com', 'Atheer');
$mail->addReplyTo('me#gmail.com', 'Atheer');
$mail->addAddress('ahood.q26#gmail.com');
$mail->isHTML(true);
// Mail subject
$mail->Subject = 'Email from Localhost by CodexWorld';
$bodyContent = '<h1>Hello</h1>';
$bodyContent .= '<p>This is Atheer<b>CodexWorld</b></p>';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I am commenting the $mail->isSMTP(); becuase it was giving errors. I even tried the tls encryption and same problem it tells me send successfully while it is not sent.
I am using PHPMailer to send mails. I have deployed my website on Google Cloud VM Instance
Here is my mail sending code
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new \PHPMailer;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->isHTML(true);
$mail->addAddress($_POST['email']);
$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hello, Below are the details of contact ";
$mail->Body = "<table><tr><td>Name:</td><td>".$_POST['name']."</td></tr><tr><td>Email Address:</td><td>".$_POST['email']."</td></tr><tr><td>Subject :</td><td>".$_POST['subject']."</td></tr><tr><td> Message :</td>".$_POST['$POST['msg']."</td></tr></table>";
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
But the mail is not be able to send. Can anyone help me out? I can provide further code references if anyone needs
Try using SMTP to send email:
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
You have commented out isSMTP() so you are not using SMTP (so none of your SMTP settings will do anything), but the default mail() transport. This requires that you have a mail server installed in your server, which you apparently don’t.
To fix, either install a mail server (postfix is good) or enable SMTP, as chirag suggested.
All of this is covered in the PHPMailer troubleshooting guide.
I am trying following code to send mail
but it is showing
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
and gmail is informing me that Sign-in attempt prevented
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sss#gmail.com';
$mail->Password = '*******';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = 'abc#gmail.com';
$mail->FromName = 'asdf ';
$mail->addAddress('abc#gmail.com', 'sadf ');
$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";
?>
How to resolve above problem?
Try the following steps:
enable debug mode to catch possible errors
$mail->SMTPDebug = 1;
enable SMTP authentication
$mail->SMTPAuth = true;
also check for SSL support in php configuration file (php.ini)
extension=php_openssl.dll
You need set permission for send mail with gmail.
- Login Google Account
- Go Privacy Page
- Allow third party apps
After try this code:
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$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.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. Do the following steps.
I hope this helps.
Check if IMAP is enabled
Check here and enable less secure apps
Display Unlock Captcha
change the following in your code
isSMTP() to IsSMTP() , addAddress() to AddAddress() & isHTML() to IsHTML().
and yes check the ports also. sometoimes port also off which do not let connection to be established.
Hope it will work!
I think you have to enable POP and IMAP in you gamil. Try this
Sign in to Gmail
Click the gear in the top right.
Select Settings.
Click Forwarding and POP/IMAP.
Select Enable IMAP.
Click Save Changes.
<?
$account="email_address#gmail.com";
$password="accountpassword";
$to="mail#subinsb.com";
$from="email_address#gmail.com";
$from_name="Name";
$msg="<strong>This is a bold text.</strong>"; // HTML message
$subject="Database Backup";
/*End Config*/
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
?>
can someone help me to find what is wrong with the following code that i used to send mail in php.
error_reporting(E_STRICT);
date_default_timezone_set('asia/kolkata');
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "manikandan#gmail.com";
$mail->Password = "mypassword";
$mail->From = ("manikandan#gmail.com");
$mail->SetFrom("manikandan");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("peter1991#gmail.com");
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
i am getting the following error:- 2015-01-05 04:32:10 Invalid address: manikandan
i am using php 5.2.2 ,Apache 2.0 Handler in windows.
setFrom expects an email address as the first parameter. Try:
$mail->setFrom('manikandan#gmail.com');
If you also want to set a proper name, use the second parameter
$mail->setFrom('manikandan#gmail.com', 'John Smith');
http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_setFrom
Also, I think you should be using the autoloader instead of including the PHPMailer class directly.
try this:
$mail->setFrom('manikandan#gmail.com', 'manikandan'); //setFrom expect email
Full code:
<?php require 'PHPmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "manikandan#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('manikandan#gmail.com', 'manikandan');
//Set an alternative reply-to address
$mail->addReplyTo('peter1991#gmail.com', 'peter');
//Set who the message is to be sent to
$mail->addAddress('peter1991#gmail.com', 'peter1991');
//Set the subject line
$mail->Subject = 'Test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = "Hi ,! Welcome to PHP mail function. \n\n .";
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.gif');
$mail->SMTPAuth = true;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
To resolved this just turn on access for less secure app in your google account. Hope this helps.
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 = "(username#hotmail.com)"; // your hotmail user name
$mail->Password = "password";
$mail->AddAddress"(username#hotmail.com)"; // 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? :(
<?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 = "useyourownemail#hotmail.com"; // hotmail username
$mail->Password = "useyourownpassword"; // hotmail password
$mail->SetFrom('yourname#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","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 = "anemail#domain.com";
$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!";
}
?>
Talha answer is works for me. Try to comment $mail->IsSMTP(); and I also commented this part $mail->Port = 587;
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 :)