【问题标题】:How do I specify to PHP that mail() should be sent using an external mail server?如何向 PHP 指定应该使用外部邮件服务器发送 mail()?
【发布时间】:2011-09-28 05:55:14
【问题描述】:

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

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

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

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

【问题讨论】:

  • 建议不要尝试直接从您的网络服务器发送邮件。虽然您可以重新配置您的 sendmail.cf 以使用外部邮件服务器,但使用答案中提到的 PHP 包会更容易。我将 Swiftmailer 与第 3 方 SMTP(SendGrid,一种基于云的 Rackspace 服务)一起使用,目前看来效果很好。
  • 使用 Rackspace 电子邮件设置了 Swiftmailer,稍后一旦电子邮件量增加,将通过 Rackspace 切换到 SendGrid。谢谢!

标签: ubuntu smtp sendmail php


【解决方案1】:

mail() 旨在移交给本地 SMTP 服务器,但效果不佳。要获得适当的邮件支持,请使用 SwiftmailerPHPMailer,它们都完全支持外部 SMTP 服务器并且更易于使用(此外,您还可以执行混合文本/html 邮件、附件等操作...)

【讨论】:

  • Swiftmailer 非常容易设置,在正确的 SMTP 设置下立即工作。
【解决方案2】:

设置内部mail 函数以使用 SMTP 仅在 Windows 上可用。在其他平台上,PHP 应该使用本地可用的 sendmail 或 sendmail 插件就好了。

如果您想在非 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('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

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

【讨论】:

    【解决方案3】:

    默认的 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('name@yourdomain.com', 'First Last');
    
    $mail->AddReplyTo("name@yourdomain.com","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 = "whoto@otherdomain.com";
    $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!";
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

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

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

      顺便说一句,没有邮件系统的 linux 系统在许多其他方面都会变得残废,所以我认为无论怎样做都是个好主意。

      【讨论】:

        【解决方案5】:

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

        众所周知,php默认使用服务器的sendmail命令 php.ini 中的 sendmail_path 选项可以更改为使用自己的参数等将设置覆盖为您自己的命令。 例如: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)
        root=postmaster@yourdomain.com
        
        # 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
        AuthUser=postmaster@yourdomain.com
        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: 电子邮件地址必须在发送帐户上设置为帐户下的“您拥有的帐户”设置。否则 google 将用x-google-original-from 重写标头,并将 From 指定为发送帐户。

        【讨论】:

        • 另一种方法是将本地邮件服务器 (MTA) 配置为使用电子邮件托管服务提供商的 SMTP 中继地址。文档GMailO365。和Rackspace
        【解决方案6】:

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

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

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

        **这显然不是为了向您提供上述任一选项的逐步详细信息,而是在您正在寻找不需要更改代码中的实例的解决方案时为您指明正确的方向调用 PHP 的 mail()。

        【讨论】:

          猜你喜欢
          • 2020-05-15
          • 2011-04-19
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 2014-05-28
          • 1970-01-01
          • 2013-04-28
          • 1970-01-01
          相关资源
          最近更新 更多