如何向 PHP 指定 mail() 应该使用外部邮件服务器发送?

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

How do I specify to PHP that mail() should be sent using an external mail server?

ubuntusmtpsendmailphp

提问by Matt

I have my email hosted at Rackspace Email and would like to use that as my mail server for the contact form on my website.

我的电子邮件托管在 Rackspace Email 中,并希望将其用作我网站上联系表格的邮件服务器。

Looking at the php.ini file, I'm only able to specify the sendmail_pathon UNIX systems, from which I've read points to the program that actually sends mail on the server.

查看 php.ini 文件,我只能sendmail_path在 UNIX 系统上指定UNIX 系统,从中我读到指向实际在服务器上发送邮件的程序。

I do not want to send mail from my Ubuntu server since I'm not experienced enough to make a secure setup for email... I would like to relay everything to Rackspace's mail.emailsrvr.com.

我不想从我的 Ubuntu 服务器发送邮件,因为我没有足够的经验来为电子邮件进行安全设置......我想将所有内容转发到 Rackspace 的mail.emailsrvr.com.

My question is, how do I specify to the PHP setup on my server that the mail()function should be using an external mail server?

我的问题是,如何在我的服务器上的 PHP 设置中指定该mail()函数应该使用外部邮件服务器?

回答by fyrye

Since I was researching this issue and stumbled across this post and a third-party php library was not an option for me.

由于我正在研究这个问题并偶然发现了这篇文章,因此第三方 php 库对我来说不是一个选择。

As we know, php uses the sendmailcommand of the server by default The sendmail_pathoption in php.inican be changed to override the setting to your own command with it's own arguments, etc. For example: sendmail_path = /usr/bin/unix2dos | /usr/bin/dos2unix | /usr/sbin/sendmail -t -i

我们知道,phpsendmail默认使用服务器的命令可以更改sendmail_path选项中的选项,php.ini以使用它自己的参数等将设置覆盖到您自己的命令中。例如:sendmail_path = /usr/bin/unix2dos | /usr/bin/dos2unix | /usr/sbin/sendmail -t -i

SSMTP will allow you to direct outbound emails to a mailhost from your web/php server. https://wiki.archlinux.org/index.php/SSMTP

SSMTP 将允许您将出站电子邮件从您的 web/php 服务器定向到邮件主机。https://wiki.archlinux.org/index.php/SSMTP

apt-get install ssmtp

Then you can use sendmail_path = /usr/sbin/ssmtp -tto tell php to use ssmtp instead of sendmail. Be sure to restart your web server after you have made changes to php.ini

然后你可以sendmail_path = /usr/sbin/ssmtp -t用来告诉php使用ssmtp而不是sendmail。更改 php.ini 后,请务必重新启动 Web 服务器

Also ensure you have configured ssmtp and validated your SPF, DKIM, DMARC records before you make the changes to sendmail_pathin php.ini

sendmail_path在对 php.ini进行更改之前,还要确保您已配置 ssmtp 并验证了 SPF、DKIM、DMARC 记录

For example gmail Mail server. /etc/ssmtp/ssmtp.conf

例如 gmail 邮件服务器。 /etc/ssmtp/ssmtp.conf

# The user that gets all the mails (UID < 1000, usually the admin)
[email protected]

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also http://mail.google.com/support/bin/answer.py?answer=78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=yourdomain.com

# The full hostname
hostname=FQDN.yourdomain.com

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
[email protected]
AuthPass=postmaster-password

# Email 'From header's can override the default domain?
FromLineOverride=yes

For a stack exchange question to the same see https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server

对于相同的堆栈交换问题,请参阅 https://unix.stackexchange.com/questions/36982/can-i-set-up-system-mail-to-use-an-external-smtp-server

To expand on this.

对此进行扩展。

If using Google, each From:email address must be setup on the sending account as an "Account You Own" setting under accounts. Otherwise google will rewrite the headers with x-google-original-fromand specify the From as the sending account instead.

如果使用 Google,则From:必须在发送帐户上将每个电子邮件地址设置为帐户下的“您拥有的帐户”设置。否则,谷歌将重写标头,x-google-original-from并将发件人指定为发送帐户。

回答by Marc B

mail()is intended to hand off to a local SMTP server, and does a poor job of it. For proper mail support, use Swiftmaileror PHPMailer, both of which fully support external SMTP servers and are far easier to use (plus letting you do things like mixed text/html mails, attachments, etc...)

mail()旨在移交给本地 SMTP 服务器,但它的工作做得很差。要获得适当的邮件支持,请使用SwiftmailerPHPMailer,它们都完全支持外部 SMTP 服务器并且使用起来要容易得多(此外还可以让您执行混合文本/html 邮件、附件等操作...)

回答by Angelo Ativo

For those who don't want to use a PHP library such as Swiftmailer (and ultimately those who don't want to touch their PHP codebase just to switch SMTP servers), you can do either one of the following:

对于那些不想使用诸如 Swiftmailer 之类的 PHP 库的人(以及最终不想仅仅为了切换 SMTP 服务器而接触其 PHP 代码库的人),您可以执行以下任一操作:

1.) Windows Servers: Modify your PHP INI file to use an external SMTP relay host. You'll see it in the mailer section labeled "For Windows servers only" - or something similar.

1.) Windows 服务器:修改您的 PHP INI 文件以使用外部 SMTP 中继主机。您将在标有“仅适用于 Windows 服务器”的邮件部分或类似内容中看到它。

2.) Linux Servers: Install Postfix (e-mail relay service) and configure that to use an external SMTP host. Your PHP installation will attempt to use this to send e-mails by default without any additional configuration.

2.) Linux 服务器:安装 Postfix(电子邮件中继服务)并将其配置为使用外部 SMTP 主机。默认情况下,您的 PHP 安装将尝试使用它来发送电子邮件,而无需任何其他配置。

**This is obviously not intended to give you step by step details on either option above, but rather to point you in the right direction if you're looking for a solution that doesn't require changing instances in your code where PHP's mail() is called.

**这显然不是要为您提供有关上述任一选项的分步详细信息,而是如果您正在寻找不需要更改 PHP 邮件( ) 叫做。

回答by jishi

Not related to the question, but there are mailer daemons that only acts as a sendmail daemon, but relays to an external mail.

与问题无关,但有邮件程序守护程序仅充当 sendmail 守护程序,但中继到外部邮件。

http://freshmeat.net/projects/nullmailer/

http://freshmeat.net/projects/nullmailer/

If you don't even need an exim/sendmail install on your machine, I suggest you try that. Of course, you can still use other third party alternatives, however if you run a daemon locally it will be able to queue the mail as well, which a php lib can not, if the relaying smtp isn't available.

如果您甚至不需要在您的机器上安装 exim/sendmail,我建议您尝试一下。当然,您仍然可以使用其他第三方替代方案,但是如果您在本地运行守护程序,它也可以将邮件排队,如果中继 smtp 不可用,则 php lib 不能。

It is part of the normal repo for Debian so I guess that is true for ubuntu as well, just apt-get install nullmailershould suffice. Then you can configure it with 1 or more smtp relays that it is allowed to use.

它是 Debian 正常 repo 的一部分,所以我想这对 ubuntu 也是如此,apt-get install nullmailer应该足够了。然后,您可以使用允许使用的 1 个或多个 smtp 中继对其进行配置。

See more here: http://packages.ubuntu.com/oneiric/nullmailer

在此处查看更多信息:http: //packages.ubuntu.com/oneiric/nullmailer

As a side note, a linux system without a mailer system becomes crippled in many other ways, so I think it's a good idea either how.

附带说明一下,没有邮件系统的 linux 系统会在许多其他方面变得瘫痪,所以我认为这是一个好主意。

回答by AlexV

Setting up the internal mailfunction to use SMTP is only available on Windows. On other platforms, PHP should use the locally available sendmail or sendmail drop-in just fine.

设置内部邮件功能以使用 SMTP 仅适用于 Windows。在其他平台上,PHP 应该使用本地可用的 sendmail 或 sendmail drop-in 就好了。

If you want to use a SMTP under a non-Windows server you will have to use a third party library such as my favorite Switfmailer.

如果您想在非 Windows 服务器下使用 SMTP,则必须使用第三方库,例如我最喜欢的Switfmailer

With Swiftmailer sending a email looks like this:

使用 Swiftmailer 发送电子邮件如下所示:

require_once 'lib/swift_required.php';

//Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$result = $mailer->send($message);

回答by jjwdesign

The default PHP function 'mail()' will only get the basic functionality to send an email. For Rackspace, you'll probably need to setup an SMTP connection to their mail server. To do this it's best to get a more advanced and developed mailing class. Several code frameworks have them available. If you're looking for a good package, check out PHP Mailer. It's almost a standard these days.

默认的 PHP 函数“mail()”只会获得发送电子邮件的基本功能。对于 Rackspace,您可能需要设置到他们的邮件服务器的 SMTP 连接。要做到这一点,最好获得一个更高级和更发达的邮件类。有几个代码框架可以使用它们。如果您正在寻找一个好的软件包,请查看 PHP Mailer。这几乎是当今的标准。

http://phpmailer.worxware.com/

http://phpmailer.worxware.com/

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "[email protected]";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}