Just finished setting up PHPMailer to send the PDF that is created from my html form (using FPDF, the pdf file is created without a problem). It says sent successfully but im not receiving anything?
I have checked other peoples code and it looks just like mine. Is there anything im doing wrong with the PHPmailer code at the bottom?
my host,username and password are all correct 100% as far as I know we dont use TLS or SSL. Maybe that has something to do with this?
My code:
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->Host ="*****";
$Mail->SMTPAuth = true; // enable SMTP authentication;
$mail->Username = "*****";
$mail->Password = "****";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->From = "******";
$mail->FromName = "Jurgen Hof";
$mail->addAddress("testingaccount23#gmail.com", "Tester");
$mail->isHTML(true);
$mail->Subject = 'Test Leave Application';
$mail->Body = 'Test.';
$mail->AddAttachment("/var/www/html/leaveform/AlpineLeaveApplication.pdf");
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Email Sent Successfully!';
?>
You're setting lots of SMTP properties, but not actually asking it to send via SMTP! Add this:
$mail->isSMTP();
Your browser will then explode with debug output, so I suggest you turn down SMTPDebug = 2.
Related
When I trying to send a email using PHP SMTP email server, following error has occurred.
SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
Following is my code that I have used.
function supervisorMail(){
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "***#gmail.com";
$mail->Password = "****";
$mail->SetFrom("***#gmail.com", "Employee Leave Management System");
$userID=$_SESSION['userID'];
$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'");
$select_sql = mysql_fetch_array($select_query);
$name=$select_sql['manager_name'];
var_dump($name);
$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'");
$select_sql1 = mysql_fetch_array($select_query1);
$email=$select_sql1['email'];
var_dump($email);
$mail->Subject = "subject ";
$mail->Body = "something.";
$mail_to = $email;
$mail->AddAddress($mail_to);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
How can I fixed this error.
The error message is very clear "Could not authenticate".
I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.
I suppose that in your question the fields username and password look like
$mail->Username = "#gmail.com";
$mail->Password = "";
just because you, obviously, don't want to share them, I would suggest to present them like this in the question
$mail->Username = "********#gmail.com";
$mail->Password = "********";
So if you are using the correct username and password there are other two things to check
Maybe your setting "Access for less secure apps" is turned off.
After you login from the web you can turn it on from this page
https://www.google.com/settings/u/0/security/lesssecureapps
If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.
Follow this link for a step by step guide on how to do it
http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm
Please unable your less secure app
and then
Try to comment this line
//$mail->IsSMTP();
I hope it will work!
You may try to use google app password for authentication:
https://support.google.com/accounts/answer/185833?hl=en
2-Step-Verification & 2. App Passwords
As mentioned in comment, you need to enter valid gmail id and password.
Please refer below demo code.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // 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 = '*****001#gmail.com'; // SMTP username
$mail->Password = 'ma****02'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('ravi******#gmail.com', 'Mailer'); // Add set from id
$mail->addAddress('er.ravihirani#gmail.com', 'Ravi Hirani'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$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 ran into this error message and wanted to post an answer in case it can help someone else. I have phpmailer installed with composer and am running php 7. The implementation of the phpmailer script that threw the error is in a php object method. The password variable, set in a config file elsewhere, needed to be declared as a global in the object context otherwise it did not contain the actual password and thus caused the authentication error..
global $password; // <- this added to make script work
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
// Set mailer to use SMTP
$mail->Host = '*******.com';
$mail->SMTPAuth = true;
$mail->Username = 'no-reply#*******.com';
$mail->Password = $password;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('no-reply#*******.com', '******* - DO NOT REPLY');
//$mail->isHTML(true);
$mail->AddAddress($user->email);
$mail->Subject = "*******";
$mail->Body = "*******";
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
In my case it was because I didn't turn on the Less secure app access from here: https://myaccount.google.com/lesssecureapps
Hello guys I get this error:
The following From address failed: username_from#gmail.com : Called Mail() without being connected
<?php
require("PHPMailer/class.phpmailer.php");
$msg = 'hello world!';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'username#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->From = 'username_from#gmail.com';
$mail->FromName = 'My name';
$mail->AddAddress('other_user#gmail.com', '');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = 'hello';
$mail->Body = $msg;
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
?>
How can I fix it? Thanks! I send email from localhost running XAMPP in PHP using Gmail mail server.
Possible reason for error:
you have to edit your gmail account permissions. Trying enabling the 'Access for less secure apps' Security setting.
may be,you're using an old version of PHPMailer - update it.
Don't use ssl/465 for gmail; use tls/587. Take a look at the gmail example code provided with PHPMailer.
You don't need to call singleTo if you're only adding one address anyway.
Try and set Host to smtp.gmail.com
you can add $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only so you can see some debug statements?
I am trying to configure PHPMailer in order to have it working ona Windows IIS server using php scripts.
The application I have is written in PHP so we expected we could easily use PHPMailer to send emails.
Well, that doesn't work.
We have tried configuring it as SMTP, but we still get error in configuration.
Here it is our script:
date_default_timezone_set('Etc/UTC');
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 1;
//$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "account#gmail.com";
$mail->Password = "gmailPWD";
$mail->setFrom('info#website.com', 'The Website');
$mail->addAddress($to, '');
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$errors = "Errori Mailer: " . $mail->ErrorInfo;
} else {
$errors = "<h5>Sent!</h5>";
}
Any clue? How can that be used on windows server?
Thank you
EDIT
So, it was a problem of Gmail and its security settings. I followed this post: https://stackoverflow.com/a/25175234/1873501 and everything went smooth!
I just wanted to share it.
You've downloaded and extracted the class I am assuming. Maybe you're not autoloading? Make sure you include:
require '/path/to/PHPMailerAutoload.php';
It should be fine on IIS/Windows Server. See some of the examples here in the documentation (and in the examples folder): https://github.com/PHPMailer/PHPMailer
Also, if you are getting any errors make sure you list them so we know how to help you further.
function sendMail($request) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourmail#gmail.com;
$mail->Password = 'yourgamilPass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('yourgmail#gmail.com', 'Title');
$mail->addAddress(emailsendingto#gmail.com);
$mail->addReplyTo('yourgmail#gmail.com');
$mail->isHTML(true);
$mail->Subject = '$the subject of the e-mail';
$mail->Body = 'The body of the email';
$mail->AltBody = 'Alternative'; // this is mostly sent to your own mail
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message 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.
I am trying to let users fill out a contact form, which will then be sent to my email. But its not working for some reason. I just get a blank page with no error message or any text and email is also not sent.
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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;
exit;
}
echo 'Message has been sent';
You need to call:
$mail = new PHPMailer(true); // with true in the parenthesis
From the documentation:
The true param means it will throw exceptions on errors, which we need
to catch.
Its working now, i didnt include the 'class.smtp.php' file. The working code is below:
if (isset($_POST['submit']))
{
include_once('class.phpmailer.php');
require_once('class.smtp.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$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;
exit;
}
echo 'Message has been sent';
I had the same problem with no error message even with SMTPDebug enabled. After searching around for working examples I noticed that I didn't include the SMTP Secure value. Try adding this line:
$mail->SMTPSecure = 'ssl'; //secure transfer enabled
Work like a charm now.
I had a similar problem. In reference to #Syclone's answer. I was using the default "tls".
$mail->SMTPSecure = 'tls';
After I changed it to
$mail->SMTPSecure = 'ssl';
It worked ! My mailserver was only accepting connections over SSL.
What worked for me was setting From as Username and FromName as $_POST['email']
Hope this helps
PHPMailer use exception.
Try this
try {
include_once('class.phpmailer.php');
$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);
$subject = "Contact Form from DigitDevs Website";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info#example.com"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress('info#example.com', 'Information');
$mail->AddReplyTo($email, 'Wale');
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Send();
exit;
} catch (phpmailerException $e) {
echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage();
}
I was trying to load an HTML file to send, which did not belong to the www-data group on my Ubuntu server.
chown -R www-data *
chgrp -R www-data *
Problem solved!
I was debating whether to write my own handler or crow-bar PHPMailer into my existing class structure. In the event it was very easy because of the versatility of the spl_autoload_register function which is used within the PHPMailer system as well as my existing class structure.
I simply created a basic class Email in my existing class structure as follows
<?php
/**
* Provides link to PHPMailer
*
* #author Mike Bruce
*/
class Email {
public $_mailer; // Define additional class variables as required by your application
public function __construct()
{
require_once "PHPMail/PHPMailerAutoload.php" ;
$this->_mailer = new PHPMailer() ;
$this->_mailer->isHTML(true);
return $this;
}
}
?>
From a calling Object class the code would be:
$email = new Email;
$email->_mailer->functionCalls();
// continue with more function calls as required
Works a treat and has saved me re-inventing the wheel.
Try this ssl settings:
$mail->SMTPSecure = 'tls'; //tls or ssl
$mail->SMTPOptions = array('ssl' => array('verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true));