Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

php - Mail returns false

I'm currently using a custom made library at my job. Until just rencently the library was working perfectly. It apparently return false since about today.

The library itself it basically a wrapper around the function mail. It builds the "boundaries" parts and everything.

Since the class is quite big enough I wont post it here ... but I'm wondering, what are the reasons in theory of why mail would return false?

  • SMTP is set in PHP.ini
  • Sender is set in headers
  • Sender is int the form of: sender<sender@email.com>
  • Everything is sent correctly (body+headers+subject)
  • Assume that mail( ) works correctly on the website but on this specific page it just doesn't. I know it must be comming from me but would be fun to have somewhere to start looking for.
  • Oh and yeah the library is undocumented.

[edit] Just found a smaller function and still doesn't work, I'll print it out then:

function send_html($from, $email, $subject = "AUCUN", $message, $cc = "", $bcc ="", $priotity = "3") {
    $headers = "";
    $headers .= "MIME-Version: 1.0
"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1
";

    if (strpos($from, "ourwebsite.com") != false || strpos($from, "rencontresportive.com") != "") {
        $headers .= "From: Ourwebsite.com <" . $from . ">
";
    } else {
        $headers .= "From: " . $from . " <" . $from . ">
";
    }

    $headers .= "X-Sender: <" . $from . ">
";
    $headers .= "X-Priority: " . $priotity . "
";
    $headers .= "X-Mailer: PHP
";
    $headers .= "Return-Path: <admin@ourwebsite.com>
";

    if ($cc != "") {
        $headers .= "cc:" . $cc . "
";
    }
    if ($bcc != "") {
        $headers .= "bcc:" . $bcc . "
";
    }
        if (mail($email, $subject, $message, $headers)) {
        return true;
    } else {
        return false;
    }
}

I called it with :

send_html(contact@ourwebsite.com, me@me.com, utf8_decode("the subject"), "<h1>test</h1>");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just had the same problem. After a php upgrade, the mail function always returns false.

I used this little snippet to check it out:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo 'I am : ' . `whoami`;
$result = mail('myaddress@mydomain.com','Testing 1 2 3','This is a test.');
echo '<hr>Result was: ' . ( $result === FALSE ? 'FALSE' : 'TRUE') . $result;
echo '<hr>';
echo phpinfo();

The solution was setting a value in my php.ini for 'sendmail_from' and 'sendmail_path'. The correct values in my case were:

sendmail_from = "no-reply@mydomain.net"
sendmail_path = "/usr/sbin/sendmail -t -i"

(I'm using CentOS 5.3 w/ Zend Server CE.)

You could use ini_set() to set the value of 'sendmail_from', but the 'sendmail_path' var must be setup in your php.ini or http.conf.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...