Jump to content


Photo

Why doesn't mail() work?


  • Please log in to reply
27 replies to this topic

#1 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 July 2006 - 11:08 AM

>:(
I just want to send emails using the mail() function. It returns True, but doesn't actually send the email! Any ideas please?

#2 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 30 July 2006 - 11:10 AM

Do you know if your code's correct? or what? Post your code and we could take a look.
Link shortener with advanced, detailed statistics:

http://tyny.us/

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 11:45 AM

here is a simple script. It should work fine.

<?php
if (isset($send) {
    if ((!$to) || (!$subject) || (!$message)) {
        echo "You did not submit the following required information";
        if (!$to) {
             echo "Must enter a email address to send the mail";                                        
        }
        if (!$subject) {
            echo "You must enter a subject";
        }
        if (!message) {
            echo "You must enter a message";
        }
        exit;
    }
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$to = stripslashes($to);
$subject = stripslashes($subject);
$message = stripslashes($message);

mail($to, $subject, $message, "From: You<you@yoursite.com>\nX-Mailer: PHP/" . phpversion()) or die("Unable to send mail"); 
echo "Your email was sent successfuly";
}
?>					
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Send mail Script</title>
</head>

<body>
<form action="send.php" method="post">
<p>To</p>
<p><input type="text" name="to" size="20"></p>
<p>Subject</p>
<p><input type="text" name="subject" size="20"></p>
<p>Message</p>
<p><textarea cols="38" rows="10" name="message"></textarea></p>
<input type="submit" name="send" value="Send">
</form>
</body>
</html>

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 July 2006 - 11:50 AM

Thanks for the quick replies. Here's my simplified test code that still won't work:

<?php
// The message
$message = "Line 1\nLine 2\nLine 3";

// Send
$result=mail('terry.richter@virgin.net', 'My Subject', $message);
if ($result){
    echo "true";
} else echo "False";
?>

The function returns True (= 1), but either the mail is being sent into a black hole in cyberspace, or isn't being sent at all. I'm certainly not receiving anything!

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 30 July 2006 - 11:54 AM

try that code I posted above You will need to change the From You<you@yoursite.com> To your actual email address. Also you will need to name it send.php

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 silver32

silver32
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 30 July 2006 - 02:35 PM

you forgot to set senders e-mail, should be

$result=mail('terry.richter@virgin.net', 'My Subject', $message, "From: You<you@you.com>" );

#7 para11ax

para11ax
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationColumbus, OH

Posted 30 July 2006 - 03:53 PM

The host I use recently put some restrictions on mail() to avoid it being hijacked for spamming easily.  They require a good amount of headers to be submitted with the mail() for it to go through:

$headers  = "From: MyName <".$fromemail.">\r\n";
$headers .= "Reply-To: <".$fromemail.">\r\n";
$headers .= "X-Sender: <".$fromemail.">\r\n";
$headers .= "X-Mailer: PHP4\r\n"; //mailer
$headers .= "X-Priority: 3\r\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: <".$fromemail.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n\n\r\n";

mail($toemail, $subject, $message, $headers);
(you define $fromemail, $toemail, $subject, $message before this)

This might be worth a shot if you are still having problems.  Otherwise you can try contacting your host to see if they have disabled it (or check the phpinfo() output to see).  Hope this helps.

#8 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 30 July 2006 - 03:53 PM

Some mail systems auto delete emails without proper headers, your format does not have any headers
the From does not always matter (it will be from nobody@server.com or simular)

my Basic PHP Mail

$to = "info@example.com";

$subject = "This is an Email";

$body = "This is the email body,<br /> I tend to use HTM format\n";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //This is for HTM Format
$headers .= 'To:  Name<'.$_to.'>,' . "\r\n";
$headers .= 'From: CompanyName<info@example.net>' . "\r\n";

if(mail($to, $subject, $body, $headers){
echo "Mail should have sent";
}else{
echo "There must have been an error
}


If this fails, you need to look at
setting the information
I think it was php_ini["smtp"];
I cant remember tho, so dont hold me against that
Tell me the problem, I will try tell you the solution

#9 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 July 2006 - 05:38 PM

Thanks to all replies, but sorry, I've tried them all and still no luck. What should I look for in phpinfo to see if mailing has been disabled?

#10 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 960 posts

Posted 30 July 2006 - 05:48 PM

Instead of
"From: You<you@yoursite.com>\nX-Mailer: PHP/"
"From: You<you@yoursite.com>\r\n"

and something like this before the mail function
ini_set("sendmail_from", "root@des-otoole.co.uk");


#11 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 July 2006 - 06:30 PM

Still no progress. It just says the mail has been sent. Maybe it has - but I sure ain't receiving it!

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 30 July 2006 - 06:47 PM

the mail might go in a spam folder check ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 July 2006 - 09:44 PM

Thanks for the suggestion, but I don't have a spam folder set up on the account I'm using. I'm using Outlook Express, and can send emails to myself perfectly well.

#14 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 01 August 2006 - 12:03 AM

Its not the account which makes the mail go to SPAM account
Its to do with Mail Headers, the mail system, Your POP3 Account Holder, Before outlook express reads mails.
This can tell if the email come from a website or pop 3 account ...
do you have spam assasin running on your account (Check Cpanel)
try the same thing with a Hotmail account or something
Tell me the problem, I will try tell you the solution

#15 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 August 2006 - 11:18 AM

I set up a new Hotmail account and tried it, but still no joy. This is the actual code I'm using, though I have tried the other suggestions too. The file is called mailtest3.php. I get the "Your email was sent successfuly" message, but no message arrives.

<?php
if (isset($_POST['btnSend'])) {
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];

    if ((!$to) || (!$subject) || (!$message)) {
        echo "You did not submit the following required information:\n";
        if (!$to) {
            echo "Must enter a email address to send the mail\n";                                       
        }
        if (!$subject) {
            echo "You must enter a subject\n";
        }
        if (!message) {
            echo "You must enter a message\n";
        }
        exit;
    }
else {

$to = stripslashes($to);
$subject = stripslashes($subject);
$message = stripslashes($message);

mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail");
echo "Your email was sent successfuly";
}
}
else
{echo "btnSend not set";}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR...ml4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Send mail Script</title>
</head>

<body>
<form action="mailtest3.php" method="post">
<p>To</p>
<p><input type="text" name="to" size="40" value = 'terry654321@hotmail.co.uk'></p>
<p>Subject</p>
<p><input type="text" name="subject" size="40" value = 'xxx' ></p>
<p>Message</p>
<p><textarea cols="38" rows="10" name="message">time is now <?php time() ?> </textarea></p>
<input type="submit" name="btnSend" value="Send">
</form>
</body>
</html>


#16 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 01 August 2006 - 11:23 AM

mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail"); 
thats not error handling
if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail"))
{
echo "postd";
}
else
{
echo "no post";
} 

this should tell you the truth and put ini_set("display_errors", "1"); in the starting, befor anything.
I am a PHP Guru, ask me questions if you want to

#17 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 01 August 2006 - 11:25 AM

go to this line:

mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail");
      echo "Your email was sent successfuly";

See, it will automatically give you that message, try to put an IF stement.

So, try this:
if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/")){
      echo "Your email was sent successfuly";
}

I'm new to PHP so IDK if that syntax is right, hopefully someoen can correct me if im wrong.


Link shortener with advanced, detailed statistics:

http://tyny.us/

#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 11:55 AM

You may need to use the little known and little used fifth parameter to the mail() function that sets the "Return-path:" header. Many sites, including Hotmail, Yahoo, and AOL are rejecting or just dropping the messages if the domain in the Return-Path header doesn't match that in the "From:" header. It won't if you're using shared hosting. To use:
<?php
//
// all your other header set up
//
$fifth = '-f email@your.domain.here';
mail($to,$subject,$body,$headers,$fifth);
?>

Ken

#19 TerryR

TerryR
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 01 August 2006 - 12:22 PM

I take the point about the If statement, and the fifth parameter, so I've now got

$fifth = '-f terry654321@hotmail.co.uk';
if(mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/",$fifth))
{
echo "posted";
}
else
{
echo "no post";
}

... and it still doesn't work!

#20 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 01 August 2006 - 12:36 PM

Do you have your own mail server or are you useing a hosting service? The reason that I ask is some mail servers require that you identify your self as a known sender. Here is an example of a simple way to send mail with Mecury Mail.

/*This should be an email address known to the server. Unless you are 
using Hot mail to as your out going mail server, do not use your hotmail address.
Use an email address that would be known to the mail server*/
$knownsender = "you@yoursite.com"; 
$mailtos = "email";// the email address the mail is being sent to 
$subject = "Your subject";
$message = "Your message";
}
if ($ccaddress=="" || $ccaddress==" "){
	$header="From: $knownsender";
}else{
	$header .="From: $knownsender\r\n";
	$header .=" Cc: $ccaddress";
}
// $header.="Bcc: $bcaddress";

if (@mail($mailtos, $subject, $message, $header)){
	echo "It worked";
}else{
	echo "There was an error sending your email";
}

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users