Connecting Tech Pros Worldwide Forums | Help | Site Map

HTML mail send in php

Newbie
 
Join Date: Jan 2008
Posts: 7
#1: Jan 22 '08
Hi all, i am new in php, I looked lots of example from Forum and tried online server but its nt work at all, i know i may be doing great mistake to sending HTML mail. last time i tried this code beloo, pls give me idea what is the erroe and its say "Mail Failed'

<?php
$to = 'example@test.com';
$subject = 'Test HTML email';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

//define the body of the message.

ob_start(); //Turn on output buffering
?>

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
what i need to do clearly if i want to send HTML email?
&
what need to do for testing on my PC in xp OS
&
what i need to do on real server when i will keep it?
Thx a lot

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,947
#2: Jan 22 '08

re: HTML mail send in php


Take away the '@' symbol before $mail_sent to stop it supressing any errors.

See if it now shows you an error
Newbie
 
Join Date: Jan 2008
Posts: 7
#3: Jan 22 '08

re: HTML mail send in php


Quote:

Originally Posted by markusn00b

Take away the '@' symbol before $mail_sent to stop it supressing any errors.

See if it now shows you an error

Hi,
I removed @sybbol still same problem, do i need to edit or do something on server? sorry i have no idea abt this, Thx a lot
stepterr's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 144
#4: Jan 22 '08

re: HTML mail send in php


Quote:

Originally Posted by redblue

Hi,
I removed @sybbol still same problem, do i need to edit or do something on server? sorry i have no idea abt this, Thx a lot

RedBlue,
I just tried it on my server and the code is working, even with the @ symbol. So unfortunately it isn't something easily detectable in your code. How are you trying to run this on your PC?
Newbie
 
Join Date: Jan 2008
Posts: 7
#5: Jan 22 '08

re: HTML mail send in php


Quote:

Originally Posted by stepterr

RedBlue,
I just tried it on my server and the code is working, even with the @ symbol. So unfortunately it isn't something easily detectable in your code. How are you trying to run this on your PC?

Hi Stepterr,

I just run on my pc simpley as any php file. also on the srever same. i mean is it anything to do i mean some edit or change on server? pls tell me ? i never done mail related thing on web. Thx a lot

after test on my pc windows xp OS: the eroor is :
Fatal error: Call to undefined function: ob_get_clean() in C:\apache\htdocs\email\mail.php on line 30
and online server is : fail to send!
stepterr's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 144
#6: Jan 22 '08

re: HTML mail send in php


Quote:

Originally Posted by redblue

Hi Stepterr,

I just run on my pc simpley as any php file. also on the srever same. i mean is it anything to do i mean some edit or change on server? pls tell me ? i never done mail related thing on web. Thx a lot

after test on my pc windows xp OS: the eroor is :
Fatal error: Call to undefined function: ob_get_clean() in C:\apache\htdocs\email\mail.php on line 30
and online server is : fail to send!


Do you know what version of PHP you are using? ob_get_clean() is a standard function since PHP 4.3.0. That's probably what the issue is. To get around this you could just add the following code.

[PHP]
<?php
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();
ob_end_clean();
return $ob_contents;
}
}
?>
[/PHP]

While this should help you get through, you may still want to get a newer verison to keep yourself from getting hung up on other things as well.
Newbie
 
Join Date: Jan 2008
Posts: 7
#7: Jan 22 '08

re: HTML mail send in php


Quote:

Originally Posted by stepterr

Do you know what version of PHP you are using? ob_get_clean() is a standard function since PHP 4.3.0. That's probably what the issue is. To get around this you could just add the following code.

[PHP]
<?php
if (!function_exists("ob_get_clean")) {
function ob_get_clean() {
$ob_contents = ob_get_contents();

Hi Steperr,
I included this code u provided , Now there is no error like before , but say "mail failed" i mean mail cant sent?
Newbie
 
Join Date: Jan 2008
Posts: 7
#8: Jan 24 '08

re: HTML mail send in php


Quote:

Originally Posted by redblue

Hi Steperr,
I included this code u provided , Now there is no error like before , but say "mail failed" i mean mail cant sent?

Hi Steperr,
I think a little problem now, I really cant understand why its say Mail faild? for sending mail should i make change any properties of any file on server or on my PC? Thx
Reply