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
Related
I gonna send email via 365 using phpmailer lib.
This is my config
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=support#xxxxx.com
MAIL_PASSWORD=XXXXXX
MAIL_ENCRYPTION=STARTTLS
The following code is function for send email
public function __construct()
{
date_default_timezone_set('America/Virgin');
$this->mail = new \PHPMailer;
$this->mail->isSMTP();
$this->mail->CharSet = 'UTF-8';
//Ask for HTML-friendly debug output
$this->mail->Debugoutput = 'html';
//Set the hostname of the mail server
$this->mail->Host = getenv("MAIL_HOST"); //'ssl://smtp.gmail.com';
$this->mail->Port = getenv("MAIL_PORT");
//Set the encryption system to use - ssl (deprecated) or tls
// $this->mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$this->mail->SMTPAuth = true;
$this->mail->Username = getenv("MAIL_USERNAME");
$this->mail->Password = getenv("MAIL_PASSWORD");
$this->mail->SMTPSecure = getenv("MAIL_ENCRYPTION");
$this->setSender(
$this->sender_email,
$this->sender_password,
$this->sender_display_name,
$this->sender_display_email,
$this->sender_reply_to_name,
$this->sender_reply_to_email
);
}
public function sendEmail($to_email, $to_name, $subject, $message, $template_name = '')
{
//Set who the message is to be sent to
$this->mail->addAddress($this->trim_input($to_email), $this->trim_input($to_name));
//Set the subject line
$this->mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$this->mail->msgHTML($template_name);
//Replace the plain text body with one created manually
$this->mail->AltBody = $message;
//send the message, check for errors
if (!$this->mail->send()) {
return false;
// echo "Mailer Error: " . $this->mail->ErrorInfo;
} else {
return true;
}
}
I have tried to send email on my local server and digital ocean server
In that case I have correctly sent and received email,
but in Godaddy server there is error
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting.
I had tried to changed email driver and another that previously mentioned via this platform Setting up PHPMailer with Office365 SMTP
How to fix that ?
Thanks for your advance.
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/
I tried sending email using smtp gmail using phpmailer. My gmail account got suspended saying that there is an unusual activity. Here is the code I have used for sending emails. What's the correct way of sending emails using smtp gmail in phpmailer?
My question is not duplicate. I have already tried this : Send email using the GMail SMTP server from a PHP page
I'm using phpmailer, and my accounts getting suspended.
<?php
include('phpmailer.php');
class Mail extends PhpMailer
{
// Set default variables for all new objects
public $From = 'noreply#exmaple.org';
public $FromName = SITETITLE;
public $Host = 'smtp.gmail.com';
public $Mailer = 'smtp';
public $SMTPAuth = true;
public $Username = 'username#gmail.com';
public $Password = 'password';
public $SMTPSecure = 'tls';
public $WordWrap = 75;
public function subject($subject)
{
$this->Subject = $subject;
}
public function body($body)
{
$this->Body = $body;
}
public function send()
{
$this->AltBody = strip_tags(stripslashes($this->Body))."\n\n";
$this->AltBody = str_replace(" ", "\n\n", $this->AltBody);
return parent::send();
}
}
Here is my php code :
$to = $_POST['email'];
$subject = "Registration Confirmation";
$body = "<p>Thank you for registering at demo site.</p>
<p>To activate your account, please click on this link: <a href='".DIR."activate.php?x=$id&y=$activasion'>".DIR."activate.php?x=$id&y=$activasion</a></p>
<p>Regards Site Admin</p>";
$mail = new Mail();
$mail->setFrom('noreply#example.org');
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: index.php?action=joined');
exit;
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.
I am using phpmailer to sent email, and it works the recipients receive the mail except the bcc and cc details is not showing the mail. Someone can suggest a solution to this
.
the code is
require_once("PHPMailer_v5.1/class.phpmailer.php");
require_once("PHPMailer_v5.1/language/phpmailer.lang-en.php");
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = "tls";
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 587;
$mailer->Username = "myuserid";
$mailer->Password = "mypassword";
$mailer->FromName = $fromname;
$mailer->From = "myuserid";
$mailer->AddAddress("to#gmail.com",$toname);
$mailer->Subject = $subject;
$mailer->Body =$content;
$mailer->AddCC("something#gmail.com", "bla");
$mailer->AddBCC("foo#gmail.com", "test");
if(!$mailer->Send())
{
echo "Message was not sent";
}
else
echo "mail sent";
Use as
$mailer->AddBCC("foo#gmail.com", "test");
$mailer->AddCC("something#gmail.com", "bla");
You never see BCC details. That's what they are BCC details for. Even the recipient of a BCC will not see his own name with the recipients.
PS: You noticed you wrote addBCC instead of AddBCC (capital A)?
From the phpMailer function reference:
Adds a "Bcc" address. Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
This might be causing your issue.
PHPMailer not sending CC or BCC
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
It´s addBCC
$email->addBCC('my#email.com', 'My Name');
See PHPMailer.php (current version 6.0.5) on line 934 (https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php#L934):
/**
* Add a "BCC" address.
*
* #param string $address The email address to send to
* #param string $name
*
* #return bool true on success, false if address already used or invalid in some way
*/
public function addBCC($address, $name = '')
{
return $this->addOrEnqueueAnAddress('bcc', $address, $name);
}
the bcc will never show; only TO and CC
BCC=Blind Carbon Copy
Here is a working example from the newest release, and on Office 365, I use it to send email from shared folders...
<?
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once('./phpmailer/Exception.php');
require_once('./phpmailer/PHPMailer.php');
require_once('./phpmailer/SMTP.php');
//* Working Example As Of 09/21/2019 - Sends From Shared Mailbox With Mailbox Member
function SendO365EmailTLS($options)
{
$from = isset($options['from']) ? $options['from'] : false;
$recipients = isset($options['recipients']) ? $options['recipients'] : false;
$ccRecipeints = isset($options['ccrecipients']) ? $options['ccrecipients'] : [];
$bccRecipients = isset($options['bccrecipients']) ? $options['bccrecipients'] : [];
$attachments = isset($options['attachments']) ? $options['attachments'] : [];
$credentials = isset($options['credentials']) ? $options['credentials'] : false;
$subject = isset($options['subject']) ? $options['subject'] : '';
$body = isset($options['body']) ? $options['body'] : '';
if(!$from) throw new Exception('Cannot send email with blank \'from\' field');
if(!$recipients) throw new Exception('Cannot send email, no recipients specified!');
if(!$credentials) throw new Exception('Cannot send email, credentials not provided!');
$mail = new PHPMailer;
foreach($recipients as $recipient) $mail->addAddress( $recipient[ 'email'], $recipient['name']);
foreach($ccRecipeints as $ccRecipient) $mail->addCC( $ccRecipient[ 'email'], $ccRecipient['name']);
foreach($bccRecipients as $bccRecipient) $mail->addBCC( $bccRecipient['email'],$bccRecipient['name']);
foreach($attachments as $attachment) $mail->addAttachment($attachment[ 'path' ], $attachment['name']);
$mail->setFrom($from['email'], $from['name']);
$mail->Username = $credentials['username'];
$mail->Password = $credentials['password'];
$mail->Host = 'smtp.office365.com';
$mail->Subject = $subject;
$mail->SMTPSecure = 'tls';
$mail->Body = $body;
$mail->SMTPAuth = true;
$mail->isHTML(true);
$mail->Port = 587;
$mail->isSMTP();
$success = $mail->send();
return $success;
}
// $options = ['from'=> ['email'=>'', 'name'=>''],
// 'recipients'=> [['email'=>'', 'name'=>'']],
// 'ccrecipients'=> [['email'=>'', 'name'=>'']],
// 'bccrecipients'=>[['email'=>'', 'name'=>'']],
// 'attachments'=> [['path'=>'./attachments/file1.jpg','name'=>'1.jpg'],
// ['path'=>'./attachments/file2.jpg','name'=>'2.jpg'],
// ['path'=>'./attachments/file3.jpg','name'=>'3.jpg']],
// 'credentials'=> ['username'=>'','password'=>''],
// 'subject'=> 'Email Subject Line',
// 'body'=> '<h1>Email Body</h1><p>HTML!!!</p>'];
// $success = SendO365EmailTLS($options);
// echo $success ? 'Email Sent':'Email Not Sent';
// die();
$mail->addCC('cc#example.com');//Carbon copy
$mail->addBCC('bcc#example.com');//Blind Carbon copy
Please Read This
https://github.com/PHPMailer/PHPMailer
To operate the clausla BCC AddCC MUST precede as well nuloy hidden email will arrive to your recipient would otherwise happen nuna
Example:
$ mail-> AddCC ("");
$ mail-> AddBCC ("mail # domain")