如何在 Linux 上使用 PHP 从本地主机发送电子邮件

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

How to send email from localhost using PHP on Linux

phplinuxemailsmtplocalhost

提问by Templar

I know that to send e-mail from localhost on Windows, you need to change SMTP server in php.ini however this is valid only on Windows:

我知道要在 Windows 上从本地主机发送电子邮件,您需要在 php.ini 中更改 SMTP 服务器,但这仅在 Windows 上有效:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

So what I should do to be able send e-mails from Linux OS?

那么我应该怎么做才能从 Linux 操作系统发送电子邮件?

回答by Kumar

Setup sendmail or exim. Search in your package manager for either of the option and install them. Senmail is common and setting it up should do the job. The mail command on linux is an MUA or mail user agent, a Mail User Agent (MUA) is an application that is used to send and receive email, more of a client. Whereas mail transfer agent transfers emails from one computer to another using a client–server application architecture.

设置 sendmail 或 exim。在您的包管理器中搜索任一选项并安装它们。Senmail 很常见,设置它应该可以完成这项工作。Linux 上的邮件命令是一个 MUA 或邮件用户代理,邮件用户代理 (MUA) 是一个用于发送和接收电子邮件的应用程序,更像是一个客户端。而邮件传输代理使用客户端-服务器应用程序架构将电子邮件从一台计算机传输到另一台计算机。

回答by symcbean

There should be a stub in your php.ini file already, something like:

您的 php.ini 文件中应该已经有一个存根,例如:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/sbin/sendmail -t -i

Check that the command specified exists on your filesystem and you have (installed and) configured the MTA correctly.

检查指定的命令是否存在于您的文件系统中,并且您已经(安装和)正确配置了 MTA。

If you've not already got an MTA set up, there are lots of MTAsavailable for Linux systems, I'd recommend sendmail (comples/difficult to configure but great performance and amazing flexibility) or postfix (easier to configure, good security out of the box).

如果您还没有设置MTA,Linux 系统有很多 MTA可用,我建议使用 sendmail(复杂/难以配置,但性能和灵活性惊人)或 postfix(更容易配置,安全性好)的盒子)。

Learn how to use the 'mail' cli client or run a different MUA on the server to seperate configuring the MTA from PHP integration.

了解如何使用“邮件”cli 客户端或在服务器上运行不同的 MUA 以将 MTA 配置与 PHP 集成分开。

回答by mzhang

If you're running Debian and variants thereof (*buntu, etc.), you can install a mail server by running sudo tasksel install mail-server, which should set you up with basic email capabilities. You can test this by running in command line echo 'body' | sendmail [email protected], or as others have mentioned, mail($to, $subj, $msg)in PHP.

如果您正在运行 Debian 及其变体(*buntu 等),您可以通过运行 安装邮件服务器sudo tasksel install mail-server,这应该为您设置基本的电子邮件功能。您可以通过在命令行echo 'body' | sendmail [email protected]或其他人提到mail($to, $subj, $msg)的 PHP 中运行来测试这一点。

回答by MarkR

You need to install a MTA such as sendmail (Some distros like Debian seem to prefer exim, or maybe postfix).

你需要安装一个 MTA,比如 sendmail(像 Debian 这样的一些发行版似乎更喜欢 exim,或者可能是 postfix)。

Using PHP's SMTP implementation is a bad idea, because it will not respect the retry requirement of RFC5321 "4.5.4.1. Sending Strategy: mail that cannot be transmitted immediately MUST be queued and periodically retried by the sender."

使用 PHP 的 SMTP 实现是一个坏主意,因为它不会遵守 RFC5321 “4.5.4.1. 发送策略:不能立即传输的邮件必须排队并由发送者定期重试”的重试要求。

This violation of an RFC "MUST" requirement generally reduces robustness and will interfere with correct interoperation in most cases. This is why you need to use a local MTA (under Linux or Windows). Ideally, you need a non-SMTP mechanism for submitting messages to the MTA, which is why PHP will should execute "sendmail" to send mail. It tends to do this by default if a MTA is installed.

这种违反 RFC“必须”要求的行为通常会降低健壮性,并且在大多数情况下会干扰正确的互操作。这就是您需要使用本地 MTA(在 Linux 或 Windows 下)的原因。理想情况下,您需要一个非 SMTP 机制来向 MTA 提交消息,这就是 PHP 应该执行“sendmail”来发送邮件的原因。如果安装了 MTA,它往往会默认执行此操作。

回答by Jacob Dalton

I would suggest installing ssmtprather than installing a full mail server like postfix. If this is just a local test environment, you probably don't need a full MTA. ssmtpis very easy to setup--you just supply your smtp credentials for a remote server. There's a tutorial here.

我建议安装ssmtp而不是安装像 postfix 这样的完整邮件服务器。如果这只是一个本地测试环境,您可能不需要完整的 MTA。ssmtp非常容易设置——您只需为远程服务器提供您的 smtp 凭据。有一个教程在这里

回答by Shashidhara

To send mail from localhost (WAMP, XAMP, or LAMP) you can use PHPMailer package (Download PHPMailer from here).

要从本地主机(WAMP、XAMP 或 LAMP)发送邮件,您可以使用 PHPMailer 包(从此处下载 PHPMailer )。

First you have to edit the "php.ini" To find this file display the phpinfo by using following code from the WAMP server. Create one php file and add this content.

首先,您必须编辑“php.ini” 要找到此文件,请使用 WAMP 服务器中的以下代码显示 phpinfo。创建一个 php 文件并添加此内容。

<?php
     echo phpinfo();
?>

There search for "Loaded Configuration File" That will be the path to your php.ini.

在那里搜索“加载的配置文件”,这将是你的 php.ini 的路径。

In this file remove the ;(semi colon) given to extension=php_openssl.dll.

在这个文件中删除给 extension=php_openssl.dll 的 ;(分号)。

After downloading PHPMailerX.X.X package

下载PHPMailerX.XX包后

Extract->Copy the full folder into your project folder.

提取-> 将完整文件夹复制到您的项目文件夹中。

In test folder there is one file called testemail.php.

在 test 文件夹中有一个名为 testemail.php 的文件。

Change the parameter as your need. (Example given below).

根据需要更改参数。(下面给出的例子)。

Then in the browser type 127.0.0.1/PHPMailer/test/testemail.php.

然后在浏览器中输入 127.0.0.1/PHPMailer/test/testemail.php。

Then it will show successful message if email sent, else it will give error message. Example:

然后,如果发送电子邮件,它将显示成功消息,否则将给出错误消息。例子:

//add these codes if not written

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465; 

//You have to change these parameters to your requirements.
//…….code….

$mail->Username = “[email protected]”; // GMAIL username
$mail->Password = “abcdefgh”; // GMAIL password
//……..code….. There are many other functions to attach file etc.. For that refer doc file.
$mail->AddAddress(“[email protected]”,”Nick name”);
//…….code…..

回答by Fred Christophe

This worked for me on Linux Mint 17 for sending emails from the localhost:

这对我在 Linux Mint 17 上从本地主机发送电子邮件有用:

sudo apt-get install sendmail