Hi,
I am new to php, having come from a C++ background. I am trying to do something that should be fairly simple but have been banging my head against the wall trying to get it to work and not finding the answer on any forums. I am hoping that someone will be able to help me so that i can continue and learn more php functionality.
I am building a program which is supposed to take user input from a html form, insert it into a letter and email it in html format. This is something you see all over the web already. For instance, the user inputs her name and email address in to the html form, and then receives a pretty html email to her address, with the header saying "Dear Whomever Youare".
Here is what I have done so far:
Right now the user fills out the form and clicks on the submit button.
The values are assigned to the php variables.
Another php script is called using include('english.php'); or whatever language was chosen. What is displayed on the screen looks perfect.
The following lines are used to send the email
ob_start();
$emailBody = ob_get_clean();
mail( $email, $emailSubject, $emailBody, $headers);
What the recipient gets is an email that contains the contents of the enlish.php file as code, not as formatted html.
Does anyone know how to do this seemingly simple task?
Thanks,
MJ
emailing html form
Moderator: General Moderators
Re: emailing html form
Hi,
to send an HTML email, you must create the email to spec, and then include the right headers. I do it like this:
It's obviously a bit from a larger piece of code, but it does work for me. Let me know if you need any clarification.
to send an HTML email, you must create the email to spec, and then include the right headers. I do it like this:
Code: Select all
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html;charset=utf-8"."\r\n";
if(basicCheckEmail($contact)) $headers.='Reply-To: <'.$contact.'>'."\r\n";
// More headers
$headers .= 'From: <generic@example.com>' . "\r\n";
$line = "\r\n";
$message = '
<html>
<style>
body{
font-family: sans-serif;
color: #333;
}
a{
color: #800;
}
a:hover{
color: #f00;
}
</style>
<body>
<div style="
font-family: sans-serif;
color: #333;
font-size: 11pt;
margin-left: 5%;
margin-right: 5%;
padding: 12pt;">
<b>From:</b> '.$from.'<br/>'.$line.'
<b>Sent:</b> '.date('F d, Y (l)').'<br/>'.$line.'
<b>Contact:</b> '.$contact.'<br/>'.$line.'
<b>Message:</b><br/>'.$line.'<br/>'.$line.'
'.nl2br($message).'
</div>
</body>
</html>
';
if(mail($to, $subject, $message, $headers)) echo 'SENT!';Re: emailing html form
That was amazingly helpful. Thank you very much.
As I suspected, the missing bit was the header section
"Content-type:text/html;charset=utf-8"
Now I can get move on and learn some more interesting stuff.
Thank you again.
As I suspected, the missing bit was the header section
"Content-type:text/html;charset=utf-8"
Now I can get move on and learn some more interesting stuff.
Thank you again.
Re: emailing html form
This is moving along quite nicely, but I have hit another obstacle similar to the last one. The problem comes from working with french characters, such as éèçà and so on.
I have an html web form which the user fills out. Values are then taken from said form and inserted into an html letter (for lack of a better term) using php. The values that are inserted show up just fine. The problem is the text that was already in the html letter loses its frenchness, with the characters turning into garbage characters. This happens when the values are loaded into the buffer like this
ob_start();
if ( $language == "en" ) { include('english.php'); }
if ( $language == "fr" ) { include('french.php'); }
$emailBody = ob_get_clean();
where the french.php file contains the html letter and the php code that inserts the values. The reason I say this is happening during the buffering and not the sending is because if I echo $emailBody, it also comes out with garbage characters.
I am hoping that this is another one line problem. I was hoping that by simply changing the character set from UTF-8 to ISO-8859-15 this would fix it, but clearly it didn't. I also suspected that it could be incorrect placement of <meta http-equiv="content-type" content="text/html; charset=utf-8">. I still think that may be the case, but I have had no success by moving it around.
Thanks in advance to anyone who takes the trouble to read this.
I have an html web form which the user fills out. Values are then taken from said form and inserted into an html letter (for lack of a better term) using php. The values that are inserted show up just fine. The problem is the text that was already in the html letter loses its frenchness, with the characters turning into garbage characters. This happens when the values are loaded into the buffer like this
ob_start();
if ( $language == "en" ) { include('english.php'); }
if ( $language == "fr" ) { include('french.php'); }
$emailBody = ob_get_clean();
where the french.php file contains the html letter and the php code that inserts the values. The reason I say this is happening during the buffering and not the sending is because if I echo $emailBody, it also comes out with garbage characters.
I am hoping that this is another one line problem. I was hoping that by simply changing the character set from UTF-8 to ISO-8859-15 this would fix it, but clearly it didn't. I also suspected that it could be incorrect placement of <meta http-equiv="content-type" content="text/html; charset=utf-8">. I still think that may be the case, but I have had no success by moving it around.
Thanks in advance to anyone who takes the trouble to read this.
Re: emailing html form
try processing with htmlentities()?