Mail Sending to multiple recipient without sharing the email id using php - php

I have a send button who's id is 'btnEmail'.My code is sending mail to only the specified email.I want to send it to multiple people without sharing the email id.My code goes here:
<?php
if(isset($_POST['btnEmail']))
{
require_once("../include/phpmailer/class.phpmailer.php");
$mail1 = new PHPMailer(); // create a new object
$mail1->IsSMTP(); // enable SMTP
$mail1->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail1->SMTPAuth = true; // authentication enabled
$mail1->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail1->Host = "smtp.gmail.com";
$mail1->Port = 465; // or 587
$mail1->IsHTML(true);
$mail1->Username = "some_mail#gmail.com"; // SMTP username
$mail1->Password = "Sup3rS3cr3T"; // SMTP password
$mail1->SetFrom("some_mail#gmail.com");
$mail1->Subject = "Testing mail for php";
$mail1->Body = "<html><h4>Tested Successfully</h4></html>";
$mail1->AddAddress('another_mail#gmail.com');
}

You seem to be using phpmailer. Have you checked the examples it gives?
https://github.com/Synchro/PHPMailer/blob/master/examples/mailing_list.phps
This should be present on your computer or server. You can also read the documentation, it is helpful.

Related

PhPMailer - how to force gmail to show the name argument from the SetForm?

I use PhpMailer and Google apps (with a simple contact form). Let's assume my main email (set up with Google apps) is info#company.com. The PhPMailer code is standard, like below:
<?php
include "classes/class.phpmailer.php"; // include the class name
$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 = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; // or 587
$mail->IsHTML(true);
$mail->Username = "info#company.com"; //MX records set up to use Gmail servers
$mail->Password = "xxxxxxxxx";
$mail->addReplyTo($_POST['email]);
$mail->SetFrom("info#company.com","Company Name");
$mail->AddAddress("help#company.com");
$mail->Subject = $_POST['subject];
$mail->Body = $_POST['body];
$mail->Send());
?>
My problem is - in Gmail I always see such submitted emails sent from 'me' (ie. from info#company.com). But it is not convenient because I want to see some other unique info about the sender. Obviously, changing this part above to:
$mail->SetFrom($_POST['email]);
would be considered email forgery and cannot be used any more with Gmail STMP. HOWEVER, why this doesn't work (ie. doesn't change the name associated with email):
$mail->SetFrom('info#company.com', $_POST['email]);
? After all, isn't the second argument in the SetForm the receipient's name? Why gmail doesn't list this name and it continues to list 'me' as the sender?
I even tried this:
$mail->SetFrom('info#company.com', 'Customer Request');
and in Gmail it is still listed as 'me' - why isn't it listed as 'Customer Request'? Name shouldn't be considered as 'forged'?
Is there a way to force to show name from the SetFrom instead of 'me' in Gmail?
I attach a screenshot of where the 'me' appears (it appears on the main gmail tab).

php email, code reuse fails on second include

I posted earlier as an email and relocation issue, however after trying several things, and tracing (in production! yech!)
I have found the culprit - but have NO IDEA why or where to go from here.
you will see in the below code I include 'email.php' twice - because one receipt goes to the user, the other includes some meta data about the user and goes to support...
if i comment out the SECOND include, my redirect works, if i leave it in, it bombs...
the notify email is valid, and is the only thing that is different.
Im at a loss
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
//I make it this far in my trace - then nothing
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
It's the require in email.php that is causing the problems. If you want to make the least change possible and make it work, change it to require_once. This will ensure "phpmailer/class.phpmailer.php" is only loaded once, therefore the class will only be defined once.
take the require() line from email.php and put it in the calling file

phpmailer, email behavior mystery

I have been in PHP/email 'hell' - I got close, and can't seem to get to the 'finish line'....
Om using phpmailer to send support requests in a clients site. my process looks like this:
FORM -> PROCESS (generate feedback message AND cc message to support) -> mail to sender -> mail to support -> redirect to thank you page.
the issue is two fold:
1) the emails go thru as expected IF i have debuging turned on, but I get the debug AND no redirect
2) if I turn off debug - the email DOES NOT go out AND I get a blank page - with NO redirect
* addendum *
The emails just came thru - so it's ONLY a redirect problem... either with or without debug, My meta refresh does not get sent - maybe there's a better way????
PHP FORM PROCESSOR
...
// send two emails
$_emailTo = $email; // the email of the person requesting
$_emailBody = $text_body; // the stock response with things filled in
include ( 'email.php' );
$_emailTo = $notifyEmail; // the support email address
$_emailBody = $pretext.$text_body; // pretext added as meta data for support w/ same txt sent to user
include ( 'email.php' );
// relocate
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php" >';
exit;
PHP MAILER (email.php)
<?php
require 'phpmailer/class.phpmailer.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = "mail.validmailserver.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 26;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "validusername";
//Password to use for SMTP authentication
$mail->Password = "pass1234";
//Set who the message is to be sent from
$mail->SetFrom('me#validmailserver.com', 'no-reply # this domain');
//Set an alternative reply-to address
//$mail->AddReplyTo('no-reply#validmailserver.com','Support');
//Set who the message is to be sent to
$mail->AddAddress( $_emailTo );
$mail->Subject = $_emailSubject;
$mail->MsgHTML( $_emailBody );
$_emailError = false;
//Send the message, check for errors
if( !$mail -> Send() ) {
$_emailError = true;
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
help - please
Your problem may be that some output has already been sent to the browser before the redirect is attempted. You can't normally do a redirect in that situation. If that's the case you may be able to use output buffering as in the following example:
ob_start();
//statements that output data to the browser
print "some text";
if (!headers_sent()) {
header('Location: /success.php');
exit;
}
ob_end_flush();
This can also be turned on by default in the php.ini file with the output buffering directive, in which case you won't need the ob_start() and ob_end_flush() statements. My php.ini file has this:
output_buffering = 4096

Send multiple emails from a database using PHP

I need help in sending multiple emails from database using PHP. I have a code that work, but it can only allow one email in it. Is there some way to modify it to help me send multiple ones?
<?
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
//Gmail configuration
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "******#gmail.com"; // GMAIL username
$mail->Password = "785123nick"; // GMAIL password
$prize = "lol";
//End Gmail
$mail->From = "from#email.com";
$mail->FromName = "Jetstar";
$mail->Subject = "Order Redemption";
$mail->MsgHTML("You have bought " . $prize . " Print this and collect it at our office.");
//$mail->AddReplyTo("reply#email.com","reply name"); //They answer here, optional
$mail->AddAddress("your-email","name to");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) { //To see if we return a message or a value bolean
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
echo "Message sent!";
?>
Assuming that you would like to send same email to multiple recipients and that your email addresses are stored in a database, you may do something like this:
read email addresses from your database table
loop through the email addresses and pass each email address to $mail->AddAddress();
This way you can add multiple email addresses to your mail object and then send to all.
Hope it helps!
Using phpmailer, you may add multiple recipients simply calling addAddress multiple times...
Obviously, as suggested before, you may want to call this script by mean of a cronjob, thus respecting limits imposed by the mail server.

PHPMailer setting for individual emails is sending empty To: fields

Using PHPMailer to send individual emails to recipients I'm getting nothing in the To: field when I add $mail->SingleTo = TRUE; to my code.
When I remove $mail->SingleTo = TRUE; I receive emails with an email address in the To: field that is correct.
This is what I get:
reply-to xxxxxx <xxxx#xxxx.com>, No Reply <no-reply#no-reply.com>
to
date Mon, Mar 21, 2011 at 5:07 PM
subject Testing
mailed-by gmail.com
signed-by gmail.com
(where xxxxxxx represents my email address.)
Here's my code:
if(isset($_POST['submit']))
{
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer();
$subject = $_POST['subject'];
$body = $_POST['emailbody'];
$to = $_POST['to'];
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // 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->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
// After adding this line I'm getting an empty To: field
$mail->SingleTo = TRUE;
$mail->AddAddress("address1#xxxxxx.com", 'xyz abc');
$mail->AddAddress("address2#xxxxxx.com", 'abc xyz');
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
$message= "Mailer Error: " . $mail->ErrorInfo;
}
else
{
$message= "Message sent!";
}
}
You are using SMTP to send email. The phpmailer class is not using the SingleTo parameter when senting using Smtp.
More, if you see the CreateHeader function when the SingleTo == true the recipents are only aded to $this->SingleToArray and not to the header itself so basically it PHPmailer bug.
Looks like the only choice, unless you want to patch phpmailer, is to send email one-by-one without using SingleTo property
the solution will be
function & prepare_mailer($subject, $body) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "localhost"; // 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->SMTPSecure = "SSL"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxxxxxxx#gmail.com"; // GMAIL username
$mail->Password = "*********"; // GMAIL password
$mail->SetFrom('xxx#xxx.com', 'XXXXXX');
$mail->AddReplyTo("no-reply#xxxxx.com","No Reply");
$mail->Subject = $subject;
$mail->MsgHTML($body);
return $mail;
}
foreach( $_POST['to'] as $to ){
$mail = null;
$mail = & prepare_mailer($_POST['subject'],$_POST['body']);
$mail->AddAddress($to['address'], $to['name']);
$mail->Send();
}
Set up all the other parameters, then in a loop set the recipient, send the email, then use the ClearAllRecipients() function. Like so:
{ // loop start
$mail->AddAddress($person_email, $person_name);
$mail->Send();
$mail->ClearAllRecipients();
} // loop end
The problem is in the SmtpSend function (starting at line 701 in class.phpmailer.php). As you can see there, they don't take the singleTo setting into account, and just add all recipients and send the body of the message once.
So you should add some logic there to test if singleTo is true, and if it is modify the code so it sends these mails separately, prefixing the To: header to the body of the message. If singleTo is false you can call the code as-is (line 713 - 758).
Or alternatively, if you don't want to patch things, then you could use a transport method (ie. sendmail) that does seem to support the singleTo parameter.
$mail->AddAddress()
doesn't like CSV's
so if you have:
$Emails="addr1#host.com,addr2#host.net"; #etc;
do a for loop after a split:
$NewList = preg_split("/,/",$Emails);
foreach ($NewList as $k=>$email){
if ($k==0) $mail->AddAddress($email); # Add main to the "To" list.
else $mail->AddCC($email); # The REST to CC or BCC which ever you prefer.
}
-- BF.
SingleTo Is not a good idea. It only works with "sendmail" or "mail" transports, not with SMTP. If you use SingleTo with SMTP, this parameter is just ignored without any error or warning, and you may get duplicates.
Since you use both SingleTo an SMTP, as shown in your code, the SingleTo in your case is ignored.
The SMTP protocol is designed in a way that you cannot send one message to several different recipients, each having only its own address in the TO: field. To have each recipient have only its name in the TO:, the whole message have to be transmitted again. This explains why SingleTo is incompatible with SMTP.
According to the authors of the PHPMailer library, SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0. The authors have explained that it's better to control sending to multiple recipients at a higher level, since PHPMailer isn't a mailing list sender. They tell that the use of the PHP mail() function needs to be discouraged because it's extremely difficult to use safely; SMTP is faster, safer, and gives more control and feedback. And since SMTP is incompatible with SingleTo, the authors of PHPMailer will remove SingleTo, not SMTP.

Categories