Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mail returns false

Tags:

php

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<[email protected]>
  • 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\r\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

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

    $headers .= "X-Sender: <" . $from . ">\r\n";
    $headers .= "X-Priority: " . $priotity . "\r\n";
    $headers .= "X-Mailer: PHP\r\n";
    $headers .= "Return-Path: <[email protected]>\r\n";

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

I called it with :

send_html([email protected], [email protected], utf8_decode("the subject"), "<h1>test</h1>");
like image 906
Erick Avatar asked Apr 24 '09 15:04

Erick


4 Answers

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('[email protected]','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 = "[email protected]"
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.

  • http://us3.php.net/manual/en/configuration.changes.modes.php
  • http://us3.php.net/manual/en/ini.list.php
like image 161
txyoji Avatar answered Nov 20 '22 18:11

txyoji


I had similar problem , mail function always returns false even if the email is received successfully.

I found in php configuration file php.ini, I had set up

; For Win32 only.
;sendmail_from = [email protected]

; For Unix only.
sendmail_path = "/usr/sbin/sendmail -t -i -f "[email protected]"

I have changed it to below

; For Win32 only.
;sendmail_from = [email protected]

; For Unix only.
sendmail_path = /usr/sbin/sendmail -t -i -f [email protected]

As per this sendmail_from is for win32 , so in *unix OS we need to set the value as shown in sendmail_path variable.

Regards Minesh

like image 24
minesh Avatar answered Nov 20 '22 19:11

minesh


If the class is only a wrapper around the function mail, I would try printing to a file the parameters used when calling the mail function

like image 4
Razvan Stefanescu Avatar answered Nov 20 '22 20:11

Razvan Stefanescu


Try setting the

ini_set('sendmail_from', $from);

If you could show us the code it would be easier to see what is going wrong.

like image 4
Alix Axel Avatar answered Nov 20 '22 18:11

Alix Axel