I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
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 :)
I am obtaining two email id's from database by a query. Both stored in a single variable. I want to send email to these two addresses by PHPMailer keeping them in cc. Currently only one email is being selected and passed in cc. Can I know where am I going wrong. My code here,
$get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
$user_email_cc='';
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc=$get_data_cc['email'];
}
$mail = new PHPMailer();
$subject = "Mail";
$content ="XYZ";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com"; // Changed username and password from
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.xyz.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "XYZ");
$mail->AddAddress(abc#abc.com);
$mail->AddCC($user_email_cc);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo "Mail Sent";
use this code
$mail->AddCC('person1#domain.com', 'Person One');
$mail->AddCC('person2#domain.com', 'Person Two');
use $user_email_cc as array then it will store you both email a 0 and 1 position
$user_email_cc=array();
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc[] =$get_data_cc['email'];
}
New Code
$get_cc_email_id_sql=mysql_query("select * from tbl_name where column_name IN(13,5)");
$user_email_cc=array();
while ($get_data_cc=mysql_fetch_array($get_cc_email_id_sql))
{
$user_email_cc[] =$get_data_cc['email'];
}
$mail = new PHPMailer();
$subject = "Mail";
$content ="XYZ";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Port = 465;
$mail->Username = "xyz#xyz.com"; // Changed username and password from
$mail->Password = "xyz";
$mail->Host = "ssl://smtp.xyz.com";
$mail->Mailer = "smtp";
$mail->SetFrom("xyz#xyz.com", "XYZ");
foreach($user_email_cc as $email_cc){
$mail->AddCC($email_cc);
}
$mail->AddAddress(abc#abc.com);
$mail->Subject = $subject;
$mail->WordWrap = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);
if(!$mail->Send())
echo "Problem sending mail.";
else
echo "Mail Sent";
Can you call $mail->AddCC(...) multiple times, just like $mail->AddAddress(...)?
I'm using PHPMailer and am unable to send emails when configuring it to use my SendGrip credentials. Here is my sendEmail function:
public function sendEmail($to, $subject, $message)
{
$mail = new \PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.sendgrid.net';
$mail->SMTPAuth = true;
$mail->Username = 'apikey';
$mail->Password = 'removed';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('removed#email.com');
$mail->addAddress($to);
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $message;
if(!$mail->send()) {
throw new MailerException();
}
}
Is there anything obviously wrong with this function?
Thanks,
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
SMTP -> ERROR: DATA not accepted from server: 550 This message was classified as SPAM and may not be delivered
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.cc.com";
$mail->SMTPAuth = true;
$mail->Username = "info#cc.com";
$mail->Password = "cc";
$mail->From = "cc.com"." <info#cc.com>";
$mail->AddReplyTo("cc1#gmail.com");
$mail->FromName = "info#cc.com";
$mail->AddAddress($climail);
$mail->AddCC("cc1#gmail.com");
$mail->Sender="info#cc.com";
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->Subject = $sub;
echo $meegate;
$mail->Body=$meegate;
$mail->SMTPDebug = 1;
$mail->Send();
Error occurring while sending mail.. mail not sent..
You need to set SMTP host and also the port I guess which is 465:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "info#gmail.com";
$mail->Password = "cc";
$mail->From = "gmail.com"." <info#gmail.com>";
$mail->AddReplyTo("cc1#gmail.com");
$mail->FromName = "info#gmail.com";
$mail->AddAddress($climail);
$mail->AddCC("cc1#gmail.com");
$mail->Sender="info#gmail.com";
$mail->IsHTML(true);
$mail->WordWrap = 70;
$mail->Subject = $sub;
echo $meegate;
$mail->Body=$meegate;
$mail->SMTPDebug = 1;
$mail->Send();