By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

HTML mail send in php

P: 7
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
Jan 22 '08 #1
Share this Question
Share on Google+
7 Replies


Markus
Expert 5K+
P: 6,050
Take away the '@' symbol before $mail_sent to stop it supressing any errors.

See if it now shows you an error
Jan 22 '08 #2

P: 7
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
Jan 22 '08 #3

stepterr
100+
P: 157
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?
Jan 22 '08 #4

P: 7
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!
Jan 22 '08 #5

stepterr
100+
P: 157
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.
Jan 22 '08 #6

P: 7
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?
Jan 22 '08 #7

P: 7
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
Jan 24 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.