I have the below code. I usually send an email to one recipient. My question is how can I modify for it to send to multiple recipients that are in my database.
Suppose I have a table called "tblemails" and a column called "email".
Note:
I also dont want to show all address to each member of my list (something like using Bcc).
Please Help?
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "sender#gmail.com";
$to = "receiver#gmail.com";
$subject = "Online order";
$message = "You just ordered";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
?>
I am sure you can fetch the email data from the database,
You can update header string as
$headers .= 'BCC: '. implode(",", $emailData) . "\r\n";
$emailData - Should be 1-D array which should contains all email ids. ref
you can use while loop
$a = mysqli_query($con,"SELECT email FROM table");
while($row = mysqli_fetch_array($a)){$email = $row['email'];mail($email,$subject,$msg,$header);}}
I hope you know how to fetch the email ids from the database.
$link = mysqli_connect("localhost", "root", "password", "DBName");
$sql = "SELECT * FROM tableA";
if ($res = mysqli_query($link, $sql)) {
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_array($res)) {
$recipients[] = $row['email'];
}
}
}
$to = 'some#email.com';
$bcc = implode(',', $recipients);
$headers .= 'From: noreply#company.com\r\n'; //here you can set sent from
$headers .= 'BCC: '.$bcc. '\r\n';
mail($to,$subject,$message, $headers);
Related
i am working on script to send a mail to a list of emails from my table using php
presently i run a query on the table and echo all the emails meeting such requirements from the sql query
sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql)or die("Cannot query orders_products data" . mysql_error());
while($row=mysql_fetch_array($result)) {
$list = array();
$list[] = $row['Email'];
echo implode(",",$list);
$email = 'xxxxxxxxxxx';
$emailto = implode( "," ,$list );
$subject = "xxxxxxxxxxxxxxxx";
$headers = "From: $email";
$body .= ++$i.") ".$row['title'] ."\n\n";
}
$body .= "Regards\n\n";
$body .= "xxxxxxxxxxxxxxxxxx\n\n";
$send = mail($emailto, $subject, $body, $headers);
when i try passing these emails to a mail function snippet the mail sending fails.
Please what could i be doin wrong, i need help
Update :
$list[] = $row['Email'];
echo implode(",",$list);
all the emails are displayed
But without commas.
$emailto = implode( "," ,$list );
i use the same method of imploding the array of data gotten from
$list[] = $row['Email'];
By fail i just mean i was hoping since the emails got echoed using the implode it would all successfully pass the emails to $emailto and then send a mail to each of them.
So, what I think you're trying to do is to get the email address from your table where the user is either a member or a client. Then, you have a from address, subject, and a body. In addition, it looks like maybe you're trying to add to the body some unique information. Finally, I think you want to send an individual email to each user, separately.
You could modify your code to be like this:
<?php
$sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql) or die("Cannot query orders_products data" . mysql_error());
$emailContent = 'xxxxxxxxxxx';
$emailSubject = 'xxxxxxxxxxx';
$body = 'xxxxxxxxxxxxxxx';
$headers = "From: from#email.com";
while ($row = mysql_fetch_array($result)) {
// optionally modify body here...?
$emailBody = $body .= $row['title']; // or whatever you're trying to do here...
// send email to each individual person
mail($row['Email'], $emailSubject, $emailBody, $headers);
}
I think this is how you'd modify your code to get it to do what you're asking.
How can I retrieve rows from database then use a mail() function to then email that list in my php file.
The query that I am using which this works:
$sql = "SELECT * FROM leads WHERE date_stamp BETWEEN '$currentdate' - INTERVAL 7 DAY AND '$currentdate'";`
But the problem I am having is listing out all the data to then email, I am only getting the one result from database, but not all the rows. I know its the way my loop is setup, that is what I am needing help with.
Below is the full code minus the database connect.
$to = 'email#email.com';
// email subject
$subject = 'Email leads beginning '.$newdate." through ".$currentdate;
// Construct email body
$result = mysqli_query($conn, $sql);
$recipients = array();
while($rows = mysqli_fetch_assoc($result)) {
foreach ($result as $row) {
$content = $row['first_name']." ".$row['last_name']."<br>";
}
};
$body_message = $content;
// email headers
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body_message, $headers);
Try that
$to = 'email#email.com';
// email subject
$subject = 'Email leads beginning '.$newdate." through ".$currentdate;
// Construct email body
$result = mysqli_query($conn, $sql);
$recipients = array();
$body_message = '';
while($row = mysqli_fetch_assoc($result)) {
$body_message .= $row['first_name']." ".$row['last_name']."<br>";
}
// email headers
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $body_message, $headers);
I want to send mass emails using PHP mail function. Email addresses are stored in MySQL database. There are nearly 30k email addresses. I am sending one email at a time.
60 emails are sent properly & the next all emails are skipped. I am using godaddy host to send emails. Following is code I am using
<?php
$con1=mysqli_connect("subscibe","subscibe","pw","subscibe");
$subject = $_POST['subject'];
$message = $_POST['message'];
$getusers = mysql_query("SELECT * FROM subscibe.emaillist");
$email_from = "email#gmail.com";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
while($result = mysql_fetch_array($getusers)) {
$emailaddress = $result['Emailaddresses'];
mail($emailaddress,$subject,$message,$headers);
//Add email address to temp table
$sqlq="INSERT INTO subscibe.temp VALUES ('$emailaddress')";
if (!mysqli_query($con1,$sqlq)) {
die('Error: ' . mysqli_error($con1));
}
}
echo "Emails are sent"
?>
you may want to use a foreach loop which would send the email to each record in your DB.
Something like:
$getusers = mysql_query("SELECT * FROM subscibe.emaillist");
foreach ($getusers as $maillist) {
$mail->to($mailist['email_address']);
$mail->send()
<?php
$con = mysql_connect("localhost","dbuser","dbpass"); // replace dbuser, dbpass with your db user and password
mysql_select_db("dbname", $con); // replace dbname with your database name
/*
To use this script database table must have three fields named sno, email and sub_status
*/
$query = "select sno, email from dbtable where sub_status = 'SUBSCRIBED'";
$result = mysql_query($query, $con);
$emails = array();
$sno = array();
while($row=mysql_fetch_assoc($result))
{
$sno[] = $row['sno']; // this will be used to unsubscribe the user
$emails[]=$row['email']; // email id of user
}
/* you can also get email id data from CSV using below code */
//$file = file_get_contents("travel_database.csv");
//$emails = explode(",",$file);
/* count.txt is used to store current email sent number/count */
$count = file_get_contents("count.txt");
for($i=$count;$i<count($emails);$i++)
{
$to = $emails[$i];
// subject
$subject = 'Set Your Title Here';
// message
$message = file_get_contents("sample.html"); // this will get the HTML sample template sample.html
$message .= '<p>Please click here to unsubscribe.</p>
</body>
</html>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
//$headers .= "To: $to" . "\r\n";
$headers .= 'From: Name <info#yourdomain.com>' . "\r\n";
//$headers .= 'Cc: sendcc#yourdomain.com' . "\r\n";
//$headers .= 'Bcc: sendbcc#yourdomain.com' . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers)) {
$file = fopen("mailsentlist.txt","a+"); // add email id to mailsentlist.txt to track the email sent
fwrite($file, $to.",\r\n");
fclose($file);
}
else
{
$file = fopen("notmailsentlist.txt","a+"); // add email to notmailsentlist.txt here which have sending email error
fwrite($file, $to.",\r\n");
fclose($file);
}
if(($i-$count)>=200) // this will send 200 mails from database per execution
{
$filec = fopen("count.txt",'w'); // store current count to count.txt
fwrite($filec, $i);
fclose($filec);
break;
}
}//for end
$filec = fopen("count.txt",'w'); // store fine count to count.txt this will be used as a start point of next execution
fwrite($filec, $i);
fclose($filec);
?>
i design an application and build with phonegap all is working but when a value in my mysql database that is attached the app is updated i want an email to be sent automatically to the user. When the counter completes counting the value of "status" in runnindAds table is updated and i want to an email to be sent each time that value is updated, below is the script i wrote for the email to be sent but i don't know how to automate it, please help with what i can should or i can
<?
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
mysql_query("update runningAds set status = 3 where id = '$id'");
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
sendConfirmationEmail($username, $stopTime);
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Try this:
<?php
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
if (mysql_query("update runningAds set status = 3 where id = '$id'"))
{
sendConfirmationEmail($username, $stopTime);
}
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Tell me if it works or not.
I want to send an email to someone that clicks the link having the information on them from my database. So they put in their username , password, and email and get the 'Item' and 'Aisle' sent to them. The problem is they can have multiple items under their username. So I need to echo all the information in one email. But its not possible to echo information in an email. Currently it sends an email for each item and aisle information found so it can send 2+ emails of information. Any help would be loved. Thanks!
$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);
$Loc = mysql_real_escape_string($_POST['Loc']);
$To = mysql_real_escape_string($_POST['To']);
$Subject = "List";
$query = mysql_query("select * from Members where Username = '$Username' and Password = '$Password'");
while ($row = mysql_fetch_array($query)) {
$headers = 'From: email#email.com';
$Items = $row['Items'];
$Loc = $row['Loc'];
$msg= "Item: $Items
Aisle: $Loc\n";
mail($To, $Subject, $msg, 'From:' . $header);
echo 'Email sent to: ' . $To. '<br>';
Changing the 2nd part of your codes as below:
$query = mysql_query("select * from Members where Username = '$Username' and Password = '$Password'");
$items = '';
$headers = 'From: email#email.com';
while ($row = mysql_fetch_array($query)) {
$items .= $row['Items'] . PHP_EOL;
$loc = $row['Loc']; // what is Loc ?
}
$msg= "Item: $items
Aisle: $loc\n";
mail($To, $Subject, $msg, 'From:' . $header);
echo 'Email sent to: ' . $To. '<br>';
However, your table structure is weird. You should put your items in a separate table with your member ID as foreign key.
Try placing the message in a separate file and include it in the message.
like this.
$message = include('email_massage.php');