How to do email form with multiple recipients and different body? - php

i have one contact form, when user submit all value will send(email) to admin.But now i want to do when user submit admin will receive the email and user also will receive an email but with different body.
here my previous code :
<?php
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
$name= $_POST["name"];
$email= $_POST["email"];
$phone= $_POST["phone"];
$company= $_POST["company"];
$message= $_POST["message"];
require_once('lib/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->AddReplyTo("admin#gmail.com","I Concept");
$mail->SetFrom('admin#gmail.com', 'I Concept');
$mail->AddReplyTo("admin#gmail.com","I Concept");
$address = "admin#gmail.com";
$mail->AddAddress($address, "I Concept");
$mail->Subject = "MY - Request a Quote";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
}
?>

Try the following. Didn't test but you basically need to get another PHPMailer object going and set the body and to information separately.
$address = "admin#gmail.com";
$mail->Subject = "MY - Request a Quote";
// keeps the current $mail settings and creates new object
$mail2 = clone $mail;
// mail to admin
$mail->AddAddress($address, "I Concept");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "<strong>Request a Quote from I Concept Malaysia Website</strong><br><br>
Name : $name<br>
Email : $email<br>
Phone : $phone<br>
Company : $company<br>
Enquiry : $message<br> <br>
Thank You!<br>";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!<br>";
}
// now send to user.
$mail2->AddAddress($email, $name);
$mail2->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail2->Body = "Separate email body for user filling form out.";
if(!$mail2->Send()) {
echo "Mailer Error: " . $mail2->ErrorInfo;
} else {
echo "Message sent!<br>";
}

Cloning the PHPmailer object is not necessary. Just use the ClearAllRecipients method that is built into PHPmailer before changing the body and sending the second email.

I'm sure you can't send different bodies in one SMTP call. However you can just send the first email and initiate a new PHPMailer.

Related

sending attachment fails with php mailer function codeigniter

Here I am trying to send email with an attachment, all codes are running fine but I didn't find any attachment along with the mail, but the same file is getting printed in hyper link that I had given with success message. I have used php mailer class in codeigniter.
public function sendmailto()
{
$this->load->library('phpmail');
$mail = new PHPMailer();
$body = "hello";
$mail->AddReplyTo("reply#mymail.com","First Last");
$mail->SetFrom('noname#mymail.com', 'First Last');
$mail->AddReplyTo("mail#mymail.com","First Last");
$address = "abcd#mymail.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("../../uploads/a.pdf"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!<a href='../../uploads/a.pdf' >click</a>" ;
}
}
I think its path issue you can try this
$attachment = base_url().'/uploads/a.pdf';
$mail->AddAttachment($attachment); // attachment
I think you should change
$mail->MsgHTML();
to
$mail->Body;

phpmailer doesn't send important information (email address entry)

Someone built this site for me and I have no idea about php and which part I should fix :S
Basically I have this single text entry form on my site which users can enter in their email address to that form and once they send submit I'll get the email address they input in.
The dev used phpmailer and at the moment when someone submitted their email address, I don't get any of their email address. The only entry/ email content I get is from "me" and a content of "Thank you. We'll update you as soon as possible."
This is the sendmail.php on the root folder:
<?php
$from = "ask#michaelfrieda2014.com";
if (isset($_REQUEST['email']) && $_REQUEST['email'] != '')
{
$email = $_REQUEST['email'];
sendMailSingup($from, $email);
sendMailSingup($from, "ask#michaelfrieda2014.com");
}
function sendMailSingup($from, $to)
{
include_once('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$body = "Thank you. We'll update you a soon as possible.";
$mail->From = "$from";
$mail->FromName = "";
$mail->Subject = "Thank you!";
$mail->AltBody = "Thank you!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("$to", "");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
#echo "Message sent!";
echo 1;
}
}
How do I make it so that I get the important information? (Their email address sent to my email). Am I missing something?
Thanks!
Below this line...
$body = "Thank you. We'll update you a soon as possible.";
add:
$body .= "<br><br>Sent from: $to";
(note the .= -- this will append to the $body variable)
If you want to fix the reply-to address, add this line:
$mail->AddReplyTo($to, $to);
before:
$mail->From = "$from";

for loop in phpmailer

I am trying to run the emails to 10 different users, i have made variable $friendsEmails into an array which contains 10 different emails, however looks that it will duplicate 10 for each email thats 10x10. am i doing something wrong?
for($i =0; $i<11; $i++){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$mail->Subject = "We wish you a merry Christmas";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($friendsEmails[$i], $friendsNames[$i]);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
No wonder you are sending multiple emails because in every iteration of your for loop you are just adding new addresses. Use PHPMailer::clearAllRecipients() to remove data from the previous iteration before adding a new email address.
for($i =0; $i<11; $i++){
$mail->SetFrom($email, $name);
$mail->AddReplyTo($email,$name);
$mail->Subject = "We wish you a merry Christmas";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($friendsEmails[$i], $friendsNames[$i]);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
$mail->clearAllRecipients(); // Clear all recipient types(to, bcc, cc).
}
It's easier to put the e-mail addresses in key value pairs in an array. So the key is the name of your friend and the value the e-mail address. And use an foreach loop to iterate over the whole array without having to determine how many items are in the array.
Oh and reinstantiate your mail object every loop, to not have it send each last e-mail as well (don't know for sure, but that's what could be happening)
Try something like this:
$friendsEmails = array('name' => 'email_address');
foreach($friendsEmails as $name => $email) {
$mail = new Mailer();
$mail->SetFrom($name);
$mail->AddReplyTo($name);
$mail->Subject = "We wish you a merry Christmas";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress($email, $name);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
Maybe at the end of every iteration you should clean your mail object.
Another option is to instantiate one different mail class at the beginning of the loop.

PHPMailer wont send to email with Hyphen IE-#blah-blah.com

I have a simple contact form that uses PHPMailer to send it's information to the admin. Everything works fine when the results are sent to an e-mail with out a hypen such as blah#blah.com. As soon as I change the address to an e-mail with a hypen like blah#blah-blah.com, I get this error.
"Could not instantiate mail function. Mailer Error: Could not instantiate mail function."
Is this a bug with PHPMailer?
Here's my code
<?php
require_once('class.phpmailer.php');
$question_for = $_POST['question_for'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company = $_POST['company'];
$comment = $_POST['comment'];
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("donotreply#blah-blah.com","DoNotReply");
$mail->SetFrom('donotreply#blah-blah.com', 'DoNotReply');
switch ($question_for) {
case "Sales":
$address = "Sales#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Sales";
break;
case "Service":
$address = "service#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Service";
break;
case "Career":
$address = "career#blah-blah.com";
$mail->AddAddress($address, "Blah");
$mail->Subject = "Message from Career";
break;
}
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("
Question For:".$question_for."<br />
Name:".$name."<br />
Phone:".$phone."<br />
Email:".$email."<br />
Company:".$company."<br />
Comment:".$comment."<br />");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header('Location: http://blah.com/contact/?sent');
}
?>
Checking even more, I see that this works
<?php
mail("test#blah.com", "Test Email", "Testing"); ?>
but this does not
<?php
mail("test#blah-blah.com", "Test Email", "Testing"); ?>

how to send email to (-pauline#vista.com.my-) and (-$email-)?

<?php
$full_name= $_GET["full_name"];
$email= $_GET["email"];
$telephone= $_GET["telephone"];
$option1= $_GET["option1"];
$option2= $_GET["option2"];
$message= $_GET["message"];
require_once('class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->AddReplyTo("pauline#vista.com.my","Vista");
$mail->SetFrom('pauline#vista.com.my', 'Vista');
$mail->AddReplyTo("pauline#vista.com.my","Vista");
$address = "pauline#vista.com.my";
$mail->AddAddress($address, "Vista");
$mail->Subject = "Vista";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "VISTA Eye Specialist Form <br><br>
Name : $full_name<br>
Email : $email<br>
Contact : $telephone<br>
Branches : $option1<br>
Services : $option2<br>
Comments : $message<br>
Thank You!<br>
";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Please Wait...!<br>
Name : $full_name<br>
Email : $email<br>
Contact : $telephone<br>
Branches : $option1<br>
Services : $option2<br>
Comments : $message<br>
Thank You!<br>
";
}
?>
<html>
<head>
<script>
<!--
timeout = '4000'; // milliseconds/1000th of a sec
window.onload = setTimeout(myRedirect, timeout); // ensure we load the whole page
function myRedirect() {
window.location = "index.php";
}
//-->
</script>
</head>
<body>
</body>
</html>
If you mean, how can you send an email to (-pauline#vista.com.my-) and some other email you specify - basically, more than one person. Just call the addAddress() method again.
$mail->AddAddress('myotheraddress#address.com', "The Name");

Categories