i am using phpmailerclass with my website forms and its working 100% fine and i could not see any error also sending emails fine but sometimes rare miss send email. when form miss send email behaviour is like when click on submit button miss send email but on second try click submit send email. not always like that email send perfect on first try but when problem start need to second try then send email. i have Gsuite business account google side setting correct too. why sometimes missing ?
include("phpmailer/PHPMailerAutoload.php");
$fmail="username#domain.com";
// start email1
$mail = new PHPMailer();
$mail->Host='smtp.gmail.com';
$mail->port=587;
$mail->SMTPSecure='tls';
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "username#domain.com";
$mail->Password = "passwordhere";
$mail->FromName = "companyName";
$mail->SetFrom($fmail);
$mail->AddAddress($mailaddress);
//$mail->AddCC("companyname#gmailcom.com");
$filename11=$fileName1;
if($filename11!="")
{
$dir1= uploadFilePathAdmin.$orderNo."-".$filename11;
$mail->AddAttachment($dir1);
}
$filename12=$fileName2;
if($filename12!="")
{
$dir2= uploadFilePathAdmin.$orderNo."-".$filename12;
$mail->AddAttachment($dir2);
}
$mail->IsHTML(true);
$mail->Subject = "".stripslashes($designName)."-".$orderNo." - PO: ".$ponumber."";
$mail->Body = $MESSAGE;
$mail->AltBody = $MESSAGE;
$mail->Send();
// start email2
$mail2 = new PHPMailer();
$mail2->Host='smtp.gmail.com';
$mail2->port=587;
$mail2->SMTPSecure='tls';
$mail2->IsSMTP();
$mail2->SMTPAuth = true;
$mail2->Username = "username#domain.com";
$mail2->Password = "passwordhere";
$mail2->FromName = "companyName";
$mail2->SetFrom($emailee);
$mail2->AddAddress($fmail);
$mail2->AddReplyTo($mailaddress);
$filename21=$fileName1;
if($filename21!="")
{
$dir33= uploadFilePathAdmin.$orderNo."-".$filename21;
$mail2->AddAttachment($dir33);
}
$filename22=$fileName2;
if($filename22!="")
{
$dir22= uploadFilePathAdmin.$orderNo."-".$filename22;
$mail2->AddAttachment($dir22);
}
$mail2->IsHTML(true);
$mail2->Subject = "".$designName."-".$orderNo." - PO: ".$ponumber."";
$mail2->Body = $MESSAGE;
$mail2->AltBody = $MESSAGE;
$mail2->Send();
Related
I am using PHPMailer to send emails. I have created a function that sends 3 emails to 3 different email addresses (sending 9 emails in total).
The first email address is receiving all the 3 emails.
The second email address is receiving 2 emails.
The third email address is receiving only 1 email.
Why this happening?
Here is my code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
$mail = new PHPMailer(true);
$mail1 = phpmaileremail($reciever1, $usertype1, $file, $subject1, $body1);
$mail2 = phpmaileremail($reciever2, $usertype2, $file, $subject2, $body2);
$mail3 = phpmaileremail($reciever3, $usertype3, $file, $subject3, $body3);
function phpmaileremail($reciever,$usertype, $file, $subject, $body)
{
global $mail;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxx#gmail.com';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xxx', 'xxx');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
$mail->addAttachment($file);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
Because you're reusing the $mail object to addAddress() and send(). So the first time you call phpmaileremail() the first address gets the email. Then when you call it for the second time the second address is added and the first and second address get the email. And so on.
A simple solution would be to create the $mail object inside the phpmaileremail() function:
function phpmaileremail($reciever,$usertype, $file, $emailsubject, $email_body )
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
$mail->addAddress($reciever);
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
echo "Mail sent";
}
PS: Not that it matters, but reciever is written receiver. I've made that mistake as well.
Kiko's answer will work, however it's not the best way. As its name suggests, addAddress adds an address, it doesn't set absolutely or replace existing recipients you've already added.
PHPMailer has a standard function to clear the list of addresses you're ending to called clearAddresses, so the right approach is to call that after each message you send and add the new address before sending the next one, so the sequence will be roughly:
addAddress();
send();
clearAddresses();
addAddress();
send();
and so on. This is most clearly demonstrated in the mailing list example provided with PHPMailer, which does its sending in a loop, calling clearAddresses each time around.
You can achieve the same thing using a new instance of PHPMailer each time (which has the effect of clearing addresses, but also clears everything else too), but it's more efficient to re-use the instance. This is especially true if you're sending over SMTP (which you are) because it will allow you to make use of keepalive, which dramatically reduces the overhead of making an SMTP connection. If you use a new instance, the connection is dropped and recreated each time. You can achieve this inside your function by making the PHPMailer instance static:
function phpmaileremail($reciever, $usertype, $file, $emailsubject, $email_body)
{
static $mail;
if ($mail === null) {
//Set everything that remains the same all the time in here
$mail = new PHPMailer();
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXXX#gmail.com';
$mail->Password = 'XXXXXXXXXXXXXXXXXX';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPKeepAlive = true;
$mail->setFrom('XXXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXX');
}
$mail->addAddress($reciever, $usertype);
// Attachments
$mail->addAttachment($file); // Add attachments
$mail->isHTML(true);
$mail->Subject = $emailsubject;
$mail->Body = $email_body;
$mail->AltBody = 'NA';
$mail->send();
$mail->clearAddresses();
$mail->clearAttachments();
echo "Mail sent";
}
This has the added benefit of not using a global. Also note the use of clearAttachments, as that works the same way as addresses.
I have this code for sending mails using PHPMailer. The problem is that mails are getting repeated with my current code. It goes in this format.
Mail Sends To:
user#gmail.com (1st go)
user1#gmail.com, user#gmail.com (2nd go)
user2#gmail.com, user1#gmail.com, user#gmail.com (3rd go)
user3#gmail.com, user2#gmail.com, user1#gmail.com, user#gmail.com (4th go)
....and so on.
I think this is because of my while loop logic. What can be the other way to send bulk email to members in my database just once without getting repeated?
Here is my code:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = $mainf['set_smtp_host'];
$mail->Port = $mainf['set_smtp_port'];
$mail->SMTPSecure = $mainf['set_smtp_security'];
$mail->IsHTML(true);
$mail->SMTPAuth = true;
$mail->Username = $mainf['set_smtp_uname'];
$mail->Password = $mainf['set_smtp_pass'];
$mail->setFrom($mainf['set_noreply_email'], $mainf['set_site_name']);
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Subject = $sub;
$mail->Body = $mail_msg;
$emails = $pdo->prepare("SELECT mem_name, mem_email FROM members WHERE mem_email_verified = 'yes' ORDER BY mem_id ASC LIMIT 5");
$emails-> execute();
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$send = $mail->Send();
}
if($send){
$msg = "<div class='alert alert-success'>Mail sent to all members successfully.</div>";
}else{
$msg = "<div class='alert alert-danger'>Mail Server Error! Please refresh the page and try again.</div>";
}
Also, in the mailbox I can see who are the others to whom the mail was sent to. Can I add BCC option to send bulk emails just once without getting it repeated for anyone?
// option 1
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$send = $mail->Send();
$mail->ClearAllRecipients(); // reset the `To:` list to empty
}
// option 2
while($u = $emails->fetch()){
$mail->addAddress($u['mem_email'], $u['mem_name']);
$mail->AddBCC($u[0]);
}
$mail->send();
I have the following code to send an email with phpMailer. What I don't understand is why the email is sent and everything is OK even if I use a wrong password. In that case, the headers of the email give this answer (adapted from real case): X-PHP-Originating-Script: 532:class.phpmailer.php
How can I force the login error to appear and avoid the email being sent anyway? I guess that this has something to do with the class using other methods after trying to connect to the SMTP server. I don't want the email to be sent in ALL cases, if the password has changed or service not available, I want to know it and the script to stop and throw error. I use the latest available version of the class.
require 'phpmailer/class.phpmailer.php';
$to = "someone-email#example.com";
$subject = "Hello World";
$body = "HELLO WORLD";
$mail = new PHPMailer;
$mail->Host = "mail.example.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "example#example.com";
$mail->Password = "**********";
$mail->From = "example#example.com";
$mail->FromName = "TEST";
$mail->AddReplyTo($to);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "KO " . $mail->ErrorInfo;
return false;
}
else
{
echo "OK";
return true;
}
You aren't using SMTP so I guess it's defaulting to mail(), which doesn't accept authentication.
To enable SMTP:
$mail->IsSMTP();
I have a portion of my project that grabs some customer information from a DB and sends a text-message to a salesman, using PHP Mailer. Some of the customer info included:
Name
Phone
Phone 2
Address
City
State
Zip
Notes
As you can imagine, 160 characters won't cut it. I need to be able to send at least two text messages to the same number within the same function.
I have a single text message working, using PHP Mailer. I will post the relevent code below:
db_functions.php:
function send_text($name, $message){
require 'class.phpmailer.php';
$to = 'xxxxxxxxxx#vtext.com';
$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->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = 'xxxxxxx';
$mail->SetFrom('xxxxxx#gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();
$mail->ClearAddresses();
return;
}
assign_lead.php:
include 'mysql_login_pdo.php';
include '../functions/db_functions.php';
if (!isset($_POST['leadID'])) {
return;
} else {
$leadID = $_POST['leadID'];
}
if (!isset($_POST['salesID'])) {
return;
} else {
$salesID = $_POST['salesID'];
}
//DB FUNCTIONS
db_assignLead($leadID, $salesID);
$message = db_assignLeadNote($leadID);
$name = db_assignLeadName($leadID);
db_assignAddNote($leadID, $message);
//-------------------------This is the problematic area---------------------
//SEND TEXT MESSAGE(s)
send_text($name, $message);
$message = '8104124200_230 N Main St_Davison_48423';
send_text($name, $message);
As you can see, I want a text message to send to a salesman with the customer's name and a note about the customer. Then, I want to send a second text message with the customer's name and address information. I've used a placeholder of '8104124200_230 N Main St_Davison_48423' for now, but it will be replaced by a function that searches for the address info in the DB.
The first text message sends fine, but the second refuses to send. I made it work once by using a 20-second sleep, but from what I've read, it may be unnecessary. Also, the 20-second sleep was completely unreliable.
As always, I appreciate any help.
I ended up doing the following, which worked. I'm hoping it won't cause issues down the road, but if it does, I'll come back and update this post.
Basically, instead of calling the send_text() function twice, I sent the message twice within the same function:
function send_text($name, $message){
include 'class.phpmailer.php';
//$to = $_POST['to'];
//$password = $_POST['password'];
$to = 'xxxxxx#gmail.com';
$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->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = 'xxxxxx';
$mail->SetFrom('xxxxxx#gmail.com');
$mail->Subject = $name;
$mail->Body = $message;
$mail->AddAddress($to);
$mail->Send();
//Send the second message
$mail->Body = 'Testing second message';
$mail->Send();
//End Second message
return;
}
I am rather puzzled with this one.
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
if($mail->Send())
{
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->ClearCustomHeaders();
...
$mail->From = "DO_NOT_REPLY#...";
$mail->FromName = "****";
$mail->AddAddress($ToEmail1, "***"); //To: (recipients).
$mail->AddAddress($ToEmail2, "***"); //To: (recipients).
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $notification_subject;
$mail->Body = $notification_msg;
if($mail->Send())
{
...
The first email sends fine. The second one doesn't. What could be the reason for that behavior? Am I missing some kind of reset?
Update: using a different mail server seems to work so apparently it's a setting of that specific mail server causing problems. Any idea what that could be?
Some providers impose restrictions on the number of messages that can be sent within a specific time span. To determine if your problem depends by a provider "rate limit", you should try to add a pause after the first send. For example:
if ($mail->Send()) {
sleep(10); // Seconds
...
if ($mail->Send()) {
...
}
}
Then, by progressively lowering the sleep time, you should be able to determine which is the rate limit.
Try this:
As #Felipe Alameda A mentioned Remove $mail->SMTPKeepAlive = true;
// for every mail
if(!$mail->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}
$mail->SmtpClose();
IMHO you need to create new PHPMailer object for every sent email. If you want to share some common setup, use something like this:
$mail = new PHPMailer();
/* Configure common settings */
while ($row = mysql_fetch_array ($result)) {
$mail2 = clone $mail;
$mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
$mail2->AddAddress($row["email"], $row["fname"]);
$mail2->send();
}
I think your problem is $mail->SMTPAuth = false;
It is hard to believe there are ISP or SMTP providers that don't require authentication, even if they are free.
You may try this to check for errors instead of or in addition to checking for send() true:
if ( $mail->IsError() ) { //
echo ERROR;
}
else {
echo NO ERRORS;
}
//Try adding this too, for debugging:
$mail->SMTPDebug = 2; // enables SMTP debug information
Everything else in your code looks fine. We use PHPMailer a lot and never had any problems with it
The key may lie in the parts you have omitted. Is the domain of the sender of both emails the same? Otherwise the SMTP host may see this as a relay attempt. If you have access to the SMTP server logs, check these; they might offer a clue.
Also, check what $mail->ErrorInfo says... it might tell you what the problem is.
i personally would try to make small steps like sending same email.. so just clear recipients and try to send identical email (this code works for me). If this code passes you can continue to adding back your previous lines and debug where it fails
and maybe $mail->ClearCustomHeaders(); doing problems
//SMTP servers details
$mail->IsSMTP();
$mail->Host = "mail.hostserver.com";
$mail->SMTPAuth = false;
$mail->Username = $myEmail; // SMTP usr
$mail->Password = "****"; // SMTP pass
$mail->SMTPKeepAlive = true;
$mail->From = $patrickEmail;
$mail->FromName = "***";
$mail->AddAddress($email, $firstName . " " . $lastName);
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $client_subject;
$mail->Body = $client_msg;
// all above is copied
if($mail->Send()) {
sleep(5);
$mail->ClearAllRecipients();
$mail->AddAddress('another#email.com'); //some another email
}
...
Try with the following example.,
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address1 = "whoto#otherdomain.com";
$address2 = "whoto#otherdomain.com";
$mail->AddAddress($address1, "John Doe");
$mail->AddAddress($address2, "John Peter");
$mail->AddAttachment("images/phpmailer.gif"); // attachment if any
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if any
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Note : Better you can make a multiple user email and name as an ARRAY, like
<?php
$recipients = array(
'person1#domain.com' => 'Person One',
'person2#domain.com' => 'Person Two',
// ..
);
foreach($recipients as $email => $name)
{
$mail->AddCC($email, $name);
}
(or)
foreach($recipients as $email => $name)
{
$mail->AddAddress($email, $name);
}
?>
i think this may help you to resolve your problem.
I think you've got organizational problems here.
I recommend:
Set your settings (SMTP, user, pass)
Create new email object with info from an array holding messages and to addresses
Send email
Goto step 2