从 SMTP 服务器使用 PHP 发送电子邮件

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

Sending email with PHP from an SMTP server

phpemailsmtp

提问by Borut Flis

$from = "[email protected]";
$headers = "From:" . $from;
echo mail ("[email protected]" ,"testmailfunction" , "Oj",$headers);

I have trouble sending email in PHP. I get an error: SMTP server response: 530 SMTP authentication is required.

我在用 PHP 发送电子邮件时遇到问题。我收到一个错误:SMTP server response: 530 SMTP authentication is required

I was under the impression that you can send email without SMTP to verify. I know that this mail will propably get filtered out, but that doesn't matter right now.

我的印象是您可以在没有 SMTP 验证的情况下发送电子邮件。我知道这封邮件可能会被过滤掉,但现在这无关紧要。

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

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]

This is the setup in the php.inifile. How should I set up SMTP? Are there any SMTP servers that require no verification or must I setup a server myself?

这是php.ini文件中的设置。我应该如何设置 SMTP?是否有任何不需要验证的 SMTP 服务器或我必须自己设置服务器?

回答by Ivo Pereira

When you are sending an e-mail through a server that requires SMTP Auth, you really need to specify it, and set the host, username and password (and maybe the port if it is not the default one - 25).

当您通过需要 SMTP 身份验证的服务器发送电子邮件时,您确实需要指定它,并设置主机、用户名和密码(如果不是默认端口,还可以设置端口 - 25)。

For example, I usually use PHPMailer with similar settings to this ones:

例如,我通常使用与以下设置类似的 PHPMailer:

$mail = new PHPMailer();

// Settings
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "username"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

// Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();

You can find more about PHPMailer here: https://github.com/PHPMailer/PHPMailer

您可以在此处找到有关 PHPMailer 的更多信息:https: //github.com/PHPMailer/PHPMailer

回答by Dipesh Parmar

<?php
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "[email protected]");

$message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = [email protected]";

$headers = "From: [email protected]";

mail("[email protected]", "Testing", $message, $headers);
echo "Check your email now....&lt;BR/>";
?>

or, for more details, read on.

或者,有关更多详细信息,请继续阅读

回答by txyoji

For Unix users, mail() is actually using Sendmailcommand to send email. Instead of modifying the application, you can change the environment. msmtpis an SMTP client with Sendmail compatible CLI syntax which means it can be used in place of Sendmail. It only requires a small change to your php.ini.

对于 Unix 用户,mail() 实际上是使用Sendmail命令发送电子邮件。您可以更改环境而不是修改应用程序。msmtp是具有 Sendmail 兼容 CLI 语法的 SMTP 客户端,这意味着它可以代替 Sendmail 使用。它只需要对您的 php.ini 稍作改动。

sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"

Then even the lowly mail() function can work with SMTP goodness. It is super useful if you're trying to connect an existing application to mail services like sendgrid or mandrill without modifying the application.

那么即使是低级的 mail() 函数也可以与 SMTP 的优点一起工作。如果您尝试将现有应用程序连接到 sendgrid 或 mandrill 等邮件服务而不修改应用程序,这将非常有用。

回答by George Sovetov

The problem is that PHP mail()function has a very limited functionality. There are several ways to send mail from PHP.

问题是 PHPmail()函数的功能非常有限。有几种方法可以从 PHP 发送邮件。

  1. mail()uses SMTP server on your system.There are at least two servers you can use on Windows: hMailServerand xmail. I spent several hours configuring and getting them up. First one is simpler in my opinion. Right now, hMailServer is working on Windows 7 x64.
  2. mail()uses SMTP server on remote or virtual machine with Linux.Of course, real mail service like Gmail doesn't allow direct connection without any credentials or keys. You can set up virtual machine or use one located in your LAN. Most linux distros have mail server out of the box. Configure it and have fun. I use default exim4 on Debian 7 that listens its LAN interface.
  3. Mailing libraries use direct connections.Libs are easier to set up. I used SwiftMailer and it perfectly sends mail from Gmail account. I think that PHPMailer is pretty good too.
  1. mail()在您的系统上使用 SMTP 服务器。您至少可以在 Windows 上使用两个服务器:hMailServerxmail。我花了几个小时来配置和安装它们。在我看来,第一个更简单。现在,hMailServer 正在 Windows 7 x64 上运行。
  2. mail()在带有 Linux 的远程或虚拟机上使用 SMTP 服务器。当然,像 Gmail 这样的真实邮件服务不允许在没有任何凭据或密钥的情况下直接连接。您可以设置虚拟机或使用位于 LAN 中的虚拟机。大多数 linux 发行版都有开箱即用的邮件服务器。配置它并玩得开心。我在 Debian 7 上使用默认的 exim4 来监听它的 LAN 接口。
  3. 邮件库使用直接连接。Libs 更容易设置。我使用了 SwiftMailer,它完美地从 Gmail 帐户发送邮件。我认为 PHPMailer 也很不错。

No matter what choice is your, I recommend you use some abstraction layer. You can use PHP library on your development machine running Windows and simply mail()function on production machine with Linux. Abstraction layer allows you to interchange mail drivers depending on system which your application is running on. Create abstract MyMailerclass or interface with abstract send()method. Inherit two classes MyPhpMailerand MySwiftMailer. Implement send()method in appropriate ways.

无论您的选择是什么,我都建议您使用一些抽象层。您可以在运行 Windows 的开发机器上使用 PHP 库,并mail()在使用 Linux 的生产机器上简单地运行。抽象层允许您根据应用程序运行的系统交换邮件驱动程序。MyMailer使用抽象send()方法创建抽象类或接口。继承两个类MyPhpMailerMySwiftMailer. send()以适当的方式实施方法。

回答by Bolli

Here is a way to do it with PHP PEAR

这是一种使用 PHP PEAR 实现的方法

// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>'; //change this to your email address
$to = '<[email protected]>'; // change to address
$subject = 'Insert subject here'; // subject of mail
$body = "Hello world! this is the content of the email"; //content of mail

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]', //your gmail account
        'password' => 'snip' // your password
    ));

// Send the mail
$mail = $smtp->send($to, $headers, $body);

//check mail sent or not
if (PEAR::isError($mail)) {
    echo '<p>'.$mail->getMessage().'</p>';
} else {
    echo '<p>Message successfully sent!</p>';
}

If you use Gmail SMTP remember to enable SMTP in your Gmail account, under settings

如果您使用 Gmail SMTP 请记住在您的 Gmail 帐户中启用 SMTP,在设置下

EDIT:If you can't find Mail.php on debian/ubuntu you can install php-pear with

编辑:如果你在 debian/ubuntu 上找不到 Mail.php,你可以安装 php-pear

sudo apt install php-pear

Then install the mail extention:

然后安装邮件扩展:

sudo pear install mail
sudo pear install Net_SMTP
sudo pear install Auth_SASL
sudo pear install mail_mime

Then you should be able to load it by simply require_once "Mail.php"else it is located here: /usr/share/php/Mail.php

然后你应该可以简单地加载它,require_once "Mail.php"否则它位于这里:/usr/share/php/Mail.php

回答by Pekka

There are some SMTP servers that work without authentication, but if the server requires authentication, there is no way to circumvent that.

有一些 SMTP 服务器无需身份验证即可工作,但如果服务器需要身份验证,则无法绕过。

PHP's built-in mail functions are very limited - specifying the SMTP server is possible in WIndows only. On *nix, mail()will use the OS's binaries.

PHP 的内置邮件功能非常有限 - 只能在 WINdows 中指定 SMTP 服务器。在 *nix 上,mail()将使用操作系统的二进制文件。

If you want to send E-Mail to an arbitrary SMTP server on the net, consider using a library like SwiftMailer. That will enable you to use, for example, Google Mail's outgoing servers.

如果您想将电子邮件发送到网络上的任意 SMTP 服务器,请考虑使用像SwiftMailer这样的库。例如,这将使您能够使用 Google Mail 的外发服务器。

回答by Peter Drinnan

In cases where you are hosting a Wordpress site on Linux and have server access you can save some headaches by installing msmtp which allows you to send via smtp from the standard php mail() function. msmtp is a simpler alternative to postfix which requires a bit more configuration.

如果您在 Linux 上托管 Wordpress 站点并具有服务器访问权限,您可以通过安装 msmtp 来避免一些麻烦,它允许您从标准的 php mail() 函数通过 smtp 发送。msmtp 是 postfix 的一个更简单的替代方案,它需要更多的配置。

Here are the steps:

以下是步骤:

Install msmtp

安装 msmtp

sudo apt-get install msmtp-mta ca-certificates

Create a new configuration file:

创建一个新的配置文件:

sudo nano /etc/msmtprc

...with the following configuration information:

...具有以下配置信息:

# Set defaults.    
defaults

# Enable or disable TLS/SSL encryption.
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Set up a default account's settings.
account default
host <smtp.example.net>
port 587
auth on
user <[email protected]>
password <password>
from <[email protected]>
syslog LOG_MAIL

You need to replace the configuration data represented by everything within "<" and ">" (inclusive, remove these). For host/username/password, use your normal credentials for sending mail through your mail provider.

您需要替换由“<”和“>”中的所有内容表示的配置数据(包括,删除这些)。对于主机/用户名/密码,使用您的普通凭据通过邮件提供商发送邮件。

Tell PHP to use it

告诉 PHP 使用它

sudo nano /etc/php5/apache2/php.ini

Add this single line:

添加这一行:

sendmail_path = /usr/bin/msmtp -t

Complete documention can be found here:

完整的文档可以在这里找到:

https://marlam.de/msmtp/

https://marlam.de/msmtp/