Retrieving email in html format with cron - php

Hello fellow citizens of ....oh skip all that... I am experiencing some difficulty trying to output html via my cron and I have checked other responses to this issue. But, I can’t seem to get it right so I’m hoping someone out there can assist me. The data pulls correctly and the cron email sends but what I end up with is my data with all the html tags appended. I have tried moving the $to and $header out of the while loop, above the while loop, below the while loop, and also tried with everything inside the while loop with only the $message being the one call to dtat $row etc and then echo'd that from within the loop.
I have tried two versions of ---
( $headers .= 'Content-type:text/html;charset=iso-8859-1'
. "\r\n";)
and
( $headers .= 'Content-type:text/html;charset=utf-8-1' .
"\r\n";)
So any assistance would be great!
Cheers!
$db = new MySQLi('------', '------', '------', '------');
if ($db->connect_error) {
$mese = $db->connect_error; } else {
$sql = "SELECT * FROM table";
$result = $db->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
$to = "some#gmail.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=iso-8859-
1'
. "\r\n";
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= '<ul><li>' . $row['item'] .'</li></ul>';
echo $message;
$message .= "</body>";
$message = "</html>";
$from = "somewhere#.someplace.net"; $subject = "checking php
mail";
mail($headers, $to,$subject,$message, $from);
}
}
?>

All the quotes in your code are messed up. This could be the reason.
Try this:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html>';
$message .= '<head></head>';
$message .= '<body>';
$message .= '<ul><li>' . $row['item'] .'</li></ul>';
$message .= '</body>';
$message .= '</html>';

Related

Send url with variables through email body in Php

Trying to build a code to send password reset link through email in response to those who request it.
The email is received on hotmail and gmail, but not on particular mail client I do not know the configuration.
Of course, I tried several arrangements regarding the quotes and doubles-quotes, but it did not give anything.
<?php
$site = "http://www.example.fr";
$from = "info#example.fr";
$nom = "DLSS";
$limite = "_----------=_parties_".md5(uniqid (rand()));
$sujet = "Password Reset";
$text = "Please click on this link to initialize your password.";
$html = "Please click on this link to initialize your password.";
$from = $nom." <".$from.">";
$header = "From: ".$from."\n";
$header .= "Reply-to: ".$from."\n";
$header .= "Return-Path: ".$from."\n";
$header .= "Organization: ".$nom."\n";
$header .= "X-Sender: <".$site.">\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$header .= "X-auth-smtp-user: ".$from." \n";
$header .= "X-abuse-contact: ".$from." \n";
$header .= "Date: ".date("D, j M Y G:i:s O")."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/alternative; boundary=\"".$limite."\"";
$message = "";
$message .= "--".$limite."\n";
$message .= "Content-Type: text/plain\n";
$message .= "charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= $text;
$message .= "\n\n--".$limite."\n";
$message .= "Content-Type: text/html; ";
$message .= "charset=\"iso-8859-1\"; ";
$message .= "Content-Transfer-Encoding: 8bit;\n\n";
$message .= $html;
$message .= "\n--".$limite."--";
mail($eml, $sujet, $message, $header);
?>
In summary, the email is received by this particular mail client when I replace these two lines there :
$text = "Please click on this link to initialize your password.";
$html = "Please click on this link to initialize your password.";
With these two lines here :
$text = "Test Email with link. link";
$html = "Test Email with link. link";
Or with these two lines here :
$text = $eml . " - " . $cod;
$html = $eml . " - " . $cod;
Finally, the problem is in the combination of these two parts that are the web address and the two variables, it's been three days that I try to solve this problem but I can not do it alone, it would be really nice to have help, Thanks in advance !

Send Mail when SQL value is True

So, I'm kind of in a rut for over a day now because of this issue. I have a PHP wherein it gets called by Cron on a set amount. It gets called and executes just fine without much problems except for the styling. What it does is query the database on a set time for a value and if it's true then it proceeds to email that to the designated email that I put in the script. It's doing it's job but the problem is the format when the email gets delivered. The email looks like this when I open it:
The issue, as you can see is that only the first line or row gets formatted on the table and the rest of the results doesn't follow it.
Here's the current script I'm using with some omitted/changed details.
#!/path/to/php
<?php
//DB
$dbconnect=new mysqli('localhost', 'user', 'pass', 'db');
$result=$dbconnect->query("SELECT `uid`,`pid`,`pdesc`,`umail`,`name` FROM `db_table` WHERE `pid`<='6'");
//if there are any records matching this query send an email listing each one using 'uid' as the identifier
if($result->num_rows>=1) {
$to = 'test#test.com';
$subject = "TEST FOR PRIZES NOTIFICATION";
$headers = 'From: sender#test.com' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: rep#test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message .= 'THIS IS A TEST!';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr>
<td>User ID</td>
<td>Display Name</td>
<td>Email</td>
<td>Price</td>
<td>Price Description</td></tr>';
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
}
if(mail($to, $subject, $message, $headers)) {
//mail successfully sent
} else {
//mail unsuccessful
}
}
?>
Thank you for your time!
You're closing the table inside of your loop, so it's closed after the first entry. Change your code from this:
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
}
to this:
while($row=$result->fetch_assoc()) {
$message .= "<tr><td>".$row['uid']."</td>";
$message .= "<td>".$row['name']."</td>";
$message .= "<td>".$row['umail']."</td>";
$message .= "<td>".$row['pid']."</td>";
$message .= "<td>".$row['pdesc']."</td></tr>";
}
$message .= "</table>";
$message .= "</body></html>";
to close the table at the end.

execute mail once all item have been iterated

I have the following issues.
I receive separate emails for each iteration of the loop. I want mail to be only sent once with all of the items iterated.
<?php
// Honey pot trap
// Create a hidden input that is only visible to bots. If it's empty than proceed.
if (empty($_POST['humancheck'])){
// Proceeed if submit button have been pressed
$fullName = $_POST['fname'];
$email = $_POST['email'];
$stage = $_POST['stage'];
include("db.php");
$resources = "select * from resources where stage LIKE '%".$stage."%'";
$run_query = mysqli_query($con, $resources);
while($row = mysqli_fetch_array($run_query)) {
$data[] = array(
'format' => $row['format'],
'title' => $row['title'],
'costs' => $row['cost'],
'stage' => $row['stage'],
'topic' => $row['topic'],
'link' => $row['link']
);
}
foreach($data as $item) {
// Sanitize input data
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
// Mail Set up
$to = $clean_email;
$to_us = "info#email.com";
// Email subject
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br>
<i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
// Headers
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info#email.com>' . "\r\n";
}
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);
}
?>
what happens is the while loops to the data that is stored in an array. That array is used in foreach. and outside of the loops, the mail is suppose to mail the result.
In theory this should of worked but its not working.
Try this. Edit last part of your code in this way. Take one time parameters of mail content outside the loop, and let loop make only the message content.
$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
$to = $clean_email;
$to_us = "info#email.com";
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
foreach($data as $item) {
$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
}
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br><i> Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs: \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us. \r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info#email.com>' . "\r\n";
mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);

Trouble with PHPMAIL..Any advice?

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"]);
}

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?
I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {
$to = 'XXXXXX#gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
Replace the $form3_name with $form3_email
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
^^^^^^^^^^^^ //<----- Here
That's a name , not an email address , and that's the reason you get that error.
Also, you need to wrap them in tags <>
The right way..
$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";
replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
$headers .= 'Reply-To:'.$form3_email. "\n";
please let me know if you want furtther guidance...
Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.
Try something like:
$headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";
Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

Categories