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
Related
This has to be a simple and stupid question but cant seem to figure it out.
What I want to do is something like:
$message = include ('./myfile.php');
Obviously such does not work, but is there a way to accomplish this if that code worked.
Thanx
Here is full code (abbreviated):
require_once ('./connect.php');
$db = mysqli_connect($db_hostname,$db_username,$db_password,"paratb_members");
$result = mysqli_query($db,"SELECT * FROM board where accesskey = 'CHHXN5Jdwu'");
while ($row = mysqli_fetch_array($result)) {
extract($row);
$message = include './questions/resignation.php';
$subject = "IAP Ballot";
$headers = "From: xxxx" . "\r\n";
$headers .= "Reply-To: xxxx" . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$fromEmail = "xxxx";
$fifth = "-f" . $fromEmail;
$to = "$fname $lname <$email>";
mail($to, $subject, $message, $headers, $fifth);
echo "Email sent to $lname: $email<br>";
You can first read file & store data in variable which contain file's data then you can use that variable.
fopen("myfile.php", "r");
$lines = file("myfile.php");
$message = '';
foreach($lines as $value) {
$message .= $value." ";
}
echo ($message);
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";
}
}
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;
}
I have problem to send array from database via email. I´m sure may people have got the same problem. I don´t know how to explain more but here is some script.
$sql="SELECT * FROM tb_xxx WHERE id_prd = '$ref_id_prd'";
$result=mysql_db_query($dbname,$sql);
while ($rs=mysql_fetch_array($result)) {
$ref_id_prd=$rs[ref_id_prd];
$prd=$rs[prd];
$price=$rs[price];
text="$prd <br>$price";
}
$recipient = $iemail;
$subject = "my subject";
$headers = "From: xxx#xxx.com \n";
$headers .= "Reply-To: xxx#xxx.com \n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
$msg = $text;
mail($recipient, $subject, $message, $headers);
You need to accumulate all the array values into a single string for the message body.
$text = "";
while ($rs = mysql_fetch_array($result)) {
$ref_id_prd=$rs[ref_id_prd];
$prd=$rs[prd];
$price=$rs[price];
// Use .= to append to the current value
$text .= "$prd <br>$price\n";
}
To send it, use $text as the message body:
mail($recipient, $subject, $text, $headers);
Note, you will need to do additional formatting on the line below to get it looking the way you want in your HTML email:
$text .= "$prd <br>$price\n";
For example, you could make a list of products:
$text = "<ul>\n";
// inside the while loop...
$text .= "<li>$prd: $price</li>\n";
// After the loop, close the list
$text .= "</ul>"
Update: How to build it into an HTML table:
$text = "";
// Open the table:
$text .= "<table>\n";
// Table header...
$text .= "<thead><tr><th>Product</th><th>Price</th></tr></thead>\n";
// Open the table body
$text .= "<tbody>\n";
while ($rs = mysql_fetch_array($result)) {
$ref_id_prd=$rs[ref_id_prd];
$prd=$rs[prd];
$price=$rs[price];
// Build table rows...
$text .= "<tr><td>$prd</td><td>$price</td></tr>";
}
// Close the table
$text .= "</table>";
Replace your $message variable with $msg
mail($recipient, $subject, $msg, $headers);
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