I am trying to build a mail function with php, but it's justing showing "Invalid address:" after execute. Pleas help.
Below is my code:
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->From = "myemail#gmail.com";
$mail->FromName = "Web Site";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress('myfriend#gmail.com');
$mail->AddReplyTo('myfriend#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Try This
Please write the below line.
$mail->CharSet = "UTF-8"; // To support special characters in SMTP mail
//$mail->SMTPDebug = 2; Comment this line of code
I hope this will help you. Good Luck
Related
I have a really simple PHP code for sending emails. The page is loading without any errors but it doesn't seem to do anything.
also, I am new to PHP and I don't really know how to debug this stuff.
I'll really appreciate a little help (: thank you!
<?php
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot#gmail.com';
$mail->Password = 'lago9876543s';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123#gmail.com');
$mail->send();
?>
change your code like this and see if it will show you any errors
$mail = new PHPMailer();
try {
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot#gmail.com';
$mail->Password = 'lagofbot258258#258258';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123#gmail.com');
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
Try with this
require __DIR__.'../phpmailer/src/PHPMailer.php'; //configure according your files
require __DIR__."./phpmailer/src/Exception.php";
require __DIR__."../phpmailer/src/SMTP.php";
$mail = new \PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->isSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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->Username = ""; //GMAIL ACCOUNT EMAIL ID
$mail->Password = ""; // GMAIL ACCOUNT PASSWORD
$mail->SetFrom("",''); // FROM THIS MAIL ID & SET AS DEFINE IN SECOND PARAMETER
$mail->addAddress($email); // `TO` FIELD IN MAIL
$mail->IsHTML(true);
if (!$mail->Send()) {
return true;
} else {
return false;
}
And don't forget to enable less secure apps if you use gmail.
Hope this helps :)
When I try to run a simple PHPMailer test script it is redirecting me to GitHub site.
My script is given below. Can someone please help?
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/PHPMailer.php';
$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 = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587 or 465
$mail->IsHTML(true);
$mail->Username = "myaccountname";
$mail->Password = "mypassword";
$mail->setFrom('fromemailid', 'from name');
$mail->Subject = 'Test Mail';
$mail->Body = 'Test mail body';
$mail->AddAddress("someone#gmail.com");
if (!$mail->send())
{echo "Mailer Error: " . $mail->ErrorInfo;
return false;
} else {
return true;
}
?>
Which folder you exactly adding your code. As see your code
I am trying to send SMTP secured mail with attachment using PHPMailer.
I made this function with PHPMailer library
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'tester#mydomain.com';
$mail->Password = '**************';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Now if I comment the line $mail->isSMTP(); it's working fine . but I think it's not SMTP secured. Else I am getting this message: "Mail error: SMTP connect() failed. "
I searched for this problem but didn't get the proper answer to what I am looking for. Please help me.
I am working in a dev server with HTACCESS protection
Well I have found the details to fix the issue here https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting .
I have just Turned on less secure apps https://www.google.com/settings/security/lesssecureapps from my gmail account.
This takes few minutes or an hour to active.
My current code :
public function sendCsv($subject,$body,$path,$mail_to,$from_name,$from_mail,$replyto){
require getcwd() .'/lib/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
//$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'mymail#gmail.com';
$mail->Password = 'mypass';
$mail->setFrom($from_mail, $from_name);
$mail->addAddress($mail_to);
$mail->addReplyTo($replyto, 'no reply');
$mail->addAttachment($path);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
} else {
$sucess = 'Mail sent!';
echo $sucess;
}
}
Else you have to Setup a app from console.developers.google.com
Follow this guide : https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
I am working on a project and in my registration form when the user registers I want to E-mail them an activation link. the registration form works, but I am having issues with the E-Mail server. This is my php code using Phpmailer that I have but it is not working and I do not know why !
require 'include/class.phpmailer.php';
require 'include/PHPMailerAutoload.php';
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = jalilmotaz;
$mail->Password = *****;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Any sugesstions on how to work this out by gmail or some other way?
Multiple mistakes in your code.
1). SSL is deprecated. Use tls.
2). Put your full gmail email and password, and within double quoted ".
<?php
require 'include/class.phpmailer.php';
require 'include/PHPMailerAutoload.php';
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$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 = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = "youremail#gmail.com";
$mail->Password = "passwordhere";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
?>
I used phpgmailer to send emails and it was working smoothly. Today I tested my project and it's not working now.
<?php
require_once('class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'username#gmail.com';
$mail->Password = '********';
$mail->From = 'username#gmail.com';
$mail->FromName = "<blah>";
$mail->Subject = 'something';
$mail->AddAddress('xyz#gmail.com');
$mail->Body = "Hello Sir"."\n"."
Your Password is : ."."";
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Send();
if(!$mail->Send())
{
echo 'Message could not be sent.' ;
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
I suggest you download PHPMailer and try this code:
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "from#gmail.com";
$mail->Password = "****";
$mail->FromName = "Sender name";
$mail->Subject = "test";
$mail->Body = "Test body";
$mail->AddAddress('sender#mail.com');
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message has been sent";
}
#goose is right, remove the first $mail->Send() and leave the one in the if statement. If the From address is the same as the username email, then you don't need that either as it will take it from your gmail account.
Try that and see if it works.
EDIT: try adding the following;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$mail->Username = 'email#gmail.com';
$mail->Password = 'password';
Try that and if there are errors, then it should give you more detail.
EDIT2: If that doesn't work then I suggest trying PHPMailer instead of PHPGmailer - and follow the tutorial here: http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
Please, make sure:
$mail->Username is equal to $mail->From, and is correct
$mail->Password is correct
$mail->FromName does not contain "<" and ">" characters; just try $mail->FromName = 'Test';