This question already has answers here:
PHP Send HTML mail
(3 answers)
Closed 7 years ago.
I've found instructions online how to do this, but it's not working for me... My shortened PHP message is:
$msg = '<html><body>';
$msg .= 'Dear' .$firstname. 'Best wishes';
$msg .= '</body></html>';
I'm intending to actually add some html INTO the message, but this just comes out with:
<html><body>
Dear Sam Best wishes
</body></html>
What am I missing?! Thanks.
Please follow this instructions, that is added in official documentation. And here is a working example that you could compare to your code.
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
$subject = 'Birthday Reminders for August';
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I would like to send an email using php and I have used this code:
<?php
// multiple recipients
$to = 'x#example.com' . ', '; // note the comma
$to .= 'y#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
What I do is to save this file as a example.php file and upload it to a directory in my ftp. Then I use a browser to go to site.com/directory/example.php
and I think the code runs but it can not send the emails.
Should I configure anything else in the cpanel or the server to use this code?
<?php
$to = "abc#example.com";
$subject = "This is subject of mail";
$message = "
<html>
<head>
<title>".$subject."</title>
</head>
<body>
<p>This here is the description of mail</p>
</body>
</html>
";
// 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: thesender#example.com' . "\r\n";
mail($to,$subject,$message,$headers);
echo "<script> alert('Form Succesfully Submitted'); window.location.href='contact.php'; window.location.href='contact.php'; </script>";
?>
Try this out.
If you need the Cc:, or Bcc include them after in headers variable.
I am building a website contact form that uses MAILTO to send text that the user submits via the web form. However, the email account that receives the MAILTO text comes in 'ugly' and was wondering if there was a way to get that text into an HTML format to enhance visually without having to rely on some back-end script.
Or, is there a better way to do this?
Thanks.
you can't format html body using mailto, you need to use mail() function of php,
here is short example:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
more detail: http://php.net/manual/en/function.mail.php
i need to send a mail from CRM to a particular gmail account using php , the from and to mail id's are default.the user need to enter the subject and the body of the mail only in graphical view like as outlook express.
I think this manual will help You
Mail php manual
sample script
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I am trying to create an autoresponder email and the content is a PHP variable. I want it to output HTML code and right now it's not.
//Example:
$respondmessage = " Hello $fullname,
We are confirming your Appointment today!
Please click here to confirm!
";
This outputs:
Hello Your Name,
We are confirming your Appointment today!
Please click here to confirm!
In the email. Is there a way to make the email accept HTML code?
You can following line as an header in your code :
$headers = "Content-type: text/html\r\n";
This will help you to create email into HTML
Assuming you're using the built in mail function in PHP:
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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";
// Mail it
mail($to, $subject, $message, $headers);
?>
This example is from: http://us2.php.net/manual/en/function.mail.php
The important part is the $headers variable.
By the way, as andrewsi mentioned - you need to escape your quotes:
$respondmessage = "Hello $fullname,
We are confirming your Appointment today!
Please click here to confirm!";
You have to include this header to your email script
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
From http://php.net/manual/en/function.mail.php example# 4
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I'm still struggling with this mail script - I'm now getting all the marked up html through rather than seeing it as rendered html if that makes sense?
<?php
$mailheader .= "MIME-Version: 1.0\r\n";
$mailHeader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$formcontent .="<table border='1'>";
foreach ($_POST as $field=>$value)
{
$formcontent.="<tr>";
$formcontent .= "<td>$field:</td> <td>$value</td>";
$formcontent.="</tr>";
}
$formcontent .= '<tr><td>User-Agent: </td><td>'.$_SERVER['HTTP_USER_AGENT'].'</td>';
$formcontent .="</table>";
$recipient = "info#*******.com";
$subject = "Event feedback form";
$mailheader = "From: web.form#*******-events.co.uk\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailHeader .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
header("location:http://www.******-events.co.uk");
?>
Follow php's documentation:
You'll need html tags
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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 <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I'd rather use a PHP class as PHPMailer for HTML emails. And btw I will add the full HTML document tags (html, head, body, etc...) for the mail body.
I'd missed these:
$formcontent = '<html><body>';
Thanks for everyone's time and input though
Jim