Jump to content

Why doesn't mail() work?


TerryR

Recommended Posts

here is a simple script. It should work fine.

[code=php:0]<?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>[/code]
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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:

[code="php"]
$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);
[/code]
(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.
Link to comment
Share on other sites

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

[code=php:0]
$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
}

[/code]

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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/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="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>
Link to comment
Share on other sites

[code]
mail($to, $subject, $message, "From: me<terry654321@hotmail.co.uk>\nX-Mailer: PHP/") or die("Unable to send mail");
[/code]
thats not error handling
[code]
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";
}
[/code]

this should tell you the truth and put ini_set("display_errors", "1"); in the starting, befor anything.
Link to comment
Share on other sites

go to this line:

[code]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";[/code]

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

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

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

Link to comment
Share on other sites

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:
[code]<?php
//
// all your other header set up
//
$fifth = '-f email@your.domain.here';
mail($to,$subject,$body,$headers,$fifth);
?>[/code]

Ken
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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.

[code=php:0]
/*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";
}
[/code]
Link to comment
Share on other sites

There are some questions that haven't been asked of the OP yet.

1) Is this one a shared hosting machine or one host per machine?
2) Is this your own machine or are you buying (or using) site space, if so who is hosting your site?
3) Which web server software is being used?
4) Windows or Unix/Linux?
5) If this is a Unix/Linux box do you have shell access?
6) Is there a web based interface to the administration end of the host, for example cPanel?
7) What version of PHP?
8.) Have you talked to your Tech Support about looking at the mail logs?

The answers are all relevent to trying to get an answer to your problem.

Ken
Link to comment
Share on other sites

Ken,
Here are my answers:
1. I've tried on two hosts, both with many users - does that mean shared?
2. I'm buying site space on one server, but at present trying things out on my college system
3. I don't know
4. My machine is Windows XP
5. n/a
6. Yes, for the site I am buying, but no for my college site
7. 5.1.4
8. Not yet, but I will tomorrow
Link to comment
Share on other sites

Ronald, I think you might just have cracked it for me. This is what I've got:

SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

It feels wrong to have localhost, but I don't know what to have instead. Could you explain a bit more please?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.