How to send a retrieved table through PHP mail() function - php

The below code gives the output as how in screenshot. I want to send this content via php mail function . When i try to add this code into $message , It doesnt work as it contains php code(to retrieve data from table) in between html code. Kindly help on how this can be achieved ?
This is the code i was trying out for email .
<?php
include('db.php'); //connection to database
$to = 'example#gmail.com';
$subject = "test php mail" ;
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
<?php
$records = mysql_query("SELECT * FROM hindiday order by
rand(curdate())
limit 1 ");
while ($course = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' '
.$course['id']." </td>"; "</tr>" ;
}
?>
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc#gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>

I would suggest looping outside the string, and the concatenate the result like so :
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
//Using the variable in your message
$message = '
...
'. $string .'
...
';
That way wherever you want to add the $string you just concatenate it, as if you try to execute PHP code inside a string it'll be considered as a string and the PHP in question won't be executed.
So the final result would look like this :
<?php
include('db.php'); //connection to database
$to = 'example#gmail.com';
$subject = "test php mail" ;
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
'. $string .'
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc#gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>

Related

Issue adding HTML tables into PHP Mail script

I have the following mail script with the structure shown below:
<?php
$to = 'example#testing';
$subject = 'Testing';
$from = 'test#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";
// 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!</h1>';
$message .= '<p style="color:#080;font-size:18px;">This is a test.</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
I then would like to put the following table inside the body as well but when adding the following, the PHP does not work:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
tr.header
{
font-weight:bold;
}
tr.alt
{
background-color: #777777;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.striped tr:even').addClass('alt');
});
</script>
<title></title>
</head>
<body>
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()");
?>
<table class="striped">
<tr class="header">
<td>first_name</td>
<td>last_name</td>
<td>sign_date</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[first_name]."</td>";
echo "<td>".$row[last_name]."</td>";
echo "<td>".$row[sign_date]."</td>";
echo "</tr>";
}
?>
</table>
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()");
?>
<table class="striped">
<tr class="header">
<td>employee_id</td>
<td>job_title</td>
<td>address</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[employee_id]."</td>";
echo "<td>".$row[job_title]."</td>";
echo "<td>".$row[address]."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
How can the two simply be added together. The PHP does not seem to be read and is ignored when combining the two, what is the most effective way of having the two scripts work together?
You can likely combine the two parts like this - the PHP code needs to run before the mail is sent ( I found the "but when adding the following, the PHP does not work" a little confusing )
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$to = 'example#testing';
$subject = 'Testing';
$from = 'test#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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message="
<html>
<head>
<title></title>
<style>
tr.header { font-weight:bold; }
tr.alt { background-color: #777777; }
tr:nth-of-type(even) td{background-color:#777777}
</style>
</head>
<body>
<table class='striped'>
<tr class='header'>
<td>first_name</td>
<td>last_name</td>
<td>sign_date</td>
</tr>";
$query = mysql_query('SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()');
while ( $row = mysql_fetch_array( $query ) ) {
$message.='
<tr>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['sign_date'].'</td>
</tr>';
}
$message.="
</table>
<table class='striped'>
<tr class='header'>
<td>employee_id</td>
<td>job_title</td>
<td>address</td>
</tr>";
$query = mysql_query('SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()');
while ($row = mysql_fetch_array($query)) {
$message.='
<tr>
<td>'.$row[employee_id].'</td>
<td>'.$row[job_title].'</td>
<td>'.$row[address].'</td>
</tr>';
}
$message.="
</table>
</body>
</html>
";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
When you add the HTML to the body it becomes a string.
A string can convert php variables to strings. But can't execute php code.
You'll have to execute the PHP code in advance. Add the data from the PHP code to the HTML and then add the HTML to the message body.
EDIT:
This is an example of how it would work:
<?php
$html = generateHTML();
$to = 'example#testing';
$subject = 'Testing';
$from = 'test#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";
// 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;
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
function generateHTML(){
$html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script><style type="text/css">tr.header{font-weight:bold;}tr.alt{background-color: #777777;}</style><script type="text/javascript">$(document).ready(function(){$(".striped tr:even").addClass("alt");});</script><title></title></head><body><table class="striped"><tr class="header"><td>first_name</td><td>last_name</td><td>sign_date</td></tr>';
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("SELECT first_name, last_name, sign_date FROM Table1 WHERE sign_date = NOW()");
while ($row = mysql_fetch_array($query)) {
$html.="<tr>";
$html.="<td>".$row[first_name]."</td>";
$html.="<td>".$row[last_name]."</td>";
$html.="<td>".$row[sign_date]."</td>";
$html.="</tr>";
}
$html.='</table><table class="striped"><tr class="header"><td>employee_id</td>td>job_title</td><td>address</td></tr>';
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("SELECT employee_id, job_title, address FROM Table1 WHERE sign_date = NOW()");
while ($row = mysql_fetch_array($query)) {
$html.="<tr>";
$html.="<td>".$row[employee_id]."</td>";
$html.="<td>".$row[job_title]."</td>";
$html.="<td>".$row[address]."</td>";
$html.="</tr>";
}
$html.= '</table></body></html>';
return $html;
}
?>

Updating sql quantity using php from cart page

I have a page where users select a product (products come from sql database) and input a quantity needed. They then view a "cart" page and submit the order which just sends a email to me with the details. I am trying to automatically update the quantity in the sql database when they submit the order.
I think I would have to do something like the following, but I am new to php and sql. I would have to take the original quantity from the DB and subtract the "ordered quantity" which is entered by the user and displayed on the cart page and set that new value. Can anyone shed some light on how I might accomplish this?
What I think I have to do?:
$updquery = 'UPDATE "products" SET "Quantity"= "Quantity" - '. $product['quantity'] .' WHERE PID = '. $product['id'] .' ';
cart.php
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
?>
<?php
#session_start();
if(isset($_POST['logout'])) {
unset($_SESSION['shopping_cart']);
}
if(isset($_POST['submit'])) {
$email = $_POST['email'];
// Create the email and send the message
$to = 'myemail#gmail.com'; // Add your email address inbetween the ''
replacing yourname#yourdomain.com - This is where the form will send a
message to.
$email_subject = "Products Order - ".$_POST['email']."";
// PREPARE THE BODY OF THE MESSAGE
$email_body = '<html><body>';
$email_body .= '<h1 style="text-align:center;">Products List</h1>';
$email_body .= '<table rules="all" style="border-color: #666;margin:
auto;" border="1" cellpadding="10">';
$email_body .= '<tr><th colspan="2"><h3>Requested Parts</h3></th>
</tr>';
$email_body .= '<tr><th width="100" align="left">Product ID</th><th
width="100" align="right">Quantity</th></tr>';
foreach($_SESSION['shopping_cart'] as $key => $product):
$email_body .= '<tr><td>'. $product['id'] .'</td><td
align="right">'. $product['quantity'] .'</td></tr>';
endforeach;
$email_body .= "</table>";
$email_body .= "</body></html>";
$headers = "From: myemail#gmail.com\n"; // This is the email address
the generated message will be from. We recommend using something like
noreply#yourdomain.com.
$headers .= "Reply-To: ".$_POST['email']."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to,$email_subject,$email_body,$headers);
unset($_SESSION['shopping_cart']);
?>
<script>
alert("Your order has been submitted")
window.location.href = "index.php"
</script>
<?php
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inventory Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/icon.ico" type="image/x-icon">
<style>
*{
margin :auto;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}
tr:nth-child(even) {
background-color: #eee;
}
tr:nth-child(odd) {
background-color: #fff;
}
</style>
</head>
<body>
<h1 style="text-align:center;">Cart</h1>
<?php
if(!empty($_SESSION['shopping_cart'])){ ?>
<div class="table-responsive" style="background:white;">
<table class="table table-responsive" id="mytable" border="1"
align="center">
<tr><th colspan="2"><h3>Requested Parts</h3></th></tr>
<tr>
<th width="100" align="left">Product ID</th>
<th width="100" align="right">Quantity</th>
</tr>
<?php
foreach($_SESSION['shopping_cart'] as $key => $product):
?>
<tr>
<td><?php echo $product['id']; ?></td>
<td align="right"><?php echo $product['quantity']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
<div style="text-align:center;">
<form action="" method="post">
<input type="email" size="31" name="email" placeholder="Please enter
your email address" required /><br>
<input type="submit" name="submit" value="Submit" id="btnSubmit"/>
</form>
<form action="" method="post">
<input type="submit" name="logout" value="Clear" id="btnClear" />
</form>
</div>
</div>
<?php
}
else{
?>
<script>
alert("Cart is empty")
window.location.href = "index.php"
</script>
<?php
}
?>
<br><br>
</body>
</html>
You provided your own answer (query):
1) Set up mysql connection in the beginning of your script:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
2) In your foreach, add the query to update each os the product's quantity as you add them to the e-mail. Becomes:
foreach($_SESSION['shopping_cart'] as $key => $product):
$email_body .= '<tr><td>'. $product['id'] .'</td><td align="right">'. $product['quantity'] .'</td></tr>';
mysqli_query($con,'UPDATE products SET Quantity = Quantity - '. $product['quantity'] .' WHERE PID = '. $product['id'] .' ');
endforeach;
mysqli_close($con);

image is not embeding in email in php

I am trying to embed the image to the html content of email in php but whenever the email sent it show me broken link of image there. Image is not embeding to the email here is the code
<?php
$to = 'xxxx#xxxx.com';
$subject = "Beautiful HTML Email using PHP by CodexWorld";
$htmlContent = '
<html>
<head>
<title>Welcome to CodexWorld</title>
</head>
<body>
<h1>Thanks you for joining with us!</h1>
<table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;">
<tr>
<th>Name:</th><td>CodexWorld</td>
</tr>
<tr style="background-color: #e0e0e0;">
<th>Email:</th><td>contact#codexworld.com</td>
</tr>
<tr>
<th>Website:</th><td>www.codexworld.com</td>
</tr>
<tr>
<th>image:</th><td><img src="http://xxxxxxxxxxxxx.com/email/1.png"/></td>
</tr>
</table>
</body>
</html>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: xxxx#xxxxx.com' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
Please guide me where is I am wrong

Form not showing properly with Plesk on Mac Mail

I have a form that is emailing and posting to the database as it should. The problem is that when it sends to email addresses on our own server (Parallels Plesk) and it comes into Mac Mail it comes through a little corrupt. The text is broken up and there's $= all over the form. Sometimes it even has part of the html coding sat at the top of the email, like:
<= /tr><= td colspan='3'>Further Comments:
= <= /tr><= td width='40%' colspan='2'>Expectations
But everytime it's slightly different. I.e. there's no thing it does everytime it comes through; everytime the symbols and coding is a little different.
This is my code for emailing it:
<?php
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO guestquestionnaire (date_submitted, choice, expectations, res, res_information, res_staff, further_comments1)
VALUES (:date_submitted, :choice, :expectations, :res, :res_information, :res_staff, :further_comments1)");
$stmt->bindParam(':date_submitted', $date, PDO::PARAM_STR);
$stmt->bindParam(':choice', $choice);
$stmt->bindParam(':expectations', $expectations);
$stmt->bindParam(':res', $res);
$stmt->bindParam(':res_information', $res_information);
$stmt->bindParam(':res_staff', $res_staff);
$stmt->bindParam(':further_comments1', $further_comments1);
// insert a row
$date = date('Y-m-d H:i:s');
$choice = $_POST['choice'];
$expectations = $_POST['expectations'];
$res = $_POST['res'];
$res_information = $_POST['res_information'];
$res_staff = $_POST['res_staff'];
$further_comments1 = $_POST['further_comments1'];
$stmt->execute();
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
<?php
require 'PHPMailerAutoload.php';
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "info#personalemail.com";
$mail->FromName = "Host Person";
//To address and name
$mail->addAddress("user#icloud.com", "User");
$mail->addAddress("info#personalemail.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("info#personalemail.com", "Reply");
//CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Guest Questionnaire Received";
// Build message.
$img="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res'].".jpg'>";
$img2="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res_information'].".jpg'>";
$img3="<img src='http://www.myurl.com/guestquestionnaire/images/star".$_POST['res_staff'].".jpg'>";
$message = '<html><body>';
$message .= "<strong>Guest Questionnaire </strong>";
$message .= "<strong>Received: </strong>";
$message .= "<P>";
$message .= '<table rules="all" style="border: 1px solid #999;" cellpadding="7" width="100%" >';
$message .= "<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'><td colspan='3'>Prior to Arrival</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td>What made you choose us for your recent trip? </td><td width='40%' colspan='2'>" . strip_tags($_POST['choice']) . "</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td>Did we meet your expectations as advertised? If no, please state why: </td><td width='40%' colspan='2'>" . strip_tags($_POST['expectations']) . "</td></tr>";
$message .= "<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'><td colspan='3'>Making your Reservation</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td>Ease of making your reservation: </td><td width='40%'>$img</td><td width='5%'>" . strip_tags($_POST['res']) . "</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td>Hotel information offered: </td><td width='40%'>$img2</td><td width='5%'>" . strip_tags($_POST['res_information']) . "</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td>Warmth and friendliness of staff: </td><td width='40%'>$img3</td><td width='5%'>" . strip_tags($_POST['res_staff']) . "</td></tr>";
$message .= "<tr style='font-size: 8pt;'><td colspan='3'>Further Comments: </BR></BR>" . strip_tags($_POST['further_comments1']) . "</td></tr>";
$message .= "</table>";
$message .= "<BR>";
$message .= "</table>";
$message .= "</body></html>";
$message .= "</body></html>";
$mail->Body = "".$message."";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="surveystyle.css" media="all" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery.inputfocus-0.9.min.js"></script>
<script type="text/javascript" src="/jquery.main.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<div id="container">
<div class="logo-header"><img src="images/logo.jpg" alt="" width="205" height="119" /></div>
I have really gone through so many different ways to identify that the problem is somewhere between this particular form, our server and Mac Mail. I believe something can be changed on this form to make it work (I just don't know what). The form displays fine on the webmail platform and other email accounts (not on our server) display the form perfectly fine.
Any help is GREATLY appreciated!!!
UPDATE:
I went through and removed each line to see if I could try and investigate error. It seems that the problem is to do with the length of the form. If there are 10 entries or so it goes through fine (regardless of what the field is) but once it's over 10 entries it starts giving all the weird $= all over the place. Very weird. Any ideas?

Sending email in HTML [duplicate]

This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?
Here is my script
<?php
//Script for sending a confirmation email
$body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<title>Demystifying Email Design</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
<table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
<tr bgcolor=\"#3498db\">
<td align=\"center\">
<img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
</td>
</tr>
<tr bgcolor=\"#ffffff\" align=\"center\">
<td>
<h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
</td>
</tr>
<tr align=\"center\" bgcolor=\"#ffffff\">
<td>
<h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
</td>
</tr>
</table>
</body>
</html>";
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
if (mail($to,$subject,$body)){
echo "Mail was sent";
} else{
echo "Failed";
}
This is the email I received
You need to also set a header indicating this is HTML, e.g.
// 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";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source (see Example 3).
Correct code will be:
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if (mail($to,$subject,$body,$headers)){
echo "Mail was sent";
}

Categories