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

问题描述 投票:9回答:6

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

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

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

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

ubuntu smtp sendmail php
6个回答
11
投票

mail()旨在传递给本地SMTP服务器,并且做得很差。要获得正确的邮件支持,请使用SwiftmailerPHPMailer,它们都完全支持外部SMTP服务器并且更容易使用(还允许您执行混合文本/ html邮件,附件等操作...)


11
投票

由于我正在研究这个问题,偶然发现了这个帖子,第三方php库对我来说不是一个选择。

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

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

apt-get install ssmtp

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

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

例如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

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

为了扩展这一点。

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


3
投票

对于那些不想使用Swiftmailer等PHP库的人(以及那些不想只是为了切换SMTP服务器而不想触及他们的PHP代码库的人),您可以执行以下任一操作:

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

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

**这显然无意为您提供上述任一选项的分步详细信息,而是指向您正确的方向,如果您正在寻找一个不需要在代码中更改PHP邮件的实例的解决方案( ) 叫做。


2
投票

与问题无关,但有一些邮件程序守护程序仅作为sendmail守护程序,但转发到外部邮件。

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

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

它是Debian正常回购的一部分,所以我想对于ubuntu也是如此,只需要apt-get install nullmailer即可。然后,您可以使用一个或多个允许使用的smtp继电器进行配置。

在这里查看更多:http://packages.ubuntu.com/oneiric/nullmailer

作为旁注,没有邮件系统的Linux系统在许多其他方面变得瘫痪,所以我认为这是一个好主意。


1
投票

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

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

随着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);

1
投票

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

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!";
}
© www.soinside.com 2019 - 2024. All rights reserved.