PHP:用于 SMTP 和 SMTP_PORT 的带有运行时 ini_set() 的 mail() 函数在 Linux 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18717119/
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-25 18:05:17  来源:igfitidea点击:

PHP: mail() function with runtime ini_set() for SMTP and SMTP_PORT not working on Linux

sendmailphpini-set

提问by Padyster

I have used a PHP code for Mailing using a SMTP HOST as given below:

我已经使用 PHP 代码使用 SMTP HOST 进行邮件发送,如下所示:

        ini_set('SMTP','myserver');
ini_set('smtp_port',25);
$to = $email;
$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers  .= "From: NO-REPLY<[email protected]>" . "\r\n";
$subject = "Confirmation For Request";
$message = '<html>
                <body>
                    <p>Hi '.$firstname.' '.$lastname.'</p>
                    <p>
                        We recieved below details from you. Please use given Request/Ticket ID for future follow up:
                    </p>
                    <p>
                        Your Request/Ticket ID: <b>'.$ticketID.'</b>
                    </p>
                    <p>
                    Thanks,<br>
                    '.$team.' Team.
                    </p>
                </body>
            </html>';
mail( $to, $subject, $message, $headers ); 

Now When i execute the code in Windows Localhost.. I am succcesfully recieving the mail whereas, If I deply the same code on my Linux setup, I do not recieve anymail, though the mail() function returns true in linux machine as well....

现在,当我在 Windows Localhost 中执行代码时.. 我成功地收到了邮件,而如果我在 Linux 设置上发送相同的代码,我不会收到任何邮件,尽管 mail() 函数在 linux 机器中也返回 true。 ...

On looking into the phpinfo for both windows localhost and Linux server, for mail parameters I found a single difference,

在查看 windows localhost 和 Linux 服务器的 phpinfo 时,对于邮件参数,我发现了一个区别,

In Windows I found sendmail_path == "No Value", whereas on linux server it says, "usr/sbin/sendmail -t -i"

在 Windows 中,我发现 sendmail_path == "No Value",而在 linux 服务器上,它说,"usr/sbin/sendmail -t -i"

Could some one help me to resolve this issue?

有人可以帮我解决这个问题吗?

NOTE: In windows, its a WAMP setup, whereas Linux is a Dedicated server...

注意:在 Windows 中,它是 WAMP 设置,而 Linux 是专用服务器......

回答by René H?hle

If you're looking to your php.inithere is a short description that

如果你正在寻找你的php.ini有一个简短的描述

ini_set('SMTP','myserver');
ini_set('smtp_port',25);

This both values are only for Windows. So if you want to send mails over SMTP on linux you have to install postfix for example and build a relay.

这两个值仅适用于 Windows。因此,如果您想在 linux 上通过 SMTP 发送邮件,您必须例如安装 postfix 并构建一个中继。

https://www.linode.com/docs/email/postfix/postfix-smtp-debian7

https://www.linode.com/docs/email/postfix/postfix-smtp-debian7

Or what is really much easier use a lib that can send SMTP mail over socket or curl like Swiftmailer.

或者使用可以通过套接字或 curl 发送 SMTP 邮件的库(如 Swiftmailer)更容易。

http://swiftmailer.org/docs/sending.html

http://swiftmailer.org/docs/sending.html

Thats much easier and its working.

这要容易得多,而且它的工作。

回答by René H?hle

I have had a look at this before and in PHP.ini there are two keys sendmail_fromhttp://php.net/sendmail-from(For Win32) and sendmail_pathhttp://php.net/sendmail-path(For Unix) on a wamp or similar setup for linux the default of this key is me@localhost which when on an actual mail server it should reject that email address as a user as they don't exist on the server.

我之前看过这个,在 PHP.ini 中有两个键sendmail_fromhttp://php.net/sendmail-from(对于 Win32)和sendmail_pathhttp://php.net/sendmail-path(对于 Unix) wamp 或类似的 linux 设置,这个键的默认值是 me@localhost ,当在实际的邮件服务器上时,它应该拒绝该电子邮件地址作为用户,因为它们不存在于服务器上。

Try adding something like...

尝试添加类似...

ini_set('sendmail_from','[email protected]')