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);
?>
Related
I want to send mail (only once) using php when $cstt == 10. I want to prevent sending mail on every time I refresh the page. here is my code
if ($cstt == 10) {
echo "Great";
// SENDING EMAIL
$to = "user#example.com";
$subject = "$tracking_id_user is in transit.";
$message =' Hi user, Simple html message. Best Regards!';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Company <info#company.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
echo "mail send";
}else{
echo "email not sent";
}
}
I did the following tasks to achieve the result.
Created a Table mail_record.
added id and mail_sent columns.
after creating the table, wrote the following code.
Connectin to database
$conn = new mysqli('localhost', 'user_id', 'password', 'databasename');
Accessing the data
$sql = "SELECT * FROM mail_record";
$query = $conn->query($sql);
$row = $query->fetch_assoc();
Accessing database to get mail_sent record.
// GETTING MAIL SENT / NOT RECORD (BLANK = NOT SENT & yes10 = Sent)
$mail_status = $row(['mail_sent']);
// CREATING THE CONDITION
if($cstt == 10 and $mail_status == ''){
// SENDING EMAIL if $cstt = 10 & email is not sent (blank)
$to = "admin#example.com, user#example.com";
$subject = "email subject";
$message ='Hi user, email text';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Web admin <admin#example.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
// ADDING The RECORD THAT EMAIL HAS BEEN SENT FOR ($CSTT == 10)
$sql = "INSERT INTO `mail_record ` (`mail_sent`) VALUES ('yes10')";
if ($conn->query($sql) === TRUE) {
echo "Email sent and record added successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
by adding a if condition / logic we can write a code to execute mail send action only once.
now if you refresh the page the first if condition will give result FALSE because $cstt == 10 TRUE but $mail_status == '' FALSE.
using this method you can be very sure that the mail will be sent only once. no matter how many times page is being refreshed.
I wish to send an email to one address and cc to other addresses drawn from a table.
// get all cc email id's for this users site
$results3h = mysql_query("SELECT user_id FROM company_ccemails WHERE site_id = '$site_id' ");
echo mysql_error();
while($row = mysql_fetch_array($results3h))
{
$cc_id = $row['user_id'] ;
//get email addresses for each id
$results3i = mysql_query("SELECT username FROM user WHERE id = '$cc_id' ");
echo mysql_error();
while($row = mysql_fetch_array($results3i))
{
$ccemails = $row['username'] . "," ;
}
mysql_free_result($results3i);
}
mysql_free_result($results3h);
//send emails to
$to = "support#mydomain.com ; ";
$subject = "$email_subject";
$message = "$email_message";
$headers = "CC: " .$ccemails. "\r\n";
$headers .= "From: " . strip_tags($myusername) . "\r\n";
$headers .= "Reply-To: ". strip_tags($myusername) . "\r\n";
$headers .= "BCC: " .$myusername. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
when sending this email only the 'to' and 1 'cc' is sent although there are 3 'cc' email address. if I move the send code under $ccemails = $row... then an email is sent to each 'cc' separately along with support#mydomain.com. this results in support# getting lots of emails.
how do I change the code to get the cc emails in one string and send as one email so support only receives one copy?
I'm relatively new to PHP coding (usually ASP) and am sure this is straight forward but is confusing me at the moment
thanks for any help
With $ccemails = $row['username'] . "," ; you are assigning the value of $row['username'] to your $ccemails variable. This means you override it everytime in the loop.
You have to concat the string with .=
Try this below:
$ccemails = ""; // define this here, because otherwise you will get a notice
while ( $row = mysql_fetch_array( $results3h ) ) {
/**
* Your code
*/
while ( $row = mysql_fetch_array( $results3i ) ) {
$ccemails .= $row['username'] . ",";
}
/**
* Your code
*/
}
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 am trying to write some PHP code that emails every user in my database (around 500) their usernames. I have successfully managed to pull the email addresses and usernames for each user and store them in an array. However, I cannot figure out how to email each individual user with their individual usernames. I am pretty sure I need to use a foreach loop to do this, but I have had no luck.
Here is what I have.
<?php
include('databaseConn.php');
$query = mysql_query("SELECT * FROM staff");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
print_r($emailArray); //This associative array now contains each username along with their respective email address.
?>
==***********===
WITH MAIL FUNCTION
<?php
include('functions/core.php');
$query = mysql_query("SELECT * FROM users");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
foreach($emailArray as $email => $username) {
echo $username; // outputs the indexes.
$subject = 'Accoutn Details';
$message = 'This email contains your login details.<br/><br/>
<b>Username: '.$username.'</b><br/>
<br/><br/>Kind regards,<br/>';
$headers = 'From: noreply#xxxxx.co.uk' . "\r\n" .
'Reply-To: noreply#xxxxx.co.uk' . "\r\n" .
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($emailAddress, $subject, $message, $headers);
}
//print_r($emailArray);
?>
bad idea to use mail in a loop unless its a one off, which it sounds like it, also most shared hosts would not allow 500 emails to be sent at once.
$subject = 'Account Details';
$headers = 'From: noreply#xxxxx.co.uk' . "\r\n" .
'Reply-To: noreply#xxxxx.co.uk' . "\r\n" .
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
while ($row = mysql_fetch_array($query)) {
$message = 'This email contains your login details.<br/><br/>
<b>Username: '.$row['username'].'</b><br/>
<br/><br/>Kind regards,<br/>';
mail("$row['email']", $subject, $message, $headers);
}
If your question is how to use foreach... http://www.php.net/manual/en/control-structures.foreach.php
For example:
foreach ($emailArray as $email => $username) {
// Send an email to $email with their $username
}
#Dagon is right, there's no need to iterate twice if there's nothing else you need the associative array for. Even if you do need an array, it doesn't necessarily have to be associative; it could be the raw rows from the DB, which you could iterate over. The only time you need an associative array is when you need to look up the values (usernames) by the keys (email addresses), which, as far as I can tell, you don't need to do.
$sql = "select emailid from table where category = 1";
while($row=mysql_fetch_array($sql))
{
$email=$row['emailid'];
$to = $email;
$subject = "E-mail subject";
$body = "E-mail body";
$headers = 'From: info#mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info#mydomain.com' . "\r\n";
mail($to, $subject, $body, $headers);
}
Suppose the above code will fetch 100 e-mail ids from database and send the mail one by one to each e-mail id. but what i want to do is.... fetch all e-mail ids in an array and send them as 'BCC' to each e-mail at once. one more thing i want to customize the e-mail body content for each id....
Any ideas?
What you want to do is impossible. If you send them at once via multiple addresses in To/CC/BCC, everyone gets exactly the same email. So you cannot customize the bodies.
If sending the same email to everyone is fine, try this:
$sql = mysql_query("select emailid from table where category = 1");
$recipients = array();
while($row = mysql_fetch_array($sql)) {
$recipients[] = $row['emailid'];
}
$to = 'info#mydomain.com';
$subject = "E-mail subject";
$body = "E-mail body";
$headers = 'From: info#mydomain.com' . "\r\n" ;
$headers .= 'Reply-To: info#mydomain.com' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";
mail($to, $subject, $body, $headers);