PHP Mail using HTML with PHP if statement embedded - php

I currently have PHP sending an HTML email that lists every item from a form even if the quantity is 0. Now I want to change it to only list items that are being ordered (Qty>0). After looking all over the internet to to and find a way to embed a PHP if statement inside an HTML email, this is what I have come up with and it's still not working...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
?>
<?php
$to = "me#company.com";
$subject = "Marketing Material Order from $name";
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
<?php if ($AV000100 != 0) {
$AV000100 - Brochures;
}
?>
<?php if ($AV000101 != 0) {
$AV000101 - Folders;
}
?>
<?php if ($AV000102 != 0) {
$AV000102 - Pens;
}
?>
<p>Comments: $comments</p>
</body>
</html>
";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\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);
?>
How do I change the embedded PHP if statement to show this as the email body...
Name: John Doe
Email: jon#mail.com
Date Needed: Today
2 - Brochures
2 - Pens
Comments: Thank you
Instead of...
Name: John Doe
Email: jon#mail.com
Date Needed: Today
2 - Brochures
0 - Folders
2 - Pens
Comments: Thank you
FYI...the way I have the PHP if statement written above, none of the items show up. Any help would be greatly appreciated!

Not quite sure what you want - but if you want to use the result of evaluating the PHP code as the message body then you need to use output buffering (or a lot of inline ifs - output buffering is cleaner).
For example, if you put all your code the generate the message body in a file body.php then you could use:
ob_start();
include('body.php');
$message = ob_get_Clean();
You don't need to have the body code in a seperate file - just include it instead of the include statrement betwen the ob_start() and ob_get_clean()

Here's what you want to do with what you have.
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email: $email<br>
Date Needed: $date
</p>
" . ($AV000100 != 0 ? $AV000100 . " - Brochures" : "") . "
" . ($AV000101 != 0 ? $AV000101 . " - Folders" : "") . "
" . ($AV000102 != 0 ? $AV000102 . " - Pens" : "") . "
<p>Comments: $comments</p>
</body>
</html>";

You can append the message line by line if you want using the concatenating assignment operator (.=), and put the if statements where you need them. Like so:
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
You can just send $message as usual.
Here is the full code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];
$comments = $_POST['comments'];
$to = "me#company.com";
$subject = "Marketing Material Order from $name";
$message = "<html>";
$message .= "<head></head>";
$message .= "<body>";
$message .= "<p>";
$message .= "Name: $name<br>";
$message .= "Email: $email<br>";
$message .= "Date Needed: $date";
$message .= "</p>";
if ($AV000100 != 0) {
$message .= "$AV000100 - Brochures";
}
if ($AV000101 != 0) {
$message .= "$AV000101 - Folders";
}
if ($AV000102 != 0) {
$message .= "$AV000102 - Pens";
}
$message .= "<p>Comments: $comments</p>";
$message .= "</body>";
$message .= "</html>";
$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\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);
?>

Try like this
$message = "
<html>
<head></head>
<body>
<p>
Name: $name<br>
Email:$email<br>
Date Needed:$date
</p>
...
...";
Each time you break the string and then concatenate the string with the php results using . operator.

Related

Mail Function is giving false return in PHP

I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';

Php form handler/formatting of email message lost

I've got a form on my website and when I added htmlspecialchars function to the php form handler, it seemed I also had to change the character encoding to utf-8 so that letters with accents would come through. Now that I've added $headers, the characters are shown correctly but the formatting of the email message ($message) is lost with no line breaks. This is what my code looks like:
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$message = "
Website form:
Name: " . $firstname . " " . $surname . "
Address: " . $address . "
Age: " . $age . ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
if (mail("me#me.com", $_POST['firstname'] ." ". $_POST['surname'], $message, $headers)) {
header("Location: ...");
}
I tried to fix the issue with
$message = "Website form\n";
$message .= "Name: " . $firstname . " " . $surname . "\n";
$message .= "Address: " . $address . "\n";
$message .= "Age: " . $age . "\n";
but the problem persists. I also tried to change content-type to text/plain but the email message appeared as an attachment file. I am at a loss and don't know what I'm doing wrong as the formatting is still lost. I'm a beginner with php so any help will be appreciated.
You should try message format like this:
// email message
$message ="";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
Here is complete code:
<?php
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$to = 'youemail#email.com';
$subject = 'Subject Line';
$from = 'info#example.com';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// email message
$message = "";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>

PHP: Mail not sent

I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n";
// $headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}
Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com);
// 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: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';

sending all data from email with require field

I'm creating a user require form in my site. For this I put some validation on compulsory fields, and when a user fills in the form and presses submit and validation is correct then I receive a email on my email address.
But now I would like all user information in the email, like name, city, budget etc... so what changes do I need to make in my email.php script?
If some fields are not compulsory and the user doesn't fill them in, can they affect my script?
My script is:
<?php
$to = "test#networkers.in";
$subject = "a new requiremnet come!";
$message = "Hi,\n\nyou get a new require";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
$headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
$headers .= "Priority: normal" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
echo "Your mail was sent successfully";
} else {
echo "We encountered an error sending your mail";
}
?>
and the data I recieve is:
$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];
You can add fields to the message body by concatenating them like so:
$message = "Hi,\n\nyou get a new require";
$message .= "\n Name: " . $name;
$message .= "\n Email: " . $email;
$message .= "\n Phone: " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt: " . $pt;
$message .= "\n cwl: " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb: " . $pb;
$message .= "\n bpd: " . $bpd;
$message .= "\n hdyhau: " . $hdyhau
Any fields that weren’t filled in by the user will simply be blank.

PHP Form Review - Cc and Bcc not working

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";

Categories