i created this PHP Class for sending emails
class Mailbox{
private $mail;
public function initialize(){
$this->mail = new PHPMailer;
$this->mail->Mailer = "smtp.gmail.com";
$this->mail->Host = "mail.****.***.*g";
$this->mail->Port = 587;
$this->mail->IsSMTP(); // telling the class to use SMTP
$this->mail->SMTPAuth = true; // turn on SMTP authentication
$this->mail->SMTPSecure = "ssl";
$this->mail->Username = '****#******.com';
$this->mail->Password = '67******!'; // SMTP password
$this->mail->SetFrom('****#******.com', 'Federal Admin');
}
public function addSubject($sub='***** testing mail...'){
$this->mail->Subject = $sub;
$this->mail->AltBody = "...just a sample message";
}
public function addMessage($msg){
$this->mail->MsgHTML($msg);
}
public function addAddress($addr){
foreach($addr as $ad){
$this->mail->AddAddress($ad[0], $ad[1]);
}
}
public function send(){
if(!$this->mail->Send()) {
return "Mailer Error: mail not sent.." . $this->mail->ErrorInfo;
} else {
return true;
}
}
}
and this is my implementation in my code...
$ar = array(
['****r#k**g.gov', 'Recruit'],
['an***i#aol.com','Officer'],
['***iii#gmail.com','Secretary']
);
$message = $msg->render(); //renders a htmlview
$m = new Mailbox;
$m->initialize();
$m->addSubject('Reports from my store Armory');
$m->addMessage($message);
$m->addAddress($ar);
if($m->send()){
echo 'messsage sent successfully';
}else{
echo 'error sending message';
}
it was working three days ago, tuesday precisely, the image below is the output from my aol mail, but just of recent, since wednesday, it hasnt been functioning properly. i didn't touch or change anything, i have been lost and going round in circles. please i need help. Does anyone know if GMail is Having any issues or is anyone experiencing the same issue as me..
Please i need help if anyone has solved this issue, my guess is that Gmail is having issues, i have changed the allow unsecured apps settings and yet i still keep getting these errors.
turn On Allow less secure apps.
and change:
$mail->SMTPSecure = 'tsl';// Enable TLS encryption,sslalso accepted
$mail->Port = 587;
check this!
http://www.tutsplanet.com/send-email-using-smtp-server-phpmailer-997/
Related
I am using phpMailer to send emails using PHP.
The mail is being sent but it is not received in inbox/spam or anything.
It is surprising that it was working until few days ago.
I have tested it and almost 500-600 emails were sent and received.
But suddenly it stopped "working".
Here's my Php script:
public static function mailTo($recipients)
{
$f3 = \Base::instance();
$edit = $f3->get('editTrue');
$user = AclHelper::getCurrentUser();
$template= new \Template;
if(isset($edit))
{
$mailBody = $template->render('leave/requestEdit.html');
}
else
{
$mailBody= $template->render('leave/emailTemp.html');
}
// When true, PHPMailer returns exceptions
$mail = new PHPMailer(true);
try {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isHTML(true);
$mail->addAddress($user['email']);
$mail->addAddress("malakar.rakesh1993#gmail.com");
// foreach($recipients as $recipient){
// $mail->addCC($recipient);
// }
$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 = "malakar.rakesh1993#gmail.com";
$mail->Password = "abcd";
// $mail->Host = $f3->get('GBD.smtp.host'); // Specify main and backup SMTP servers
$mail->setFrom($user['email']);
$userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
$mail->FromName = $userFullName;
$mail->Body = $f3->get('message');
$mail->Body .="<br>". $mailBody;
if(isset($edit))
{
$mail->AltBody = '';
}
else
{
$mail->AltBody = 'Hello Team,<br>I would like to request leave for the leave dates specified as follows.
Application Date:' . $f3->get('issuedDate') . '<br>Leave requested from:' . $f3->get('leaveFrom') . '<br>Leave requested to:' . $f3->get('leaveTo') . '<br>Leave Description:' . $f3->get('leaveDescription') . 'Leave Type:' . $f3->get('leaveType').'<br><br>Hoping for a positive response.<br><br> Thank you.';
}
$mail->Subject = 'Updates on leave date applied';
$mailStatus = (boolean)$mail->send();
if ($mailStatus === true) {
return $mail;
}
} catch (phpmailerException $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
} catch (Exception $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
}
I received a junk email though [only one] that says :
This sender failed our fraud detection checks and may not be who they appear to be. Learn about spoofing
I cannot figure out what's going wrong.
It is working until previously. And I have tons of emails in my inbox.
Could it be that there's some limit of sending emails?? Or could it be that some one reported it as spam or spoofing??
Any help is very much appreciated. Thanks.
Because you're using SMTPSecure = 'ssl' you won't get any debug output with SMTPDebug = 2 because that only shows SMTP-level output; You need SMTPDebug = 3 to show connection-level problems. This is probably caused by out of date CA certificates in your PHP config. There have been lots of reports of this because gmail changed theirs recently (why your script stopped working). It's covered in the troubleshooting guide.
Also, why are you putting HTML tags in your plan-text AltBody? They won't work in there.
I've been using phpMailer to send emails until few days ago, and it stopped working surprisingly.
I'm using GMAIL SMTP server as a host.
Here's what the Exception and debugging message shows in console :
2017-12-19 05:39:02 Connection: opening to ssl://smtp.gmail.com:465, t=10, opt=array (
)
2017-12-19 05:39:02 SMTP ERROR: Failed to connect to server: (0)
2017-12-19 05:39:02 SMTP connect() failed.
And here's my mail functionality:
public static function mailTo($recipients)
{
$f3 = \Base::instance();
$edit = $f3->get('editTrue');
$user = AclHelper::getCurrentUser();
$template= new \Template;
if(isset($edit))
{
$mailBody = $template->render('leave/requestEdit.html');
}
else
{
$mailBody= $template->render('leave/emailTemp.html');
}
// When true, PHPMailer returns exceptions
$mail = new PHPMailer(true);
try {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isHTML(true);
$mail->addAddress($user['email']);
$mail->addAddress("malakar.rakesh1993#gmail.com");
// foreach($recipients as $recipient){
// $mail->addCC($recipient);
// }
$mail->SMTPDebug = 4;
$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 = "malakar.rakesh1993#gmail.com";
$mail->Password = "abc123";
// $mail->Host = $f3->get('GBD.smtp.host'); // Specify main and backup SMTP servers
$mail->setFrom($user['email']);
$userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
$mail->FromName = $userFullName;
$mail->Body = $f3->get('message');
$mail->Body .="<br>". $mailBody;
if(isset($edit))
{
$mail->AltBody = '';
}
else
{
$mail->AltBody = 'Hello.. Not working';
}
$mail->Subject = 'Updates on leave date applied';
$mailStatus = (boolean)$mail->send();
if ($mailStatus === true)
{
return $mail;
}
}
catch (phpmailerException $e)
{
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
catch (Exception $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
}
I also tried using tls as encryption protocol and 587 port, but gives 500 internal server error.
The same code is running in development version in server, but unfortunately not in my localhost.
I was suggested of out-dated CA certificate in my system, and I tried renewing the up-to-date CA certificate following the link Trobuleshooting, but it's not working either.
Stupid maybe but I tried pinging to smtp.gmail.com [without ssl and port] in my command prompt and I'm receiving feedback.
Could it be that Google limits email sending? But I can still access my account.
I have followed almost every articles and solutions available in the Internet.
Please help me through this...this is driving me crazy.
Any help is very much appreciated. Thanks...
I've been running PHPMailer for a year now on a php server. Everything was fine until 3 days ago when I started getting the following error:
SMTP Error: Could not authenticate.
Allow less secure apps is ON
Here is the code:
function SendEmail($to,$cc,$bcc,$subject,$body) {
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = 1;
try {
$addresses = explode(',', $to);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
if($cc!=''){
$mail->addCustomHeader("CC: " . $cc);
}
if($bcc!=''){
$mail->addCustomHeader("BCC: " . $bcc);
}
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on 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;
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "myemailpass"; // SMTP password
$webmaster_email = "myemail#gmail.com"; //Reply to this email ID
$name=$email;
$mail->From = $webmaster_email;
$mail->FromName = "Service";
//$mail->AddReplyTo($webmaster_email, "DiFractal Customer Service");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
} catch (phpmailerException $e) {
$myfile = fopen("debug_email.txt", "w");
fwrite($myfile,$e->errorMessage() . "\n" . $mail->ErrorInfo);
fclose($myfile);//Pretty error messages from PHPMailer
} catch (Exception $e) {
$myfile = fopen("debug_email_stp.txt", "w");
fwrite($myfile,$e->getMessage());
fclose($myfile);//Pretty error messages from PHPMailer
}
}
Note I just updated PHPMailer to the latest version to try to remedy the problem but nothing has changed! The old version 5.2.2 was still having the same problem!
EDIT: I just had one successful email go through to google and sent properly. Which now makes me question if it's lag issue or something of that sort. Does anyone know how phpmailer functions under high loads or if high loads can cause the above error?
Try going to:
myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON".
Alternative:
Try changing SMTP Port to: 465 (gmail also).
I was having similar issues and needed to set the from address
$mail->setFrom('myemail#gmail.com', 'Webmaster');
Make sure you check google's usage limits! PHPMailer will not tell you particulars it will just give you the Could not authenticate error but the reason why can be because of your limits.
# https://support.google.com/a/answer/166852?hl=en
Upgraded to a new account with google business and switched to that account. Issue resolved.
I know this kind of question asked so many times in stackoverflow, but I looked at most of them and the problem still remains.
I'm using phpMailer to send mail from my site using a gmail account.
When I was using this in localhost, everything was fine and mails was sending correctly,
But as long as I uploaded my site in remote server, problem revealed itself.
There's connection timeout(110) error when I'm using port 465 and smtp.gmail.com, when I'm trying to use port 587 and tls there's error too.
Would you please let me know what's wrong?
The host php version is 5.4.
this is my code:
require_once ("../PHPMailer/class.phpmailer.php");
class mailSender
{
protected $address;
protected $username;
protected $password;
protected $cc;
protected $bcc;
protected $body;
protected $mailer;
public function __construct( $host, $port, $usn, $psw, $isSMTP = true )
{
$this->mailer = new PHPMailer(true);
//$this->mailer->Host = "mail.yourdomain.com"; // SMTP server
$this->mailer->Host = $host; //"smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->mailer->Port = $port; //465; // set the SMTP port for the GMAIL server
if( $isSMTP == true )
{
$this->mailer->IsSMTP();
//$this->mailer->SMTPDebug = 1; // enables SMTP debug information (for testing)
$this->mailer->SMTPAuth = true; // enable SMTP authentication
$this->mailer->SMTPSecure = "ssl"; // sets the prefix to the servier
}
$this->mailer->Username = $usn; //"yourusername#gmail.com"; // GMAIL username
$this->mailer->Password = $psw; //"yourpassword"; // GMAIL password
$this->mailer->CharSet = 'UTF-8';
$this->mailer->IsHTML(true);
//set High priority to prevent going to SPAM folder
$this->mailer->Priority = 1;
$this->mailer->AddCustomHeader("X-MSMail-Priority: High");
$this->mailer->AddCustomHeader("Importance: High");
}//function __construct
public function send( $address, $senderName, $replyAddr, $replyName, $fromAddr, $fromName, $subject, $body, $attachment = false, $cc = false, $bcc = false )
{
try {
//$this->mailer->AddReplyTo( $replyAddr, $replyName);
$this->mailer->AddAddress($address, $senderName);
$this->mailer->SetFrom($fromAddr, $fromName);
$this->mailer->AddReplyTo($replyAddr, $replyName);
$this->mailer->Subject = $subject; //'PHPMailer Test Subject via mail(), advanced';
$this->mailer->AltBody = 'در صورتی که قادر به تماشای محتوای ای-میل نیستید از یک نمایشگر ای-میل تحت HTML استفاده کنید!'; // optional - MsgHTML will create an alternate automatically
$this->mailer->MsgHTML($body);
//$this->mailer->MsgHTML(file_get_contents('contents.html'));
//$this->mailer->AddAttachment('images/phpmailer.gif'); // attachment
//$this->mailer->AddAttachment('images/phpmailer_mini.gif'); // attachment
$send = $this->mailer->Send();
if( $send )
{
return true;
}
else
{
return false;
}
}//try send
catch( Exception $e )
{
return false;
}//catch
}//function Send
}//class mailSender
Thanks in Advance
Might be your gmail acct is locked. Try the unlock captcha:
https://accounts.google.com/DisplayUnlockCaptcha.
Also check your account setting called Allow less secure apps here:
https://myaccount.google.com/security#connectedapps
i am using following phpmailer function to send 1000+ mails
<?php
function sendMail($sendTo,$Subject,$Body){
require_once 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com;smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'newsletter#example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'newsletter#example.com';
$mail->FromName = 'xyz';
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->addAddress($sendTo);
$mail->Subject = $Subject;
$mail->Body = ( stripslashes( $Body ) );
$mail->AltBody = 'Please Use a Html email Client To view This Message!!';
if(!$mail->send()) {
$return = 'Message could not be sent.';
// echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$return = 'Message has been sent!';
}
return $return;
}
and this is the code i am using to call function
foreach ($emails as $email) {
$subject = "sample subject";
$body = "sample body";
sendMail($email, $subject, $body);
}
size of $emails array is 1000+
is there any faster and better way to do this?
You should start by reading the docs provided with PHPMailer where you will find this example.
Of particular note in there, make sure you use SMTPKeepAlive - you may find benefit in sorting your list by domain to maximise connection re-use.
As zerkms said, you should submit to a local mail server for best performance, though surprisingly using mail or sendmail options in PHPMailer is not always faster than SMTP to localhost, largely because postfix' sendmail binary opens a synchronous SMTP connection to localhost anyway - postfix' docs recommend SMTP to localhost for best performance for this reason.
If you are sending to localhost, don't use auth or encryption as the overhead doesn't gain you anything, but if you are using a remote server, use tls on port 587 in preference to the obsolete ssl on port 465.
Generally sending directly to end users is to be avoided - the SMTP client in PHPMailer is somewhat dumb - it does not handle queuing at all, so any domains with greylisting or delivery deferrals for traffic control will fail to be delivered. the best approach is to use SMTP to a nearby MTA and leave the queue handling to that. You can get bounces back from that as well so you can remove bad addresses from your list.
Untested, but this should work.
Basically, it reuses the original object (thus reducing memory allocations).
require_once 'PHPMailer/PHPMailerAutoload.php';
class BatchMailer {
var $mail;
function __construct () {
$this->mail = new PHPMailer;
$this->mail->isSMTP();
$this->mail->Host = 'smtp.example.com;smtp.example.com';
$this->mail->SMTPAuth = true;
$this->mail->Username = 'newsletter#example.com';
$this->mail->Password = 'password';
$this->mail->SMTPSecure = 'ssl';
$this->mail->SMTPKeepAlive = true;
$this->mail->Port = 465;
$this->mail->From = 'newsletter#example.com';
$this->mail->FromName = 'xyz';
$this->mail->WordWrap = 50;
$this->mail->isHTML(true);
$this->mail->AltBody = 'Please use an HTML-enabled email client to view this message.';
}
function setSubject ($subject) {
$this->mail->Subject = $subject;
}
function setBody ($body) {
$this->mail->Body = stripslashes($body);
}
function sendTo ($to) {
$this->mail->clearAddresses();
$this->mail->addAddress($to);
if (!$this->mail->send()) {
// echo 'Mailer Error: ' . $this->mail->ErrorInfo;
return false;
} else {
return true;
}
}
}
$batch = new BatchMailer;
$batch->setSubject('sample subject');
$batch->setBody('sample body');
foreach ($emails as $email) {
$batch->sendTo($email);
}
Drop the function into c++ via cgi. A c++ mailer would be far more robust than hitting the entire http framework first. http://www.cplusplus.com/forum/windows/86562/
But PhP already uses hash table for it's associative array, so you won't pick up anymore speed with a hash table. So you really are sort of maxed out in your web framework.
Drop it to a system level function and c is your fastest/leanest choice.
Unless you are really talented with assembly language.