I have a web app using PHP, MySQL, and PHPMailer to send emails. The issue I am having is PHPMailer will send some of the emails, but not always. I'm thinking maybe it is because different list of emails have data issues? Below is my code that loops through the email addresses and send some, but not all the emails. I have tried to catch errors, but I can't see any in the logs.
How can I debug what is going on, and why some don't send?
else if($current_version == 'PROD'){
date_default_timezone_set('America/New_York');
require 'PHPMailer-master/PHPMailerAutoload.php';
$pageURL = 'xxxxxx';
$subject = "xxxxx ";
$body = "xxxxxx \r\n";
$body .= "xxxxx \r\n \r\n";
$body_html = str_replace("\r\n","<br/>",$body);
try {
$mail = new PHPMailer();
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "xxx#xxxx.com";
$mail->Password = "xxxx";
$mail->setFrom('xxxxx#xxxx.com', 'xxxx');
$mail->addReplyTo('xxxx#xxxx.com', 'xxxx');
$mail->Subject = $subject;
$mail->Body = $body_html;
$mail->AltBody = $body;
$sql_email_list = "SELECT email FROM tbl_email
WHERE distro_name='xxxxs'
AND is_active='Y' ";
$result_email_list = mysql_query($sql_email_list);
$num_email_list = mysql_num_rows($result_email_list);
$lost_email_list = mysql_result($result_email_list,0,'email');
$lost_email_array = explode(";",$lost_email_list);
foreach ($lost_email_array as $key => $val){
$mail->AddAddress($val);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
$sql_email_one = "SELECT table2.email
FROM table1
INNER JOIN table2 ON table1.cntct = table2.cntct_id
WHERE id='$id' ";
$result_email_one = mysql_query($sql_email_one);
$num_email_one = mysql_num_rows($result_email_one);
for($iiz=0;$iiz<$num_email_one;$iiz++){
$one_email = mysql_result($result_email_one,$iiz,'email');
$mail->AddAddress($one_email);
if($carrier_email_flag_global == 'ON'){
$mail->send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
echo '<hr>Email Sent to: '.$val;
}
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
echo '<hr>ERROR 001';
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
echo '<hr>ERROR 002';
}
}
Related
I need to send over 1000 mails and I am using PHPMailer for it.
I am sending mails as HTML and using SMTP.
My code looks like:
while($count<1000)
{
try{
if($count%20==0)
{
$mail = new PHPMailer;
$mail->SMTPDebug = true; // Enable verbose debug output
//$mail->SMTPDebug = 3;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->Timeout = 200;
$mail->SMTPKeepAlive = true;
$mail->setFrom(username, sender);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
}
$msg = htmlspecialchars_decode($message);
$msg = substr($msg,0,-14);
$mail->Body = $msg;
$mail->addAddress(emails);
if($mail->send())
{
$flag="1";
}
$count++;
$mail->ClearAddresses();
$mail->Smtpclose();
}catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
Atmost 20 mails are only sent at a time.
Is it because mails may contain spam?
Suggest you to delay your loop, you can use sleep.
I think you can send 5 per second with no problem, but you need to check with your email provider.
I'm trying to send multiple separate emails to multiple addresses. The below code sends it in 1 email with multiple TO addresses. This becomes an issue because everyone in the email can see each other's email addresses.
Is there a way to send separate emails?
<?php
$smtp = 'xxx.com';
$port = 25;
$secure = 'tls';
$username = 'test#xxx.com';
$pass = '';
$from = 'test#xxx.com';
$to = 'info#xxx.com';
$to1 = '';
$subject = 'Test Email';
$content = $mail_content;
require_once("include/class.phpmailer.php");
$mail=new PHPMailer(true);
$mail->IsSMTP();
try{
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
while($row = mysqli_fetch_object($result)) {
$mail->AddAddress($row->email);
echo $row->email."<br>";
}
}
$mail->Subject = $subject;
$mail->MsgHTML($content);
$mail->Send();
if (isset($email) && $email) {
?>
<script>location.href="<?php echo '../index.php' . $_REQUEST['redirect']; ?>";</script>
<?php
}
}
catch (phpmailerException $e){
echo $e->errorMessage();
}
catch (Exception $e){
echo $e->getMessage();
}
?>
Send the email in the BCC.
Look here for an example :
PHP Email sending BCC
You have a loop that checks for all of the email addresses and then assigns the email address to $mail->AddAddress().
All you need to do in this same loop is send the email with $mail->Send(); with each iteration of the loop as well
//PHPmailer object
$mail=new PHPMailer(true);
//set up
$mail->IsSMTP();
$mail->Host = $smtp;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->SMTPSecure = $secure;
$mail->Username = $username;
$mail->Password = $pass;
$mail->SetFrom($from);
$mail->Subject = $subject;
$mail->MsgHTML($content);
//add address
if (isset($email) && $email) {
$mail->AddAddress($email);
}
else {
//loop through the DB results
while($row = mysqli_fetch_object($result)) {
//you already have this
$mail->AddAddress($row->email);
echo $row->email."<br>";
//now send it here as well
$mail->Send();
//Do more stuff here
}
}
I have not tested this on my local system but this is one way to send the mail individually for each address as an alternative to sending one mail and using bcc.
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'] ;
I'm trying to run PHPMailer for an internal contact form and I am getting the error ERROR: AUTH not accepted from server. Here is my current code..
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = True;
$mail->Username = 'xxxxx';
$mail->Password = '***';
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
This error basically means that your attempt to authenticate was rejected by the remote server. Different PHPMailer settings (well SMTP settings) are required by different remote mail servers.
This could be caused by
Using the wrong port
Using the wrong host
Incorrect user/pass
Incorrect SMTPSecure
Example SMTP setup:
Gmail: use of phpmailer class
Hotmail: phpmailer with hotmail?
If you are using this internally, you may not need to use SMTP authentication at all, depending on your server settings. Try this and see if it works:
require_once('/includes/class.phpmailer.php');
include("/includes/class.smtp.php");
if (isset($_POST['submit'])) {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
try {
$mail->Host = "192.168.6.6";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = False;
$mail->Port = 25;
$mail->AddAddress('xxx#xxx.com', 'xxxxx');
$mail->SetFrom($email);
$mail->Subject = 'New message from Contact Form';
$mail->Body = $message;
$mail->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
};
i am trying to send email to user and to admin at the same time using phpmailer but only one email is sent.. either to user or to admin..
i have tried every possible way with no luck. please help
below is my code.
$html.="This is my message to user";
$bcc = "xxx#gmail.com";
$to= $clientemail;
$subject = "This is my subject;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
$mail->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
$message="Thank you for your request.";
$html="this is my second message to admin";
$bcc = "xxx#gmail.com";
$to= $admin;
$subject = "message to admin;
$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;
$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "$host"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = $username; // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;
$mail2->SMTPSecure = 'tls';
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx#gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body = $html;
$mail2->AltBody = $html;
$mail2->Send();
// echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
I'm not sure if you want to send the same email or not to differents persons, but the first improvement to do is to factorize your code.
// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
try {
$mail->Host = "$host"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = $from;
$mail->FromName = $Name;
foreach ($emails as $to) {
$mail->AddAddress($to);
}
$mail->AddBCC('xxx#gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = $html;
if(!$mail->Send()) {
// log if needed
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
}
now you can use this function with differents messages or subjects... whatever you want!
EDIT :
to send different message to users and admins :
//settings for admin
$emails = array('admin#mail.com', 'admin2#mail.com'); // or just array('admin#mail.com');
$subjects = 'my sub';
$msg = 'my msg';
sendMail($emails, $subjects, $msg);
//settings for users
$emails = array('user#mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';
sendMail($emails, $subjects, $msg);