i am trying to send bulk email using phpmailer i have more than 10 email id's in my database when i click on send button then first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on. this is my coding please help me
<?php
$body=$_POST['message'];
$subject=$_POST['sub'];
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once("class.phpmailer.php");
//include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->CharSet = "big5";
$mail->Username = "abc#gmail.com"; // GMAIL username
$mail->Password = "**********"; // GMAIL password
$mail->SetFrom("abc#gmail.com", ''); // set reply id
$mail->Subject = ($subject); // subject
$mail->MsgHTML("$body"); // message
$mail->AddAddress($address, "abc");
$con=mysql_connect("localhost","root","") or
die("could not connect:".mysql_error());
mysql_select_db("bulkemail");
$qry=mysql_query("SELECT * FROM email_id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
while($row = mysql_fetch_array($qry))
{
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
$mail->send();
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
}
?>
Remove $mail->send(); and move if(!$mail->Send()) to outside the while($row ..) loop
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
} // end the while loop
// remove $mail->send(); as it is a duplicate of if(!$mail->Send())
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
I had used phpmailer for mass mailing and I had same problem.
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
//imo if should be in the while loop.
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
//I added this function down there so that old address would be removed but in the new loop, new one will be added.
$mail-> ClearAddresses();
}
} // end the while loop
This works for me.
In your case, you are adding a new BCC to the list of all BCC already send in every repeat of the loop.
Like "kworr Sep 27 '13 at 18:36 " said, you need to create new instance for every sending in the loop.
Or you need to put
$mail->send();
out of the loop.
Related
I'm having trouble when trying to set an email dynamically from a GET variable.
I've already tried converting to string, but I keep getting: "Mailer Error: You must provide at least one recipient email address."
$destinationEmail = $_GET["email"];
//$destinationEmail = (string)$_GET["email"]; ----> already tried this.
function sendEmail($pdf,$data){
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("Noreply#email.com");
$mail->Subject = "Subject";
$mail->Body = "Hello";
$mail->AddAddress($destinationEmail);
$mail->addStringAttachment($pdf,"pdf.pdf");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
if ( $_GET["email"] ){
sendEmail($pdf,$data);
}
Any help will be highly appreciated. Thanks in advance!
The sendEmail() function doesn't know about the email address because that variable is out of scope. You need to pass the $destinationEmail variable to your sendEmail() function:
function sendEmail($pdf,$data,$destinationEmail){
...
}
And then add the email address to the function call:
if ( $_GET["email"] ){
sendEmail($pdf,$data,$destinationEmail);
}
How can I redirect when the email is being sent with PHPMailer?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
// $mail->Host = "email-smtp.us-east-1.amazonaws.com";
$mail->Host = "email-smtp.us-west-2.amazonaws.com";
$mail->Username = "AKIAIVSF45PCGR7NZWNQ";
$mail->Password = "Am2SBg4vluOvIc1+kycsWpCnxtf3jhGjYCAdBv7YYp/y";
//
$mail->SetFrom('test#gmail.com', 'Z-Reports'); //from (verified email address)
$mail->Subject = "Z-Reports (sent via smtp)"; //subject
//message
$body = emailZReports($total_sales, $inventory);
// $body = eregi_replace("[\]",'',$body);
$mail->MsgHTML($body);
//
//recipient
$mail->AddAddress("test#gmail.com", "Z-Reports");
//Success
if ($mail->Send()) {
echo "Message Sent!";
}
//Error
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
How can I redirect to a specific page instead of displaying Message Sent!?
if you are using SMTP, you can't redirect to another page if $mail->SMTPDebug is not set to 0. This is because it sends out headers which you can't modify with header("Location: path/to/redirect"). Set $mail->SMTPDebug = 0; so that you can redirect to another page after $mail->send(). but you have to do that only when you have finished debugging and your mail is sending successfully.
You can use php header function
//Success
if ($mail->Send()) {
header("Location:Yourfile.php");//echo "Message Sent!";
}
Make sure there is not echo or any output before this function
You can use like this,
See Header for more info.
It can be like this,
//Success
if ($mail->Send()) {
header('Location: nextpage.php');
}
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.nextpage.com/"
This works
if(!$mail->Send()) {
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} //echo getcwd();
header("Location:index.php") //redirects to a page named index.php;
// make another script for redirecting
<?php
require_once("mail_function.php");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
header("Location: form.php");exit; //redirect to form submit page
} else {
echo "Message sent!";
header("Location: index.php");exit; //redirect to your home page
}
?>
//mail_function.php
<?php
require 'class/class.phpmailer.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Mailer = "smtp";
$mail->SetFrom("xxxx#gmail.com", "web");
$mail->Port = '587'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication.
$mail->Username = 'xxxx#gmail.com'; //Sets SMTP username
$mail->Password = 'xxxx'; //Sets SMTP password
$mail->SMTPSecure = 'tls'; //Sets connection prefix.
$mail->FromName = $_POST["name"];
$mail->AddAddress($email); //Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "somthing";
$mail->MsgHTML($message_body);
$mail->Body = "somthing";
$result = $mail->Send();
return $result;
?>
I am dealing with something strange..
I am setting phpmailer and if I have an error I get the error normally
echo "Mailer Error: " . $mail->ErrorInfo;
actually if everything is good, the page loads a while and then it stops loading, getting in chrome the error: ERR_EMPTY_RESPONSE (Impossible to load the page because the server didn't load the data)
This is the content
<?php
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 645;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myemail#gmail.com"; // SMTP username
$mail->Password = "my password"; // SMTP password
$email = 'myemail#gmail.com';
$mail->From = $email;
$mail->AddAddress("to#example.com", "Name");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject of the mail";
$mail->Body = "content";
$mail->AltBody = "content";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
of course I have included this files:
require_once('../library/class.phpmailer.php');
require_once('../library/PHPMailerAutoload.php');
Two mistakes - just load the autoloader, that loads the class for you so that's all you need.
You've set Port = 645; I suspect you meant 465.
For gmail you should follow the example from the docs: use Port = 587 and SMTPSecure = 'tls'.
you need to Upload the PHPMailer to 6.5.
beacause the php 7.x it's diferent
look that the code change a little bit
I am using PHPMailer to send emails.
When I execute the code that sends an email to X#Y.Z, I still receive this email, not only onetime, but each time I refresh my inbox I receive many copies of it!
Note: I execute the code one time only!!
So what's wrong?
this is my code:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = "bla blas";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "xxx.xxx.xxx.xxx";
$mail->Port = 25;
$mail->Username = "****";
$mail->Password = "****";
$mail->SetFrom('z#z.z');
$mail->AddReplyTo("z#z.z");
$mail->Subject = "test";
$mail->MsgHTML($body);
$mail->AddAddress('X#Y.Z');
if(!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
} else {
//echo "Message sent!";
}
Thanks!
I'm trying to send two versions of the same email to two recipients using phpMailer
Depending on the email I need to send one version or another.
At the moment I'm able to send only the same version to both email address
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx#xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx#xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first#email.xxx", "second#email.xxx");
foreach($add as $address) {
if($address == "first#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first#mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.
I think you want to seach the $maileremail inside $add and send the desired email.
$add = array("first#email.xxx", "second#email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
EDITED:
I misunderstood the question. Brian is right.