Trouble with PHPMAIL..Any advice? - php

Having trouble executing a select and send to all email address in a script.
// Get Employee's Email Address
$getEmail = "SELECT empEmail AS theEmail FROM employees";
$emailres = mysqli_query($mysqli, $getEmail) or die('-1'.mysqli_error());
$col = mysqli_fetch_assoc($emailres);
$theEmail = $col['theEmail'];
// the message
$message = '<html><body>';
$message .= '<h3>New Site Notifications</h3>';
$message .= '<p>'.$noticeTitle.'</p>';
$message .= '<p>'.$noticeText.'</p>';
$message .= '<p>'.$messageText.'</p>';
$message .= '<hr>';
$message .= '<p>'.$emailLoginLink.'</p>';
$message .= '<p>Thank you<br>Bliss Door Supervisors</p>';
$message .= '</body></html>';
$headers = "From: ".$siteName." <".$businessEmail.">\r\n";
$headers .= "Reply-To: ".$businessEmail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// use wordwrap() if lines are longer than 70 characters
//$msg = wordwrap($msg,70);
// send email
mail($theEmail," New Site Notification",$message,$headers);
//End Send Mail
For some reason, it only emails the first email in the database but not the other 10+ witihin.
Can anyone see where i'm going wrong? or assist.
Many thanks
Spike

Fetch only pulls one row at a time. Move the fetch into a loop like...
// Get Employee's Email Address
$getEmail = "SELECT empEmail AS theEmail FROM employees";
$emailres = mysqli_query($mysqli, $getEmail) or die('-1'.mysqli_error());
while($col = mysqli_fetch_assoc($emailres)){ //start loop here so each email address is pulled
$theEmail = $col['theEmail'];
// the message
$message = '<html><body>';
$message .= '<h3>New Site Notifications</h3>';
$message .= '<p>'.$noticeTitle.'</p>';
$message .= '<p>'.$noticeText.'</p>';
$message .= '<p>'.$messageText.'</p>';
$message .= '<hr>';
$message .= '<p>'.$emailLoginLink.'</p>';
$message .= '<p>Thank you<br>Bliss Door Supervisors</p>';
$message .= '</body></html>';
$headers = "From: ".$siteName." <".$businessEmail.">\r\n";
$headers .= "Reply-To: ".$businessEmail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// use wordwrap() if lines are longer than 70 characters
//$msg = wordwrap($msg,70);
// send email
mail($theEmail," New Site Notification",$message,$headers);
//End Send Mail
}//end loop
Reference: http://php.net/manual/en/mysqli-result.fetch-assoc.php
Returns an associative array that corresponds to the fetched row
From their example:
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}

Related

PHP Mail Function waits and not working with foreach loop

I am trying to send mail to all users that have no created reports in one month, also trying to send mail to this users with foreach loop and with mail function, when i refresh one by one then it sends mail one by one, then it works. i want to send mail to all this users in one time.
$from = "xxx#xxx.com";
$subject = "";
$headers = "";
$inc = 0;
foreach($query->result_array() as $row)
{
$inc++;
$to = $row['us_email'];
$to_date = $row['report_date'];
if($to_date != "0000-00-00")
{
$subject = "Hello! (Reports Are Not Created).";
//begin of HTML message
$message = "1 month above you should create reports.";
}
else if($to_date == "0000-00-00")
{
$subject = "Hello! (Generate Reports).";
//begin of HTML message
$message ="generate reports to get more.";
}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "To: User <".$to.">, Admin <".$from.">" . "\r\n";
$headers .= "From: Complete Online Marketing <".$from.">" . "\r\n";
$headers .= "Reply-To: Recipient Name <".$from.">";
$headers .= "Cc: [email]".$from."[/email]" . "\r\n";
$headers .= "Bcc: [email]".$from."[/email]" . "\r\n";
// now lets send the email.
if(mail($to, $subject, $message, $headers))
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Success...\n";
$ins = array('report_mail'=>'0');
$this->db->update('se_user', $ins, array('us_id' => $row['us_id']));
}
else
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Fail...\n";
}
}

php foreach loop not looping to send emails only sending last record

I am pulling emails out of a recordset. They loop through and echo out correctly but am only sending to the last record? What am I missing here? It seems if it echos out the email it should be sending as well. Thanks
$query_email = "SELECT email FROM UserInfo";
$email = mysql_query($query_email, $dandee123) or die(mysql_error());
$numRows = mysql_num_rows($email);
for ($count = 0; $count < $numRows; $count++){
$row[] = mysql_fetch_array($email, MYSQL_ASSOC);
}
$subject = strip_tags($_POST['subject']);
$headers = "From: contact#xxxx\r\n";
$headers .= "Reply-To: contact#xxx";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= $_POST['messagesent'];
$message .= '</body></html>';
foreach ($row as $email)
{
$to = $email['email'];
mail($to, $subject, $message, $headers);
//this works below but email only goes to last record?
print "sent to ". $to;
}

Php Email Mysql Fetch Array

I have this script to send an email containing information from my database. The user can have 1+ items in the database with it's location. So when I empty the rows that match the user the amount of emails sent equals the number of rows they have. So if they have 8 items in the database it send 8 emails. Each adds an item. So the first email has one item, the second with two items, and so on. I am trying to find a simple way to make it get all the information before sending the email so the customer only gets one email. The logical way would be to echo the information but I can't do that with a php variable. I didn't include the query and database connection in the code below. Any help would be loved.
while ($row = mysql_fetch_array($query)) {
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
$to = "example#example.com";
$from = "example#example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example#example.com";
mail($to, $subject, $message, $headers);
}
Just move send function out of cycle:
while ($row = mysql_fetch_array($query)) {
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
}
if ($Items != '') {
$to = "example#example.com";
$from = "example#example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example#example.com";
mail($to, $subject, $message, $headers);
}
When you iterate over the items, you should only build a message and not the entire email.. It's hard to do much more than the following without knowing more about your query. I'll give it a shot anyway:
$message = '';
while ($row = mysql_fetch_array($query)) {
$Items = $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
$message .= "$Items";
}
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example#example.com";
mail($to, $subject, $message, $headers);
Note: I wouldn't implement this code as is. It's meant to serve as an example on how to structure your code.
Your concatenation is wrong, you should first declare your variable as empty outside your loop
$Items = '';
Then start your loop and get all data you need concatenating your variable
while ($row = mysql_fetch_array($query))
{
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
}
Now you are ready for send email, outside your loop or you will end up with one email for each cicle. So your code would look like this
$Items = '';
while ($row = mysql_fetch_array($query))
{
$Items .= $row['Items']. " - Aisle " .$row['Loc']. "<p> </p>";
}
$from = "example#example.com";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: example#example.com";
mail($to, $subject, $message, $headers);
Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

How to send emails to a users friends when the user takes a specific action?

I am using this script below, which works. The only thing is it sends an email to the first email id it gets. The rest are being ingnored. So how do i send email to all the emails. When i tested just the script that is pulling emails, it works, it shows all the emails.
Code:
$sql = "SELECT STRAIGHT_JOIN DISTINCT email from
friend_email_ids WHERE my_id='$id'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$email = $row['email'];
print("");
}
}
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail("$email", "Subject: $subject",
$message, "$headers" );
echo "";
You are overwriting your $email variable each time through the loop. Instead, you want to create an array, and add the email to the array each time through the loop. When finished, join the email addresses in the array with commas before sending the mail.
So before your loop (maybe after the $rows = ... statement), initialize a new array, like this:
$rows = mysql_num_rows($result);
$emails = array(); /* Add this line */
Then each time through the loop, add the email to the array:
while($row = mysql_fetch_array($query)) {
array_push($emails, $row['email']);
/* ... */
}
Finally, join them with commas in your email send:
mail(implode(',', $emails), "Subject: $subject", $message, $headers);
Alternatively, you can send one separate email to each user like this:
foreach ($emails as $email) {
mail($email, "Subject: $subject", $message, $headers);
}
Try this:
<?php
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href='www.domain.com/signup.php?t=&sign='>Click here.</a><br><br>";
$message .= "</body></html>";
$sql = "SELECT DISTINCT email from friend_email_ids WHERE my_id='$id'";
$result = mysql_query($sql) or die ("Error: ".mysql_error());
while($row = mysql_fetch_array($result))
{
mail($row['email'], $subject, $message, $headers);
// for debugging purposes, uncomment the following line
// echo $row['email']
}
Changes:
cleaned up PHP code (removed unused instructions, remove second query which doesn't do anything, removed string vars in " etc.)
corrected HTML link in the email message (it had no quotes on the href attribute)
loop through results to send email for each $row
Here i am attaching code for sending multiple mails and integrating it with your existing code.
You just need to copy paste this code in your page and upload it on some server to test for its workability. I have already tested on my server and it gives multiple mails.
$email = array();
$sql = "SELECT STRAIGHT_JOIN DISTINCT email from friend_email_ids WHERE my_id = '".$id."' ";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
while($row = mysql_fetch_assoc($query))
{
$email[] = $row['email'];
}
/*
echo "<pre>";
print_r($email);
exit;
*/
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted this mail<br><br>";
$message .= "<a href='www.aadinathtech.com/index.php'>Click here.</a><br><br>";
$message .= "</body></html>";
foreach($email as $key=>$val) {
mail($val, "Subject: $subject", $message, "$headers" );
echo "<br />Mail has been sent to $val<br /><br />";
}
Please revert me back if you still find any problem.
You need to do a small modification by moving the last block of code inside your loop.
Try this:
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$email = $row['email'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail("$email", "Subject: $subject",
$message, "$headers" );
print("");
}
}
echo "";

How to prevent receipeints from seeing which other email addresses have received an email?

I am using this script to send notificaitons to users friends. the problem is that all recepieints get to see who else got this email. How do i tweak the code so the emails still get sent to all but they can't see who else got it?
Code:
$sql = "SELECT STRAIGHT_JOIN DISTINCT email from
friend_email_ids WHERE my_id='$id'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
$emails = array();
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
array_push($emails, $row['email']);
{
$email = $row['email'];
print("");
}
}
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail(implode(",", $emails), "Subject: $subject",
$message, "$headers" );
echo "";
Just use BBC for all recipients:
Bcc: recipients get a copy of the email, but their email address is
automatically deleted at delivery. Nobody except you and the Bcc:
recipient will know that they got a copy, and their email address will
not be exposed.
-> http://email.about.com/od/emailmanagementtips/qt/How_to_Send_an_Email_to_Undisclosed_Recipients.htm
Use the additional_headers fields to add a BCC* address . See the manual
From the manual page:
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
the "birthdaycheck" email is hidden.
*(Blind Carbon Copy)
In you script it would become something like this:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $usermail\r\n";
////////////pay attention here
$headers .= "BCC: ".implode(",", $emails)."\r\n";
$to = "youremail#domain.com"; //the mail in the "TO", visible to all. there has to be 1.
////////////////////////
$subject = "$full_name added";
$message = "<html><body>";
$message .= "Hello, <br><br>$full_name posted someth<br><br>";
$message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>";
$message .= "</body></html>";
mail($to, "Subject: $subject",
$message, "$headers" );
echo "";
Put the actual sending of messages in the loop. That way you will send the e-mail to each recipient individually instead of all at once.
From PHP.net you'll find that the Bcc feature of mail() is what you need to use.
Like zoy (for multiple peeps):
$headers .= 'Bcc: someone#example.com,someone2#example.com,someone3#example.com,someone4#example.com,' . "\r\n";
Happy Haxin!
_wryteowl

Categories