mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kippy
Forum Commoner
Posts: 84
Joined: Wed Jun 07, 2006 8:25 pm

mail()

Post by kippy »

If I am trying to send html through mail(), how do I get the message to show up correctly without displaying all of the html code? Thanks.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: mail()

Post by andyhoneycutt »

You'll need to supply a content-type with your message. See Example #4 Sending HTML email from php.net's documentation on the mail() function.

-Andy
kippy
Forum Commoner
Posts: 84
Joined: Wed Jun 07, 2006 8:25 pm

Re: mail()

Post by kippy »

I followed #4 and added in the header and it is still spitting out html code. Any thoughts?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: mail()

Post by omniuni »

I have this working for some of my websites. Can you post your code here so we can take a look at it?
kippy
Forum Commoner
Posts: 84
Joined: Wed Jun 07, 2006 8:25 pm

Re: mail()

Post by kippy »

Basically it a simple html page that I have created which includes a a few styles declared in header saved in $msg. the php I am using is:

$to=$_GET['address'];
$subject="subject";
$mailheaders = 'MIME-Version: 1.0' . "\r\n";
$mailheaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$mailheaders="From: domain<>\n";
$mailheaders.="Reply-To: email@domain.com\n\n";


mail($to, $subject, $msg, $mailheaders);

I hope this helps!
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: mail()

Post by starram »

function SendMail_MultiPart($HTMLBody,$TEXTBody,$mail_to,$mail_subject,$mail_from)
{
$email_body = "\n--_jkkdsffds32432dlkjifewks_\n";
$email_body .= "Content-type: text/plain\n\n";
$email_body .= $TEXTBody;
$email_body .= "\n--_jkkdsffds32432dlkjifewks_\n";
$email_body .= "Content-type: text/html\n\n";
$email_body .= $HTMLBody;
$email_body .= "\n--_jkkdsffds32432dlkjifewks_--\n";

$content_type = 'Content-Type: multipart/alternative; boundary="_jkkdsffds32432dlkjifewks_";\n charset=iso-8859-1\n';
$headers = "From: ".$mail_from."\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= $content_type . "\n";
return (mail($mail_to,$mail_subject,$email_body,$headers))?true:false;
}

Use above function.

Call like this
$msg1=str_replace("<br>","\n",$msg;);

SendMail_MultiPart($msg, $msg1, $to, $subject, $mailfrom);
kippy
Forum Commoner
Posts: 84
Joined: Wed Jun 07, 2006 8:25 pm

Re: mail()

Post by kippy »

Is it not possible to use divs, etc with styles? Thanks.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: mail()

Post by starram »

You can use that.
jmicozzi
Forum Newbie
Posts: 2
Joined: Fri Sep 05, 2008 2:06 pm

Re: mail() sending html from a form

Post by jmicozzi »

In my code when I have the message variable hard coded to the message i.e.
$msg = '<html><body><h1>Hello World</h1></body></html>';

I am able to receive the message in html format, that is the String "Hello World" is bolded etc.

When I read the above string from a textarea on my form ($msg = $_POST['msg'];) I receive the contents (in this case "Hello World") but just in plain text.

Any ideas as to why?

Thanks
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: mail()

Post by starram »

I m sorry but I need to clear something.

Are you entering following text in the text area
<html><body><h1>Hello World</h1></body></html>

?????

If you simply use <h1> tag that will also work.
I used the same thing and it worked perfectly for me.

Try entering only hello world in the textbox
and give other tags hardcoded. If you are not doing so..

Let me know how it goes?
jmicozzi
Forum Newbie
Posts: 2
Joined: Fri Sep 05, 2008 2:06 pm

Re: mail()

Post by jmicozzi »

The problem is I get different results, if the string is hard coded everything works fine, if I read the same string from a textarea, the visual results are different.
Post Reply