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();
Related
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
I am using Windows. When I try to send mail using php mailer. It returns "couldn't instantiate mail function". Is it some permission issue or is there anything wrong with code? Thanks!
<?php
require_once("class.phpmailer.php");//location is correct
require_once("class.smtp.php");//location is correct
$to_name = "Good Better";
$to = "sending#example.com";
$subject = "Mail Test at ".strftime("%T", time());
$message = "This is a test.";
$message = wordwrap($message,70);
$from_name = "My Name";
$from = "myacc#example.com";
$mail = new PHPMailer();
$mail->FromName = $from_name;
$mail->From = $from;
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->Body = $message;
$result = $mail->Send();
echo $result ? 'Sent' : $mail->ErrorInfo;
?>
I have a mail function in codeigniter which is sending xml data but in my mail its not showing in proper format
Here in the below code $result contains data in xml format.
public function send_mail($id,$result){
require_once 'phpmailer/PHPMailerAutoload.php';
$mailto = "test#gmail.com";
$name = "Testing";
$content= "Result - $result <br>";
$subject= "Test Mail - $id : ";
$mail = new PHPMailer;
$mail->isSendmail();
$mail->setFrom('admin#mywebsite.com', 'Tester');
$mail->addAddress($mailto, $name);
$mail->Subject = $subject;
$mail->msgHTML($content);
if ($mail->send()) {
return true;
}else{
return false;
}
}
Add this :
$mail = new PHPMailer;
$mail->IsHTML(true);
...
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);