Web Hosting Talk







View Full Version : Help: error with mail()


Myname
09-22-2003, 01:18 AM
Hi, I'm new user. I have an error with mail().
This script below can not send mail. I don't receive any mail.

<?php
require ("common.php");
include ("lang/vietnamese.php");
$error="0";
$email=strtolower($email);
$email = trim ($email);
// mail("tuquyenmail@somemail.com","","test"); But If I put mail() here I can receive my mail!!!.
?>
<html>
<head>
<LINK href="../images/style.css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p><font face="Verdana"><span style="font-size: 7pt">
<?
if ($message == "" || (check_phone ($phone)) || (($email != "") && (!check_mail ($email))))
{
$error="1";
print("<font color=\"#FF0000\"><b> $lang[error]</b>");

}
else
print("<font color=\"#333333\"><b>$lang[thanks]</b>");
?>
</span></font><br>
<br>
<font color="#333333"><span style="font-size: 7pt">
<?
if ($message == "")
print("$lang[meempty]<br>");
elseif (($email!="") && (!check_mail ($email)))
print("$lang[eminvalid]<br>");
elseif (check_phone ($phone))
print("$lang[phinvalid]");
else
print("$lang[sent]");
?>
</span></font></p>
<table width="20%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><IMG height=12 src="../images/puce-ok.gif" width=12 align=absMiddle><a href="#" onClick="history.back();">QUAY LẠI</a>
</td>
</tr>
</table>
<br>
</body>
</html>
<?
if ($error="0")
{
if ($email == "")
$email="someone@somewhere.com";
if ($name == "")
$name="Someone";
$message .="\r\n\r\n------------------------------------------------------";
if ($name == "")
$message .="\r\nName: <none>";
else
$message .="\r\nName: $name";
if ($email == "")
$message .="\r\nEmail: <none>";
else
$message .="\r\nEmail: $email";
if ($phone == "")
$message .="\r\nPhone: <none>";
else
$message .="\r\nPhone: $phone";
if ($address == "")
$message .="\r\nAddress: <none>";
else
$message .="\r\nAddress: $address";
if ($company== "")
$message .="\r\nCompany: <none>";
else
$message .="\r\nCompany: $none";
$headers = "From: \"".$name."\" <".$email.">\r\n";
$subject = "Khách viếng thăm từ trang 12a3mk.com";
mail("tuquyemail@somemail.com", $subject, $message, $headers); //mail() here I can not receive any mail.
}
?>

If I put mail() at orange line I can receive mail. I don't know why. Please help me. Thanks in advance.

-------------
Forgive me for poor English.

Burhan
09-22-2003, 05:04 AM
A few suggestions to help you debug :


if ($error="0")


Remember that = is assignment and == is comparison.

Don't put quotes around your From : email address. It should be :


$headers = "From: Name <email@domain.com>";


Since you are using non-ascii characters, you need to encode everything except the body of the message. Your subject needs to be encoded for vietnamese.

You can write your own encode function, but I think it would be easier for you to use a mailer class. There are a few available on the net, you might also want to check out PEAR for more email classes. There is an example encode function in the manual reference (http://www.php.net/manual/en/function.mail.php) for mail().

Hope this helps

Rich2k
09-22-2003, 05:13 AM
Yep because it's not ASCII you'll need to encode your message in either quoted-printable if it's 8 bit or base64 otherwise.

Myname
09-22-2003, 06:49 AM
Thank you very much.

if ($error="0")

=> Yes, it should be if($error=="0") (I know this) but I think it doesn't make mail() failed.

I changed if($error="0") to if($error=="0") but I don't receive any mail.

I deleted $error and changed some places (orange) and it worked! Could you please tell me why?

<?php
require ("common.php");
include ("lang/vietnamese.php"); // vietnamese.php saved as utf-8
$email=strtolower($email);
$email = trim ($email);
?>
<html>
<head>
<LINK href="../images/style.css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p><font face="Verdana"><span style="font-size: 7pt">
<?php
if ($message == "" || (check_phone ($phone)) || (($email != "") && (!check_mail ($email))))
{
print("<font color=\"#FF0000\"><b> $lang[error]</b></font><br><br>");
}
else
{
print("<font color=\"#333333\"><b>$lang[thanks]</b></font><br><br>");
?>
</span></font>

<font face="Verdana"><span style="font-size: 7pt">
<?php }

//I don't know why mail() worked when I put } after <?php. I see this in a guestbook program. If I don't changed like that, my browser will be closed suddenly when accesse my *.php! I don't know why!

if ($message == "")
print("$lang[meempty]<br>");
elseif (($email!="") && (!check_mail ($email)))
print("$lang[eminvalid]<br>");
elseif (check_phone ($phone))
print("$lang[phinvalid]");
else
{
print("$lang[sent]");
$message .="\n------------------------------------------------------";
if ($email == "")
$email="someone@somewhere.unknow";
if ($name == "")
$name="Someone";
if ($name == "")
$message .="\r\nName: <none>";
else
$message .="\r\nName: $name";
if ($email == "")
$message .="\r\nEmail: <none>";
else
$message .="\r\nEmail: $email";
if ($phone == "")
$message .="\r\nPhone: <none>";
else
$message .="\r\nPhone: $phone";
if ($address == "")
$message .="\r\nAddress: <none>";
else
$message .="\r\nAddress: $address";
if ($company== "")
$message .="\r\nCompany: <none>";
else
$message .="\r\nCompany: $company";
$youremail = "d_tliem@yahoo.co.uk";
$headers = "From: \"".$name."\" <".$email.">\r\n";
$subject = "Khách viếng thăm từ trang 12a3mk.com"; // No problem with Vietnamese here, I can read Vietnamese easily with Web mail

mail($youremail, $subject, $message, $headers);
}
?>

</span></font></p>
<table width="20%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><IMG height=12 src="../images/puce-ok.gif" width=12 align=absMiddle><a href="#" onClick="history.back();">BACK</a>
</td>
</tr>
</table>
<br>
</body>
</html>

$headers = "From: \"".$name."\" <".$email.">\r\n";
=> I copyed at php.net
$headers = "From: Name <$email>
=> Thanks. I will do like that from today.

I have used utf-8 to write Vietnamese and I don't meet any problem.

Khách viếng thăm từ trang 12a3mk.com => No problem with subject. I saved my file as utf-8. Can I do like this while I don't have any problem?

Burhan
09-22-2003, 12:39 PM
Originally posted by Myname
Thank you very much.

if ($error="0")

=> Yes, it should be if($error=="0") (I know this) but I think it doesn't make mail() failed.

I changed if($error="0") to if($error=="0") but I don't receive any mail.

I deleted $error and changed some places (orange) and it worked! Could you please tell me why?

<?php
require ("common.php");
include ("lang/vietnamese.php"); [b]// vietnamese.php saved as utf-8
$email=strtolower($email);
$email = trim ($email);
?>
<html>
<head>
<LINK href="../images/style.css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p><font face="Verdana"><span style="font-size: 7pt">
<?php
if ($message == "" || (check_phone ($phone)) || (($email != "") && (!check_mail ($email))))
{
print("<font color=\"#FF0000\"><b> $lang[error]</b></font><br><br>");
}
else
{
print("<font color=\"#333333\"><b>$lang[thanks]</b></font><br><br>");
?>
</span></font>

<font face="Verdana"><span style="font-size: 7pt">
<?php }

//I don't know why mail() worked when I put } after <?php. I see this in a guestbook program. If I don't changed like that, my browser will be closed suddenly when accesse my *.php! I don't know why!

if ($message == "")
print("$lang[meempty]<br>");
elseif (($email!="") && (!check_mail ($email)))
print("$lang[eminvalid]<br>");
elseif (check_phone ($phone))
print("$lang[phinvalid]");
else
{
print("$lang[sent]");
$message .="\n------------------------------------------------------";
if ($email == "")
$email="someone@somewhere.unknow";
if ($name == "")
$name="Someone";
if ($name == "")
$message .="\r\nName: <none>";
else
$message .="\r\nName: $name";
if ($email == "")
$message .="\r\nEmail: <none>";
else
$message .="\r\nEmail: $email";
if ($phone == "")
$message .="\r\nPhone: <none>";
else
$message .="\r\nPhone: $phone";
if ($address == "")
$message .="\r\nAddress: <none>";
else
$message .="\r\nAddress: $address";
if ($company== "")
$message .="\r\nCompany: <none>";
else
$message .="\r\nCompany: $company";
$youremail = "d_tliem@yahoo.co.uk";
$headers = "From: \"".$name."\" <".$email.">\r\n";
$subject = "Khách viếng thăm từ trang 12a3mk.com"; // No problem with Vietnamese here, I can read Vietnamese easily with Web mail

mail($youremail, $subject, $message, $headers);
}
?>

</span></font></p>
<table width="20%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><IMG height=12 src="../images/puce-ok.gif" width=12 align=absMiddle><a href="#" onClick="history.back();">BACK</a>
</td>
</tr>
</table>
<br>
</body>
</html>

$headers = "From: \"".$name."\" <".$email.">\r\n";
=> I copyed at php.net
$headers = "From: Name <$email>
=> Thanks. I will do like that from today.

I have used utf-8 to write Vietnamese and I don't meet any problem.


Read this


If your server doesn't have mb_send_mail() enabled but you want to use non-ascii (multi-byte) chars in an email's subject or name headers, you can use something like the following:

$charset = "iso-2202-jp"; // japanese
$to = encode("japanese name 01", $charset) . " <to@email.com>";
$from = encode("japanese name 02", $charset) . " <from@email.com>";
$subject = encode("japanese text");
$message = "does not need to be encoded";
mail($to, $subject, $message, $from);

function encode($in_str, $charset) {
$out_str = $in_str;
if ($out_str && $charset) {

// define start delimimter, end delimiter and spacer
$end = "?=";
$start = "=?" . $charset . "?B?";
$spacer = $end . "\r\n " . $start;

// determine length of encoded text within chunks
// and ensure length is even
$length = 75 - strlen($start) - strlen($end);
$length = floor($length/2) * 2;

// encode the string and split it into chunks
// with spacers after each chunk
$out_str = base64_encode($out_str);
$out_str = chunk_split($out_str, $length, $spacer);

// remove trailing spacer and
// add start and end delimiters
$spacer = preg_quote($spacer);
$out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
$out_str = $start . $out_str . $end;
}
return $out_str;
}
// for details on Message Header Extensions
// for Non-ASCII Text see ...
// http://www.faqs.org/rfcs/rfc2047.html


From http://www.php.net/manual/en/function.mail.php

Rich2k
09-22-2003, 03:46 PM
for text that's 7bit ASCII use plain text, for 8bit use quoted-printable and for everything else use base64

blackbelt080
09-22-2003, 04:54 PM
$email="someone@somewhere.unknow";

Try this: $email=someone@somewhere.unknow;

Take care,
Lori :D

Rich2k
09-22-2003, 05:10 PM
That code will give you a parse error.

$email="someone@somewhere.unknow";

was fine.

blackbelt080
09-22-2003, 07:22 PM
I misread the line :( Sorry...
Thanks for catching that!

Take care,
Lori

Myname
09-23-2003, 12:27 AM
Thank you very much. I think it's enough.