Send email to all email address from MySQL using PHP - php

I am trying to send email to All users from Mysql Database. For this I have written below Code, but it is sending email to First record only. What's wrong I am doing. I need to send email to all users.
if ($_POST['do'] == 'mail') {
$result = $db->query("SELECT email FROM members WHERE status='Active'");
$input="This is a text message";
$userdetails = $db->fetch_array($result);
$emails = implode(",", $userdetails);
$message = $input;
$mail = new mail();
$mail->setFrom($settings['email_support'], $input->pc['name']);
$mail->addTo($emails);
$mail->setSubject('subject text!');
$mail->setBodyText($message);
$mail->send();
}

It seems your code perfect. It can be possible you are fetching only 1 row. If not, then try this it will help you.
if ($_POST['do'] == 'mail') {
$result = $db->query("SELECT email FROM members WHERE status='Active'");
$input = "This is a text message";
foreach ($result as $row) {
$message = $input;
$mail = new mail();
$mail->setFrom($settings['email_support'], $input->pc['name']);
$mail->addTo($row['email']);
$mail->setSubject('subject text!');
$mail->setBodyText($message);
$mail->send();
}
}

Related

How to stop PHP Mailer from appending all email address on every email?

I've been trying many different ways to prevent PHP Mailer from appending every email address coming from my DB to every single email.
I feel it doesn't look right for every user to receive an email from my website and being able to see a few other thousand emails appended to that same email as well.
Here's my code, I already tried to many diferent things:
$query_select = "SELECT * FROM users";
$query = mysqli_query($connection, $query_select);
if($select_to == 'all'){
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
}
} else {
if($select_to == 'single'){
$send_to = $to_single_email;
}
}
if($mail->send()){
$confirm = 'Sent.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
} else {
$confirm = 'There was en error while sending this email.';
$send = array(
'confirm'=>$confirm
);
echo json_encode($send);
}
$mail->send() will send to all addresses via a single message. Instead you need to send one message per recipient.
The while() loop would be refactored similar to:
while($row = mysqli_fetch_assoc($query)) {
$user_email = $row['user_email'];
$send_to = $user_email;
$mail->setFrom('example#gmail.com', 'My website');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = $subject_email;
$mail->Body = $content_email;
$mail->AltBody = $content_email;
$mail->send();
$mail->clearAllRecipients();
}
The call to clearAllRecipients() will clear out the previous addresses that would otherwise accumulate via addAddress()
You'll also need to add the error checking within the loop for each call of send()

Email not sending to one particular domian

We are using amazon server and I am using PHP, Codeigniter framework.
Below is the script which I am using for sending emails.
It is working when I send an email to Gmail domain email id (abc#gmail.com) but it is not working on some specific domain name email id (abc#xyzdmainname.com).
public function sendMail() {
$to = 'abc#xyzdomain.com';
$fromEmail = 'pqr#gmail.com';
$fromName = 'pqr';
$subject = 'Testing';
$message = 'Testing of email';
$newFile = ''
$this->CI->load->library('email');
$this->CI->email->mailtype = 'html';
$this->CI->email->from($fromEmail, $fromName);
$this->CI->email->to($to);
$this->CI->email->subject($subject);
$this->CI->email->message($message);
if (!empty($newFile)) {
$this->CI->email->attach($newFile);
}
$result = $this->CI->email->send();
$message = '';
if ($result) {
return 1;
} else {
echo $this->CI->email->print_debugger();
return 0;
};
Please let me help what-what case may be here for fail sending email on a particular domain.
and How I can overcome this issue.

cannot send email to multiple recepients using phpmailer

I'm trying to send an email to multiple recipients using phpmailer and getting the emails list from my table in the database, however, whenever I make a request only one recipient, the first one on the list gets the email. *I know this might be marked as duplicate but I couldn't find a similar example using mysql
<?php
if (isset($_POST['submit'])) {
if (empty($errors)) {
$thisType = mysql_prep($_POST["partner_type"]);
$content = $_POST["content"];
$header = $_POST["header"];
$query = "SELECT * FROM partners_list WHERE partner_type = '$thisType' ";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)){
require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/class.smtp.php");
require_once("phpMailer/language/phpmailer.lang-uk.php");
$to_name = "Order";
$subject = $header;
$message = $content;
$message = wordwrap($message,70);
$from_name = "thisCompany";
$from = "noreply#thicCompany.com";
$mail = new PHPMailer();
$mail->AddAddress($row['email'], "Test Message"); // add each DB entry to list of recipients
$mail->FromName = $from_name;
$mail->From = $from;
$mail->Subject = $subject;
$mail->Body = $message;
$result = $mail->Send();
// Success
$_SESSION["message"] = "e-mail successfully sent.";
redirect_to("successpage.php");
}
}
} else {
}
?>

phpmailer with mysql results

I'm trying to use phpmailer to send out emails to each email address found in the database, but as a unique email. For some reason, it's sending duplicate emails, and it sends it out in as many copies as my query returns rows. So, if my query returns 5 rows, each recipient will receive 5 email (total emails sent is 25). I can't use the same email for multiple recipients because the email content is personalized.
What am I doing wrong with my code? Please help...
Here's my code:
$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";
$customers = mysql_query($customers_query);
if (!$customers) {
$message = 'Error notice: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
require_once('class.phpmailer.php');
// create an instance
while ($row = mysql_fetch_array($customers)) {
$email = $row['customer_email'];
$name = $row['customers_name'];
$id = $row['customer_id'];
$mail = new PHPMailer();
// use sendmail for the mailer
$mail->IsSendmail();
$mail->SingleTo = true;
$mail->IsHTML(true);
$mail->From = "noreply#domain.com";
$mail->FromName = "MyWebsite";
$mail->Subject = "Welcome to MyWebsite";
$mail->AddAddress($email);
$mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
$mail->Send();
}
So, what am missing here? Why does it send that many emails?
Try this:
$mail->ClearAddresses(); after $mail->Send();
Try something like is below, you need to be counting your rows somehow so that it doesn't re-process them. Also, the AddAddress(); function is used to keep adding email addresses to the TO: field, so you need to call ClearAddresses(); in order to restart with a fresh set of recipients.
$customers_query = "SELECT customer_name, customer_email, customer_id FROM customers";
$customers = mysql_query($customers_query);
$count = mysql_num_rows($customers);
$result = mysql_fetch_array($customers);
$i = 0;
if (!$customers) {
$message = 'Error notice: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
require_once('class.phpmailer.php');
// create an instance
while ($i < $count) {
$email = mysql_result($result,$i,"customer_email");
$name = mysql_result($result,$i,"customers_name");
$id = mysql_result($result,$i,"customer_id");
$mail = new PHPMailer();
// use sendmail for the mailer
$mail->IsSendmail();
$mail->SingleTo = true;
$mail->IsHTML(true);
$mail->From = "noreply#domain.com";
$mail->FromName = "MyWebsite";
$mail->Subject = "Welcome to MyWebsite";
$mail->AddAddress($email);
$mail->Body = "Dear ".$name.", welcome to MyWebsite. Your ID is: ".$id.". Enjoy your stay.";
$mail->Send();
$mail->ClearAddresses();
$i++;
}
You can do one more thing, add one more extra field in the customer table, like is_email_sent, yes or no. Once sent email you can update specific row using primary key. so it will not send the same email to multiple time..

PHP script: send-to another e-mail adres with SMTP

Currently I am using the following script. This script will send an email to email1, but not always.
When somebody adds the second email2, the email only goes to number 2.
I want to change the script to sent to both emails (1 and 2) when email2 exists.
Tried a lot of things, but can't get it right.
// Email
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email2;
// Tried this below and a lot more
if(!$row->email2){
$email = $row->email1;
} else{
$email = $row->email1;
$email = $row->email2;
// E-mail to
if(!is_array($email)){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
} else{
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
$emails = array();
if($email = $row->email1) {
$emails[] = $email;
}
if($email = $row->email2) {
$emails[] = $email;
}
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
}

Categories