Send Mass E-mail PHPMAILER through G-Suite - php

So i have the following code:
while(list($i, $data_subscriber) = each($subscriber)) {
$email_counter = $i + 1;
echo ($email_counter).') '.$data_subscriber->field_1.' '.$data_subscriber->field_2.' - '.$data_subscriber->email.''."\n";
flush();
$language = $data_subscriber->lang;
$body_mail = include($template_send_newsletter);
$first_name_idx = $data_subscriber->field_1;
$last_name_idx = $data_subscriber->field_2;
if (mail_send(strtolower($data_subscriber->email),
( IsSet($first_name_idx) && IsSet($last_name_idx) ? $first_name_idx.' '.$last_name_idx : $data_subscriber->email ),
$site_admin,
$site_name,
'Newsletter ['.date('d/m/Y', time()).']',
true,
$body_mail,
NULL,
false)) {
reset($data_subscriber->newsletter_item);
unset($log_detail);
$log_detail = "";
while(list($j, $list_of_news_to_send) = each($data_subscriber->newsletter_item)) {
$log_detail = ( IsSet($list_of_news_to_send->title) ? $list_of_news_to_send->title."\n" : '');
$log_detail = ( IsSet($list_of_news_to_send->subtitle) ? $list_of_news_to_send->subtitle."\n" : '');
$log_detail = ( IsSet($list_of_news_to_send->creation_date) ? $list_of_news_to_send->creation_date."\n" : '');
$log_detail = "\n";
} /* end while */
write_newsletter_detail($data_subscriber->id_subscription,
$data_subscriber->email,
'Y',
$log_detail);
}
else {
write_newsletter_detail($data_subscriber->id_subscription,
$data_subscriber->email,
'N',
NULL);
} /* end if mail_send */
} /* end while */
And the following function
function mail_send($rcpt_to,
$to_name,
$mail_from,
$from_name,
$subject,
$isHTML = true,
$body,
$attachment = NULL,
$direct_delivery = false,
$reply_to = NULL){
$mail = new PHPmailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "mail#domain.com";
$mail->Password = "password";
$mail->Timeout = 3600;
$mail->addAddress(trim($rcpt_to), trim($to_name));
$mail->setFrom($mail_from,$from_name);
$mail->Subject = $subject;
$mail->Body = $body;
if ($isHTML) {
$mail->IsHTML(true);
$mail->AltBody = strip_tags($mail->Body);
}
if (IsSet($attachment)) {
$mail->AddAttachment($attachment['tmp_name'], $attachment['name']);
}
if (IsSet($reply_to)) {
$mail->AddReplyTo($reply_to);
}
if (!$mail->send()) {
return false;
} else {
return true;
}
}
I am using PHPMailer to send multiple e-mails using gmail SMTP server.
I have 4 files containing different but almost the same code to send newsletter.
One of my files is sending all the emails to ( ~60 recipients )
The other 3 files, each of them needs to send one email to ~600 recipients.
Problem occures here, half of the e-mails are sent the other are not.
If I run the script but this time for fewer recipients witch didn't recieved the e-mail first time, it's sending.
I'm using GMAIL with my own domain. So i'm not exceeding any limit.
What may cause this problem ? Any ideas ? Thank you.
UPDATE**
SERVER -> CLIENT: 454 4.7.0 Too many login attempts, please try again later. e11sm950198edd.68 - gsmtp
After some emails that are sent i recieve this error. Any ideas why ?
UPDATE***
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->Username = "domain#domain.com";
$mail->Password = "password";
$mail->setFrom($site_noreply, $site_name);
$subjectnewsletter = 'Newsletter ['.date('d/m/Y', time()).']';
$mail->Subject = $subjectnewsletter;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
reset($subscriber);
while(list($i, $data_subscriber) = each($subscriber)) {
$email_counter = $i + 1;
echo ($email_counter).') '.$data_subscriber->field_1.' '.$data_subscriber->field_2.' - '.$data_subscriber->email.''."\n";
flush();
$language = $data_subscriber->lang;
$first_name_idx = $data_subscriber->field_1;
$last_name_idx = $data_subscriber->field_2;
$fullname = $first_name_idx.' '.$last_name_idx;
$bodymail = include($template_send_newsletter);
$mail->msgHTML($bodymail);
$mail->addAddress($data_subscriber->email, $fullname);
reset($data_subscriber->newsletter_item);
unset($log_detail);
$log_detail = "";
while(list($j, $list_of_news_to_send) = each($data_subscriber->newsletter_item)) {
$log_detail = ( IsSet($list_of_news_to_send->title) ? $list_of_news_to_send->title."\n" : '');
$log_detail = ( IsSet($list_of_news_to_send->subtitle) ? $list_of_news_to_send->subtitle."\n" : '');
$log_detail = ( IsSet($list_of_news_to_send->creation_date) ? $list_of_news_to_send->creation_date."\n" : '');
$log_detail = "\n";
} /* end while */
if (!$mail->send()) {
write_newsletter_detail($data_subscriber->id_subscription,
$data_subscriber->email,
'N',
NULL);
echo $mail->ErrorInfo;
break; //Abandon sending
} else {
write_newsletter_detail($data_subscriber->id_subscription,
$data_subscriber->email,
'Y',
$log_detail);
echo "Message sent";
}
$mail->clearAddresses();
$mail->clearAttachments();
} /* end while */
I managed to change my code so it's not connect to the smtp server every e-mail.
But now after it sends 93 emails i get the follosing error:
SMTP server error: MAIL FROM command failed

You need to use an MTA that uses more than one connection and it already setup to respect limits of the servers you're connecting to. At this point you're attempting to exceed the limits of the server you're connecting to and companies like Google frown on too many connections or using their mail servers for broadcasts.
Look for a mail sending service if your numbers are modest, or consider purchasing and hosting an MTA if you send a considerable amount of email.

You can use SMTP relay configuration which allow you to send email from your server with any of your email domain address.

Related

PHP sending emails to but not text

I am trying to send emails to phones that have Verizon numbers. Here is my code right now
<?php require('includes/config.php');
function Send( $ToEmail, $MessageHTML, $MessageTEXT) {
require_once ( 'classes/phpmailer/phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "box405.bluehost.com"; // Sets SMTP server
$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "ssl"; //Secure conection
$Mail->Port = 465; // set the SMTP port
$Mail->Username = 'techsupport#test.com'; // SMTP fake account username
$Mail->Password = 'password1'; // SMTP fake account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->FromName = 'Tech support';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->AddAddress( $ToEmail ); // To:
$Mail->isHTML( TRUE );
$Mail->Body = $MessageHTML;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
if ( $Mail->IsError() ) { // ADDED - This error checking was missing
return FALSE;
}
else {
return TRUE;
}
}
$stmt = $db->prepare("select phone From members where phone not like 'no';");
$stmt->execute();
$result = $stmt->fetchAll();
$ToEmail = $result[0][0];
$ToName = 'techsupport';
$MessageHTML = "test";
$MessageTEXT = 'test';
$Send = Send( $ToEmail, $MessageHTML, $MessageTEXT);
if ( $Send ) {
echo "<h2> Sent OK</h2>";
}
else {
echo "<h2> ERROR</h2>";
}
die;
?>
this works when I try to send emails but when I use it to send texts it says sent but I do not receive anything. I know this is not because of the address because I use the same address when I text myself from gmail and it is not the sql query because I have tested it . I Think the problem is in the smtp or phpmailer for I have not used either of those a lot. Also the code runs though and prints out the SENT OK echo but nothing goes through and no errors.
[EDIT] To answer ironcito's question I am sending the email to
phonenumber#vtext.com
I know this works because I have used the same address through gmail.
I think this might be because you push the content to the mail (body) before you set the content (messagehtml/messagetext), which creates an empty email. Try changing these around, that might be the solution.

convert class.phpmailer.php html email to class.smtp.php

I spent the last 2 days getting a web form to send to outside domains using an internal exchange server with phpmailer.
Unfortunately now, the syntax is different and my old code doesn't work.
Has anyone used smtp.php class to send html emails?
Old code, can only send internally:
require "phpmailer/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->SMTPDebug = 2;
$mail->SMTPSecure = false;
$mail->Host = "10.10.10.10";
$mail->Port = 25;
$mail->From = "user#company.com";
$mail->FromName = "user";
$mail->SetFrom("user#company.com", "user);
$mail->Subject = $_POST['company'].": New MFD(s) (Time Sensitive)";
$mail->AddEmbeddedImage('trans2.png', 'logo', 'trans2.png ');
$mail->AddAttachment("docs/install.xlsx");
$mail->MsgHTML($message1.$message2.$message3);
$mail->AddAddress($_POST['emailid'], "");
$mail->AddCC("user#company.com");
$result = $mail->Send();
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
new code, which works with an exchange server sending to outside domains:
require("smtp.php");
require("sasl.php");
$from="user#company.com"; $sender_line=__LINE__;
$to="person#company.com"; $recipient_line=__LINE__;
if(strlen($from)==0)
die("Please set the messages sender address in line ".$sender_line." of the script ".basename(__FILE__)."\n");
if(strlen($to)==0)
die("Please set the messages recipient address in line ".$recipient_line." of the script ".basename(__FILE__)."\n");
$smtp=new smtp_class;
$smtp->host_name="mailserver"; /
$smtp->host_port=587;
$smtp->ssl=0;
$smtp->start_tls=1;
$smtp->localhost="localhost";
$smtp->direct_delivery=0;
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host="";
$smtp->user="user";
$smtp->realm="myrealm";
$smtp->password="mywonderfulpassword";
$smtp->workstation="workstationname";
$smtp->authentication_mechanism="NTLM";
if($smtp->direct_delivery)
{
if(!function_exists("GetMXRR"))
{
$_NAMESERVERS=array();
include("getmxrr.php");
}
else
{
$_NAMESERVERS=array();
if(count($_NAMESERVERS)==0)
Unset($_NAMESERVERS);
include("rrcompat.php");
$smtp->getmxrr="_getmxrr";
}
}
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class",
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"))
echo "Message sent to $to OK.\n";
else
echo "Could not send the message to $to.\nError: ".$smtp->error."\n";
How do i send html emails and add attachments?
Disregard, I was able to get the phpmailer to work with my exchange server
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = 0;
$mail->SMTPSecure = 'tls';
$mail->Host = "exchangeserver.com";
$mail->Port = 587;
$mail->Username = $_POST['emailusername'] ;
$mail->Password = $_POST['emailpassword'] ;

phpmailer throwing intermittent error : EOF found while checking if connected

I am trying to send mail via SMTP using PHPMailer class. My problem is , for the first attempt , the mail sender is working oerfect , but for all subsequent attempts it's throwing error :
SMTP -> NOTICE:
EOF caught while checking if connected
My email sending code is :
function sendEmail($toAddress,$fromAddress,$toName,$fromName,$subject,$emailContent,$content_type = false, $attach_path="", $cc = '', $cc_name="")
{
require_once('phpmailer/class.phpmailer.php');
if (empty($content_type)) $content_type = false;
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = MY_SMTP_AUTH; // turn on SMTP authentication
$mail->Host = MY_SMTP_HOST_NAME;
if (!empty($this->smtpEncryptionMode))
{
$mail->SMTPSecure= $this->smtpEncryptionMode;
}
if (!empty ($this->smtpPort))
{
$mail->Port = MY_SMTP_PORT;
}
else $mail->Port = 25;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpUserPassword;
$mail->From =$fromAddress;
$mail->FromName = $fromName;
if(is_array($toAddress))
{
foreach($toAddress as $to)
{
$mail->AddAddress($to, "" );
}
}
else
{
$mail->AddAddress($toAddress, $toName );
}
$mail->AddReplyTo($fromAddress, $fromName );
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 80; // set word wrap to 80 characters
$mail->IsHTML($content_type); // set email format to basic
$mail->Subject = $subject;
$mail->Body = $emailContent;
//Here it sets other parameters e.g attachment path etc.
$mail->SMTPDebug = true;
$result = $mail->Send();
if($result == false ) { $result = $mail->ErrorInfo;echo $result; }// Switch this on when debugging.
return $result;
Why is it throwing the error for all successive attempts?
From , what I can infer from class.smtp.php is that it is failing inside a function Connected() which actually checks the socket status of the smtp_connection instance, and there it is getting EOF.
I guess the connection itself isnot getting established...But what is going right in the first instance then?
Is the function in a while loop and if so you need to close the class. Might be to easy try this.
$result = $mail->Send();
$mail->close();
you can't send to mail instances at once for example you call test_mail() and then emailer() function test_mail uses another connection while the other function uses phpmailer and is connected to port 465. Now emailer will throw an EOF error because the connection on the first function is not yet exited.
/*function test_mail(){
$to = 'emil.nrqz#gmail.com';
$subject = 'Test email using PHP';
$message = 'This is a test email message'; $headers = 'From: webmaster#example.com' . "\r\n" . 'Reply-To: webmaster#example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers, '-fwebmaster#example.com');
}*/
function emailer($sendby,$subject,$body,$sendto,$cc){
require("../obj/PHPMailer-master/class.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->Host = "relay-hosting.secureserver.net";
$mail->Port = 25; // or 587 or 465
/*
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 465; // or 587 or 465
*/
//var_dump($mail);exit();
$mail->IsHTML(true); // sends email as html
$mail->Username = MAILER_USERNAME; // mailserver username
$mail->Password = MAILER_PASSWORD; // mailserver password
$mail->SetFrom("info#file-bird.com"); // Seen as message from
$mail->Subject = $subject; // Subject of the message
$mail->Body = $body; // email body - can be html
$mail->AddAddress($sendto); // where email will be sent
$mail->addCC($cc);
if(!$mail->Send()){
return "Mailer Error: "; // . $mail->ErrorInfo
}
else{
return "Message has been sent";
$mail->close();
}
}

Problem with using PHPMailer for SMTP

I have used PHPMailer for SMTP and there is problem in sending mail with error "Mailer Error: The following From address failed: no-reply#mydomain.org.uk"
My code is as follows:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "localhost;"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $email_address;
$mail->FromName = $email_address;
$mail->AddAddress($arrStudent[0]["email"]);
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject";
$theData = str_replace("\n", "<BR>", $stuff);
$mail->Body = $theData; // "This is the <b>HTML body</b>";
$mail->AltBody = $stuff;
if (!$mail->Send()) {
$sent = 0;
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
i researched everything and when i debug inside class.smtp.php i found error the function "get_lines()" is returning error value "550 Authentication failed"
The code was working fine previously, i am wondering how this problem came suddenly.
Desperate for some help.
Thanks,
Biplab
public function sendEmail ( $subject, $to, $body, $from = FALSE ) {
require_once('mailer.class.php');
$mailer = new PHPMailer();
//do we use SMTP?
if ( USE_SMTP ) {
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = SMTP_HOST;
$mailer->Port = SMTP_PORT;
$mailer->Password = '';
$mailer->Username = '';
if(USE_SSL)
$mailer->SMTPSecure = "ssl";
}
$mailer->SetFrom($from?$from:ADMIN_EMAIL, ADMIN_NAME);
$mailer->AddReplyTo ( ADMIN_EMAIL, ADMIN_NAME );
$mailer->AddAddress($to);
$mailer->Subject = $subject;
//$mailer->WordWrap = 100;
$mailer->IsHTML ( TRUE );
$mailer->MsgHTML($body);
require_once('util.class.php');
$mailer->AltBody = Util::html2text ( $body );
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if ( ! $mailer->Send() ) {
return FALSE;
}
else {
$mailer->ClearAllRecipients ();
$mailer->ClearReplyTos ();
return TRUE;
}
}
I've used like that... SetFrom should be used in place of From... that's your error buddy... :))
try adding belowe line to php.ini
extension=php_openssl.dll
restart and try again
I am using YII's Mailer with PHPMailer, and this works for me:
$mail = Yii::createComponent('application.extensions.mailer.EMailer');
$mail->Username = $this->SMTP_USERNAME; // SMTP username
$mail->Password = $this->SMTP_PASSWORD; // SMTP password
$mail->SMTPAuth = true;
$mail->From = $this->fromAddress;
$mail->Host = $this->SMTP_SERVER_ADDRESS;
$mail->FromName = $this->fromName;
$mail->CharSet = 'UTF-8';
$mail->Subject = Yii::t('mailer', $this->subject);
$mail->Body = $this->message;
$mail->AddReplyTo($this->toAddress);
$mail->AddAddress($this->toAddress);
$mail->IsSMTP(true);
$mail->IsHTML(true);
$mail->Send();
Hope that helps?

same program not running on a different server

Im facing a strange problem.. iv written some codes on sending mails using google smtp, yahoo smtp, and aol smtp in php. its working fine. but when im trying to run the same codes on a different server and domain it is giving me the following error:
SMTP Error: Could not connect to SMTP host.
any solutions??
Most probably you are not allowed to connect to port 25. Check with the hosting company which SMTP server to use for outgoing mail.
<?php
require_once('class.phpmailer.php');
require_once 'Excel/reader.php';
//$myid=$_REQUEST['myid'];
//$mypass=$_REQUEST['mypass'];
//$msg=$_REQUEST['msg'];
define('GUSER', 'ss#ss.com'); // Gmail username
define('GPWD', 'pass'); // Gmail password
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.aol.com';
$mail->Port = 465;
//$mail->AddAttachment('upload/logo.png', 'logo.png');
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
//$mail->MsgHTML(file_get_contents('test.html'));
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}
// initialize reader object
$excel = new Spreadsheet_Excel_Reader();
// read spreadsheet data
$excel->read('Book1.xls');
// iterate over spreadsheet cells and print as HTML table
$x=1;
while($x<=$excel->sheets[0]['numRows']) {
$y=1;
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
smtpmailer( $cell, 'ss#ss.com', 'name', 'Subject', 'trying the aol');
$y++;
}
$x++;
}
?>

Categories