I am sending mails to two different persons, two different messages one for user and one for admin.
$message1='hello user'
$message2='hello admin'
$email = 'user#email.com'
$adminemail = 'admin#email.com';
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();
//message for admin
$mail->Body =$message2;
//$adminemail = $generalsettings[0]["admin_email"];
$mail->AddAddress($adminemail);
$mail->Send();
But as a user I am receiving the message twice.. How to send two different messages to two different users.
You need to clear the recipients list before you add the new address for the second message. If you don't do that, the first recipient will receive the second message as well:
...
$mail->Body =$message1;
$mail->Send();
//message for admin
// Remove previous recipients
$mail->ClearAllRecipients();
// alternative in this case (only addresses, no cc, bcc):
// $mail->ClearAddresses();
$mail->Body =$message2;
//$adminemail = $generalsettings[0]["admin_email"];
// Add the admin address
$mail->AddAddress($adminemail);
$mail->Send();
You can initiate phpmailer class two times.
$message1='hello user'
$message2='hello admin'
$email = 'user#email.com'
$adminemail = 'admin#email.com';
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();
This should work too:
$message1='hello user'
$message2='hello admin'
$email = 'user#email.com'
$adminemail = 'admin#email.com';
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();
$mail->ClearAddresses();
$mail->AddAddress($adminemail);
$mail->Body = $message2;
$mail->Send();
$mail->ClearAllRecipients(); // It removes old emails, After sending old emails use this code and again use addaddress() and send other users
Related
Good day. I need to send two different messages to two different people (user and admin). Tell me how to do this?
My mail php
<?php
require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$name = $_POST['name'];
$email = $_POST['email'];
$mail->isSMTP();
$mail->Host = 'smtp.mail.ru';
$mail->SMTPAuth = true;
$mail->Username = 'pmewilberries#mail.ru';
$mail->Password = '123456789456';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('pmewilberries#mail.ru');
$mail->addAddress($email);
$mail->Subject = 'Заявка на участие в интенсиве Wildberries';
// $mail->Body($body);
// $mail->isHTML(true);
$body = 'Hello'
$mail->msgHTML($body);
$mail->send()
?>
Send one message, change the properties that are different, then send the second one:
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->clearAddresses();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
It will be slightly quicker if you set keepalive, which will make it re-use the existing connection for the second message:
$mail->SMTPKeepAlive = true;
Try this,
$mail->addAddress($email);
$body = 'Hello';
$mail->msgHTML($body);
$mail->send();
$mail->ClearAllRecipients();
$mail->addAddress($email2);
$body = 'Hello2';
$mail->msgHTML($body);
$mail->send();
Look at the code for clearAllRecipients;
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();
I am using phpmailer in live server for sending emails for both sender and receiver. Its working fine but I want to include additional message in sender copy as "thanks for registering with us". I am unable to do it.
Could you please help with this ?
Code I tried So far:
<?php
$msg = "";
if (isset($_POST['submit'])) {
require 'phpmailer/PHPMailerAutoload.php';
function sendemail($to, $from, $fromName, $body) {
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Contact Form - Email';
$mail->Body = $body;
$mail->isHTML(false);
return $mail->send();
}
function sender_mail($to, $from, $fromName, $body) {
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Copy Contact Form - Email';
$mail->Body = $body . 'Thanks for registering with us';
$mail->isHTML(false);
return $mail->send();
}
$name = $_POST['username'];
$email = $_POST['email'];
$body = $_POST['body'];
sendemail('abc#domain.com', 'xyz#domain.com', $name, $body);
sender_mail('abc#domain.com', $email, $name, $body);
}?>
I always create different mail object for sending another email. So it will always pick new message.
For sending another email you can update like below:
$mail2 = new PHPMailer();
$mail2->setFrom($from, $fromName);
$mail2->addAddress($to);
$mail2->Subject = 'Copy Contact Form - Email';
$mail2->Body = $body . 'Thanks for registering with us';
$mail2->isHTML(false);
return $mail2->send();
2nd option to use clearAddresses above your second email to clear your earlier messages like below:
$mail->clearAddresses();
$mail = new PHPMailer();
$mail->setFrom($from, $fromName);
$mail->addAddress($to);
$mail->Subject = 'Copy Contact Form - Email';
$mail->Body = $body . 'Thanks for registering with us';
$mail->isHTML(false);
return $mail->send();
I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content,
"From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}
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);