PHPmailer is sending copy to my email - php

When I'm sending a test message via PHPmailer (SMTP) my email appends to the recepients list. Here is what recipient sees in inbox email
To: mail#mail.com, Name <mail2#mail.com>
The second email is mine. How can I stop this?
Here is my code
function send_email($to, $fromName, $subject, $message, $contentType='text', $smtp_opts) {
$mail = new PHPmailer();
$mail->SetFrom($smtp_opts['fromEmail'], $fromName);
$mail->Subject = $subject;
$mail->Mailer = 'smtp';
$mail->AddAddress($to);
$mail->CharSet = "UTF-8";
$mail->IsHTML($contentType=='html');
$mail->Host = $smtp_opts['host'];
$mail->SMTPAuth = (bool)$smtp_opts['auth'];
if ($mail->SMTPAuth) {
$mail->Username = $smtp_opts['username'];
$mail->Password = $smtp_opts['password'];
}
$mail->Body = $message;
$mail->AddAddress($smtp_opts['fromEmail'], $fromName);
$result = $mail->Send();
$mail->ClearAddresses();
$mail->ClearAttachments();
return $result;
}
$smtp_opts = array( ... ); // host, port, fromEmail, auth, username, password
send_email('mail#mail.com', 'Name', 'Subj', 'Msg', 'html', $smtp_opts);

$mail->AddAddress($smtp_opts['fromEmail'], $fromName);
If I am not mistaken, this command adds another recipient to the list of recipients. Try to remove it and send another test E-Mail. You shouldn't be getting the copy-mail then.

I think the problem is in this line
$mail->AddAddress($smtp_opts['fromEmail'], $fromName);

Related

PHPMailer sometimes send mails sometimes not.use gmail smtp

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();

PHPMailer not sending all emails to last email address

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.

One mail cannot be send while using PHPmailer to send two mails

I am trying two send two mails using PHPMailer and i am getting one mail, But i am not getting the second mail(the user acknowledgement mail). Could some one help me out with this
include_once("mail/class.phpmailer.php");
$mail = new PHPMailer(); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
//$mail->isSMTP(); //Set mailer to use SMTP(for live server remove or comment this line)
$mail->Host = 'mail.****.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '********'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'TSL'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->From = $email_from;
$mail->FromName = $first_name;
$mail->addAddress('********#abc.com'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$txt='You got a mail from :<br>';
$mail->Body =$txt;
$mail->Send();
$mail->ClearAllRecipients();// Remove previous recipients
$mail->ClearAddresses();
$mail->From = "********#abc.com";
$mail->FromName = "Some Name";
$mail->AddAddress("********#abc.com");
$mail->IsHTML(true);
$thank_mesg = "Thank You For Sending Us Mail, We Will Reach You As Soon As Possible";
$mail->Subject = "Enquiry From xxxxxxx";
$mail->Body = $thank_mesg;
$mail->send();
Here is a some advice to fix your code:
first, you can use two instance of phpMailer class, Like:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isHTML();
$mail->IsSMTP();
$mail->setFrom('admin#mysite.com', 'admin site');
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->Body = $message1;
$mail->Send();
$mail2 = new PHPMailer(true);
$mail2->isHTML();
$mail2->IsSMTP();
$mail2->setFrom('admin#mysite.com', 'admin site');
$mail2->AddAddress($adminemail);
$mail2->Subject = $subject;
$mail2->Body = $message2;
$mail2->Send();
second, some providers impose restrictions on the number of messages that can be sent within a specific time so use sleep() function to check if it is worked or not.
$txt='You got a mail from :<br>';
$mail->Body =$txt;
$mail->Send();
sleep(10); // <<<--------------add this line - in second;
$mail->ClearAllRecipients();// Remove previous recipients
$mail->ClearAddresses();
$mail->From = "********#abc.com";
$mail->FromName = "Some Name";
$mail->AddAddress("********#abc.com");
$mail->IsHTML(true);
$thank_mesg = "Thank You For Sending Us Mail, We Will Reach You As Soon As Possible";
$mail->Subject = "Enquiry From xxxxxxx";
$mail->Body = $thank_mesg;
$mail->send();

How to fix send different emails to different people PHPMailer

I am going to send different emails to different people.
First email sent normal but second one is waiting for 180 sec and then start to send. I couldn't find any default settings. Once sent an email others are going to POOLING and failed it.
I have different bodies and different subjects.
code 1:
sendEmail(false, $email, $message, $subject, $dep_type);
sendEmail(true, $email, $message_client, $subject_client);
sendEmail function :
function sendEmail($client, $email, $message, $subject, $dep_type = null)
{
$from_mail = 'hello#example.com';
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->From = $from_mail;
$mail->FromName = "SenderName";
if ($client) {
$mail->addAddress($email);
} else {
$mail->addAddress('welcome#example.com');
}
$mail->addReplyTo($from_mail, 'name');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
}
https://aws.amazon.com/de/premiumsupport/knowledge-center/ec2-port-25-throttle/
Amazon EC2 throttles traffic on port 25 of all EC2 instances by
default, but you can request for this throttle to be removed.

How to send email using google server in my site

Now we are using our own server to send email to our customers. its possible to send email using google server. how to do this. explain with php codes
Download PHPMailer from http://phpmailer.sourceforge.net
Extract to folder phpmailer
Create a file email.php
Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username#gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username#doamin.com"; //Reply to this email ID
$email="username#domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
function email($to, $subject, $body){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->SetFrom("anything#domain.com", "Any Thing");
if(is_array($to)){
foreach($to as $t){
$mail->AddAddress($t);
}
}else{
$mail->AddAddress($to);
}
$mail->Subject = $subject;
$mail->Body = $body;
$mail->Send();
unset($mail);
}
Download http://phpmailer.sourceforge.net/ and name it "class.phpmailer.php"
http://micrub.info/2008/09/22/sending-email-with-zend_mail-using-gmail-smtp-services/
http://stackoverflow.com/questions/36079/php-mail-using-gmail

Categories