php 邮件返回错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/786221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:50:12  来源:igfitidea点击:

Mail returns false

php

提问by Erick

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.

我目前在我的工作中使用定制的库。直到最近,图书馆才完美运行。从今天开始,它显然返回 false。

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.
  • SMTP 在 PHP.ini 中设置
  • 发件人在标题中设置
  • 发件人的形式为:sender<[email protected]>
  • 一切都正确发送(正文+标题+主题)
  • 假设 mail( ) 在网站上正常工作,但在这个特定页面上却没有。我知道它一定来自我,但有地方开始寻找会很有趣。
  • 哦,是的,图书馆没有记录。

[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>");

采纳答案by Razvan Stefanescu

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

如果该类只是函数邮件的包装器,我会尝试将调用邮件函数时使用的参数打印到文件中

回答by txyoji

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

我只是遇到了同样的问题。php 升级后,mail 函数总是返回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:

解决方案是在我的 php.ini 中为“sendmail_from”和“sendmail_path”设置一个值。在我的情况下,正确的值是:

sendmail_from = "[email protected]"
sendmail_path = "/usr/sbin/sendmail -t -i"

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

(我正在使用带有 Zend Server CE 的 CentOS 5.3。)

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.

您可以使用 ini_set() 来设置 'sendmail_from' 的值,但是 'sendmail_path' var 必须在您的 php.ini 或 http.conf 中设置。

回答by minesh

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

我有类似的问题,即使成功收到电子邮件,邮件功能也总是返回 false。

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

我在php配置文件php.ini中找到了,我已经设置了

; 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.

根据此 sendmail_from 用于 win32 ,因此在 *unix OS 中,我们需要设置如 sendmail_path 变量中所示的值。

Regards Minesh

问候米内什

回答by Alix Axel

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.

如果您可以向我们展示代码,就会更容易看出哪里出了问题。

回答by Matt

It's possible that the address you are trying to send it to, the server that handles it is rejecting the mail.

您尝试将其发送到的地址,处理它的服务器可能拒绝了邮件。

There are too many variables involved to say for sure.

涉及的变量太多,无法确定。

回答by acrosman

Is your assumption that the mail() function works one you have tested? I would check to make sure that the library isn't just passing a failure of the mail() function up.

您是否假设 mail() 函数可以正常工作?我会检查以确保库不只是传递 mail() 函数的失败。

If you have tested the assumption, I would review the return statements from the library and see if I could find what would cause a false value. That's often reasonably possible even in undocumented code.

如果你已经测试了这个假设,我会查看库中的返回语句,看看我是否能找到导致错误值的原因。即使在未记录的代码中,这通常也是合理的。

回答by Marco7757

A good place to start is to check the error log.

一个好的开始是检查错误日志。

For apache and Ubuntu (for example), the error log is located under /var/log/apache2/error.log.

对于 apache 和 Ubuntu(例如),错误日志位于/var/log/apache2/error.log.

This can give you a clue about what exactly is failing.

这可以为您提供有关究竟是什么失败的线索。