Send HTML in email via PHP
It is pretty simple. Leave the images on the server and send the PHP + CSS to them...
$to = 'bob@example.com';
$subject = 'Website Change Request';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$message = '<p><strong>This is strong text</strong> while this is not.</p>';
mail($to, $subject, $message, $headers);
It is this line that tells the mailer and the recipient that the email contains (hopefully) well-formed HTML that it will need to interpret:
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
Here is the link I got the information from... (link)
You will need security though...
Send HTML email with php?
You need to send full headers with content type, not just mail_from. Heres an example
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= $mail_From . "\r\n";
mail($mail_To,$mail_Subject,$mail_Body,$headers);
http://www.w3schools.com/php/func_mail_mail.asp
In your code:
$mail_From = $From_email;
$mail_To = $payer_email;
$mail_Subject = $Subject_line;
$mail_Body = $email_msg;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
The $mail_From is the fourth parameter (headers). Create a new string that contains full headers for html email plus: $_From
$mail_From = $From_email;
$mail_To = $payer_email;
$mail_Subject = $Subject_line;
$mail_Body = $email_msg;
//start $headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; //adds content type to headers
$headers .= $mail_From . "\r\n"; //adds the sender details
mail($mail_To,$mail_Subject,$mail_Body,$headers); //sends the email
If you echo $headers it will be something like this
MIME-Version:1.0
Content-type:text/html;charset=iso-8859-1
From:email@email.com
Sending HTML Email via PHP with Variables and External HTML Template
A better approach would be to just include the file and use ob_get_clean()
:
ob_start();
if($_POST['service']=="Service 1") {include 'email_template-service-1.php';}
else include 'email_template-service-2.php';
$message = ob_get_clean();
Anything echoed between ob_start()
and $message = ob_get_clean()
will go into the $message
variable.
Send email with PHP from html form on submit with the same script
EDIT (#1)
If I understand correctly, you wish to have everything in one page and execute it from the same page.
You can use the following code to send mail from a single page, for example index.php
or contact.php
The only difference between this one and my original answer is the <form action="" method="post">
where the action has been left blank.
It is better to use header('Location: thank_you.php');
instead of echo
in the PHP handler to redirect the user to another page afterwards.
Copy the entire code below into one file.
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Original answer
I wasn't quite sure as to what the question was, but am under the impression that a copy of the message is to be sent to the person who filled in the form.
Here is a tested/working copy of an HTML form and PHP handler. This uses the PHP mail()
function.
The PHP handler will also send a copy of the message to the person who filled in the form.
You can use two forward slashes //
in front of a line of code if you're not going to use it.
For example: // $subject2 = "Copy of your form submission";
will not execute.
HTML FORM:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP handler (mail_handler.php)
(Uses info from HTML form and sends the Email)
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
To send as HTML:
If you wish to send mail as HTML and for both instances, then you will need to create two separate sets of HTML headers with different variable names.
Read the manual on mail()
to learn how to send emails as HTML:
- http://php.net/manual/en/function.mail.php
Footnotes:
- In regards to HTML5
You have to specify the URL of the service that will handle the submitted data, using the action attribute.
As outlined at https://www.w3.org/TR/html5/forms.html under 4.10.1.3 Configuring a form to communicate with a server. For complete information, consult the page.
Therefore, action=""
will not work in HTML5.
The proper syntax would be:
action="handler.xxx"
oraction="http://www.example.com/handler.xxx"
.
Note that xxx
will be the extension of the type of file used to handle the process. This could be a .php
, .cgi
, .pl
, .jsp
file extension etc.
Consult the following Q&A on Stack if sending mail fails:
- PHP mail form doesn't complete sending e-mail
Php mail: how to send html?
use this header for the mail:
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
and for the content/body:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...
it's important to use inline css commands and recommanded to use tables for the interface.
...
In your Mail-Body you than have to put HTML code with head and body
Send HTML Email With Attachments PHP
Actually I wanted to share this code snippet with community, I thought if I click on Answer your question, this question will get posted just as a topic. But let it be,
I hope other users might find it useful.
Related Topics
List All Files in One Directory PHP
How Can Strip Whitespaces in PHP'S Variable
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
.Htaccess Rewrite Get Variables
PHP Script to Loop Through All of the Files in a Directory
Replace a Whole Line Where a Particular Word Is Found in a Text File
How to Find Out Where a Function Is Defined
Converting Words to Numbers in PHP
PHP String Concatenation, Performance
How to Implement a Web Scraper in PHP
How to Fake $_Server['Remote_Addr'] Variable
How to Require a Fork With Composer
Insert Multiple Rows With One Query MySQL
How to Check What User PHP Is Running As
Difference Between JavaScript and PHP
What's the Deal With a Leading Underscore in PHP Class Methods