Unable to send mail using phpmailer - php

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.

Related

Unable to send email in php

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";
}
?>

Why does phpmailer take 15+ seconds to load?

I recently setup phpmailer as my SMTP sending script to use for a website I've been working on and it's been nothing but a headache. My setup is IIS, and I'm currently using office365 to send out emails(although I have tried gmail and the results were no faster).I would appreciate any and all advice on how to speed this up, or if there's a way to post to a php file from javascript or php and then just let it work I could do that too. I'm trying to stay away from cron jobs if possible.
my script:
function sendMail($code, $email) {
//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 = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "smtp.office365.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "support#[domain redacted].com";
//Password to use for SMTP authentication
$mail->Password = "[password redacted]";
//enable TLS
$mail->SMTPSecure = 'tls';
//Set who the message is to be sent from
$mail->setFrom('support#[domain redacted].com', '[redacted]');
//Set an alternative reply-to address
$mail->addReplyTo('support#[domain redacted].com', '[redacted]');
//Set who the message is to be sent to
$mail->addAddress($email);
//Set the subject line
$mail->Subject = 'This is a test email from EdTester Support';
//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 = "Hello there, This is your activation e-mail. Please go to: http://[domain redacted]/activate.php?code=". $code . "&email=" . $email;
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}

Class SMTP not found in phpmailer

I am having this problem for my php mailer function. I tried changing the required file but still its the same. Can anyone help me with this error.
This is my phpmailer code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "testnoreply#gmail.com";
$mail->Password = "test";
$mail->SetFrom("testnoreply#gmail.com");
$mail->Subject = "Membership expire notice";
$mail->Body = "Dear ICONIS member your membership is going to expire please renew it";
$mail->AddAddress($em);
if (!$mail->Send()) {
echo "Mailer Error: " .$mail->ErrorInfo;
} else {
echo "Mail has been sent";
}
Are you sure you have both the class.phpmailer.php AND the class.smtp.php in your folder?
Try including class.smtp.php before including class.phpmailer.php, as the following:
<?php
include 'class.smtp.php';
include 'class.phpmailer.php';
// your code goes here
Then tell if you got some error.
just download all files, extract the files into a folder and include PHPMailerAutoload.php from the folder.
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
/*======================================*/
/*======================================*/
require 'YOURFOLDERNAME/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';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP 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->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//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.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
try
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
instead of
require 'class.phpmailer.class.php';
$mail = new PHPMailer;

Email containing link of my domain is not sent

I am not able to send email from any hosting containing link of my website https://www.gogglegator.com. Emails are working fine without this link and all other links in email are working fine.
When I tried phpmailer, I got error Mailer Error: SMTP Error: data not accepted.
Is there anybody who faced such issue ever and have possible troubleshooting steps? I am searching from three days but no success.
Thanks in advance for your time.
<?php
require_once('class.phpmailer.php');
include("class.smtp.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 = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
//Set the hostname of the mail server
$mail->Host = "smtp.mail.yahoo.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
//Username to use for SMTP authentication
$mail->Username = "myaddress#yahoo.com";
//Password to use for SMTP authentication
$mail->Password = "mypass";
//Set who the message is to be sent from
$mail->setFrom('myaddress#yahoo.com', 'Test');
//Set an alternative reply-to address
$mail->addReplyTo('myaddress#yahoo.com', 'Test');
//Set who the message is to be sent to
$mail->addAddress('myaddress#yahoo.com');
//Set the subject line
$mail->Subject = "Test";
$mail->MsgHTML("https://www.gogglegator.com/");
if (!$mail->send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Thank you for contacting us.";
}
echo $message; ?>

PHPMailer error. Called Mail() without being connected

Hello guys i get this error,
Message could not be sent.Mailer Error: The following From address failed: hehe.gmail.com : Called Mail() without being connected.
<?php
require '/PHPMailer_5.2.4/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'hehe#gmail.com'; // SMTP username
$mail->Password = 'xxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted
$mail->Port = 465;
$mail->From = 'hehe#gmail.com';
$mail->FromName = 'Mailer';
$mail->AddAddress('hehe#gmail.com'); // Add a recipient
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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;
exit;
}
echo 'Message has been sent';
How can i fix it? Thanks!
Have you enabled access to less secure apps from your gmail id??
Please try this
<?php
include "PHPMailer_5.2.4/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "gmailusername#gmail.com";
$mail->Password = "**********";
$mail->SetFrom("anyemail#gmail.com");
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
?>
Please edit your gmail and password correctly.
You may see the demo on Click here
You need to use TLS, not SSL if you're using going to use gmail. Here is an example below
//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';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP 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->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
#ref: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
In my case it was because of virus guard..I disabled it and checked.It worked.

Categories