从SMTP服务器发送带有PHP的电子邮件。

时间:2022-04-14 18:16:25
$from = "someonelse@example.com";
$headers = "From:" . $from;
echo mail ("borutflis1@gmail.com" ,"testmailfunction" , "Oj",$headers);

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

我用PHP发邮件有困难。我得到一个错误:需要SMTP服务器响应:530 SMTP认证。

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 = someonelse@example.com

This is the setup in the php.ini file. 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服务器不需要验证,或者我必须自己设置一个服务器?

6 个解决方案

#1


132  

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 Auth的服务器发送电子邮件时,您确实需要指定它,并设置主机、用户名和密码(如果不是默认的端口——25)。

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

例如,我通常使用PHPMailer,设置与此类似:

$mail = new PHPMailer();
$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

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

你可以在这里找到更多关于PHPMailer的信息:https://github.com/PHPMailer/PHPMailer

#2


37  

<?php
    ini_set("SMTP", "aspmx.l.google.com");
    ini_set("sendmail_from", "YOURMAIL@gmail.com");

    $message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail@address.com";

    $headers = "From: YOURMAIL@gmail.com";


    mail("Sending@provider.com", "Testing", $message, $headers);
    echo "Check your email now....<BR/>";
?>

OR

READ ON

继续读下去

#3


35  

For Unix users, mail() is actually using Sendmail command to send email. Instead of modifying the application, you can change the environment. msmtp is 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是一个SMTP客户端,具有与Sendmail兼容的CLI语法,这意味着可以使用它来代替Sendmail。这只需要对你的博士学位做一点小小的改变。

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 good。如果您试图在不修改应用程序的情况下将现有应用程序连接到sendgrid或mandrill这样的邮件服务,那么它就非常有用。

#4


13  

Here is a way to do it with PHP PEAR

这里有一种使用PHP PEAR的方法

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

$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // 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' => 'your@gmail.com', //your gmail account
        'password' => 'snip' // your password
    ));

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

If you use gmail smtp remember to enable SMTP in you gmail account, under settings

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

#5


12  

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

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

  1. mail() uses SMTP server on your system. There are at least two servers you can use on Windows: hMailServer and 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()在系统上使用SMTP服务器。至少有两个服务器可以在Windows上使用:hMailServer和xmail。我花了几个小时来配置和安装它们。在我看来,第一个比较简单。目前,hMailServer正在运行Windows 7 x64。
  3. 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.
  4. mail()在使用Linux的远程或虚拟机上使用SMTP服务器。当然,像Gmail这样的真实邮件服务不允许直接连接,没有任何凭证或密钥。您可以设置虚拟机或使用位于您的LAN中的虚拟机。大多数linux发行版都有现成的邮件服务器。配置它,玩得开心。我在Debian 7上使用默认的exim4来监听它的LAN接口。
  5. 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.
  6. 邮件库使用直接连接。设置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 MyMailer class or interface with abstract send() method. Inherit two classes MyPhpMailer and MySwiftMailer. Implement send() method in appropriate ways.

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

#6


10  

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之类的库。这将使您能够使用谷歌邮件的传出服务器。

#1


132  

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 Auth的服务器发送电子邮件时,您确实需要指定它,并设置主机、用户名和密码(如果不是默认的端口——25)。

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

例如,我通常使用PHPMailer,设置与此类似:

$mail = new PHPMailer();
$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

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

你可以在这里找到更多关于PHPMailer的信息:https://github.com/PHPMailer/PHPMailer

#2


37  

<?php
    ini_set("SMTP", "aspmx.l.google.com");
    ini_set("sendmail_from", "YOURMAIL@gmail.com");

    $message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail@address.com";

    $headers = "From: YOURMAIL@gmail.com";


    mail("Sending@provider.com", "Testing", $message, $headers);
    echo "Check your email now....<BR/>";
?>

OR

READ ON

继续读下去

#3


35  

For Unix users, mail() is actually using Sendmail command to send email. Instead of modifying the application, you can change the environment. msmtp is 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是一个SMTP客户端,具有与Sendmail兼容的CLI语法,这意味着可以使用它来代替Sendmail。这只需要对你的博士学位做一点小小的改变。

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 good。如果您试图在不修改应用程序的情况下将现有应用程序连接到sendgrid或mandrill这样的邮件服务,那么它就非常有用。

#4


13  

Here is a way to do it with PHP PEAR

这里有一种使用PHP PEAR的方法

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

$from = '<your@mail.com>'; //change this to your email address
$to = '<someone@mail.com>'; // 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' => 'your@gmail.com', //your gmail account
        'password' => 'snip' // your password
    ));

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

If you use gmail smtp remember to enable SMTP in you gmail account, under settings

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

#5


12  

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

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

  1. mail() uses SMTP server on your system. There are at least two servers you can use on Windows: hMailServer and 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()在系统上使用SMTP服务器。至少有两个服务器可以在Windows上使用:hMailServer和xmail。我花了几个小时来配置和安装它们。在我看来,第一个比较简单。目前,hMailServer正在运行Windows 7 x64。
  3. 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.
  4. mail()在使用Linux的远程或虚拟机上使用SMTP服务器。当然,像Gmail这样的真实邮件服务不允许直接连接,没有任何凭证或密钥。您可以设置虚拟机或使用位于您的LAN中的虚拟机。大多数linux发行版都有现成的邮件服务器。配置它,玩得开心。我在Debian 7上使用默认的exim4来监听它的LAN接口。
  5. 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.
  6. 邮件库使用直接连接。设置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 MyMailer class or interface with abstract send() method. Inherit two classes MyPhpMailer and MySwiftMailer. Implement send() method in appropriate ways.

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

#6


10  

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之类的库。这将使您能够使用谷歌邮件的传出服务器。