简体   繁体   English

默认情况下PHP mail()使用什么SMTP服务器,有更好的选择吗?

[英]What SMTP server does PHP mail() use by default, and are there better options?

I host my site with GoDaddy , and I use the PHP mail() function at the moment to send form mails from my site. 我使用GoDaddy托管我的网站,我现在使用PHP mail()函数从我的网站发送表单邮件。 GoDaddy has a 1000 SMTP relay limit per day for form mail stuff, but they swear that with my PHP script that I should not be touching it. GoDaddy每天为表单邮件提供1000个SMTP中继限制,但是他们发誓我的PHP脚本不应该触及它。

  1. Since mail() doesn't take SMTP info, does it just automatically use GoDaddy's (or whatever hosting you may be on)? 由于mail()不接受SMTP信息,它是否只是自动使用GoDaddy(或您可能在的任何主机)?

  2. If my site is going to be expecting more than 1000 emails sent out a day (separate instances, not in a loop), should I be using a different method, or is mail() the right choice? 如果我的网站预计每天发送超过1000封电子邮件(单独的实例,而不是循环),我应该使用不同的方法,还是mail()是正确的选择?

Php uses by default, the local mail-server. Php默认使用本地邮件服务器。 However you can specify this in your php.ini configuration file. 但是,您可以在php.ini配置文件中指定它。

If you expect to send more email than that, you might want to look into finding a different server to mail from, or alternative hosting 如果您希望发送更多电子邮件,您可能需要查找另一个服务器来发送邮件或替代托管

Dont use mail() function of php it will send your mail to junk only. 不要使用php的mail()函数,它会将你的邮件发送到垃圾邮件。 Instead use SMTP php mailer function. 而是使用SMTP php邮件程序功能。

Why we should use SMTP instead PHP mail(): 为什么我们应该使用SMTP代替PHP mail():

SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. SMTP登录到邮件服务器上的实际帐户,并通过SMTP将邮件发送到另一个邮件服务器。 If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam. 如果邮件服务器配置正确,您的邮件将从邮件服务器上的实际帐户发送,并且不会被标记为垃圾邮件。

Mail sent with the mail() function is sent with sendmail in most cases. 与mail()函数发送邮件的sendmail在大多数情况下发送。 There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers. 没有进行身份验证,如果您在额外标头中使用“发件人:”,它几乎总是被标记为垃圾邮件。

This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. 这是因为如果您查看原始电子邮件文件,例如gmail,您将看到发送的标头。 You are actually sending from user@serverhostname.tld and not someone@example.com like you had told the mail function to do. 您实际上是从user@serverhostname.tld发送的,而不是像您告诉邮件功能那样的someone@example.com。 If you use SMTP and view the original the email is actually sent from someone@example.com 如果您使用SMTP并查看原始邮件,则电子邮件实际上是从someone@example.com发送的

You can download SMTP class from: 您可以从以下位置下载SMTP类:

  1. https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170 https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
  2. http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

On a *nix machine, the PHP mail() function does not support SMTP, but instead uses the sendmail() or other configured mail script on the server. 在* nix机器上,PHP mail()函数不支持SMTP,而是使用服务器上的sendmail()或其他配置的邮件脚本。 This script can send through an SMTP, but this isn't the easiest way within PHP (unless you already have the script). 此脚本可以通过SMTP发送,但这不是PHP中最简单的方法(除非您已经拥有该脚本)。 To use SMTP, I would recommend PHPMailer . 要使用SMTP,我建议使用PHPMailer I have been using it for a few years now and have been impressed. 我已经使用它几年了,并留下了深刻的印象。 It supports SMTP along with many other protocols and also has other helpful functionality, such as adding a text only body for an HTML email and creating the proper email headers. 它支持SMTP以及许多其他协议,还具有其他有用的功能,例如为HTML电子邮件添加仅文本正文以及创建正确的电子邮件标头。 You can also extend the class to set defaults, such as the SMTP server and from email/name so you don't have to set these every time you want to send an email. 您还可以扩展类以设置默认值,例如SMTP服务器和电子邮件/名称,这样您就不必在每次发送电子邮件时都设置这些默认值。 It also does very nice error reporting and debugging. 它还可以进行非常好的错误报告和调试。

I would also recommend this class for sending out 1000s of emails. 我还建议这个课程发送1000多封电子邮件。 I recently did >5000 in one day with it and had no problems. 我最近在一天内做了> 5000并且没有问题。

If you need to use an external email server that requires authentication, you will not be able to use the PHP mail() function. 如果您需要使用需要身份验证的外部电子邮件服务器,您将无法使用PHP mail()函数。

I recommend using: http://pear.php.net/package/Mail 我建议使用: http//pear.php.net/package/Mail

Since this is an old post I thought it would be helpful if I updated the answer - 由于这是一篇旧帖,我认为如果我更新答案会有所帮助 -

This is a lot simpler to do now than it used to be :-) In PHP 4 the PEAR Mail package is typically already installed, and this really simple tutorial shows you the few lines of code that you need to add to your php file http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm 这比现在要简单得多:-)在PHP 4中,PEAR Mail包通常已经安装,这个非常简单的教程向您展示了需要添加到php文件中的几行代码http ://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

Most hosting companies list the SMTP settings that you'll need. 大多数托管公司列出了您需要的SMTP设置。 I use JustHost, and they list theirs at https://my.justhost.com/cgi/help/26 (under Outgoing Mail Server) 我使用JustHost,他们在https://my.justhost.com/cgi/help/26 (在发送邮件服务器下)列出他们的

mail() does use the setting as defined in the php.ini. mail()确实使用php.ini中定义的设置。 Windows servers require an actual smtp server while *nix servers will use whatever mta is installed on the server (if any). Windows服务器需要实际的smtp服务器,而* nix服务器将使用服务器上安装的任何mta(如果有)。

As others have mentioned, if you do want to use an alternate smtp server, use an alternative library like SwiftMailer. 正如其他人所提到的,如果你想使用备用的smtp服务器,请使用像SwiftMailer这样的替代库。 Also you'd want to make sure the smtp server is fast. 此外,您还要确保smtp服务器速度很快。 I have seen slowdowns when using an smtp server like gmail. 我在使用像gmail这样的smtp服务器时看到了减速。

GoDaddy uses a Smart SMTP Relay, even on for dedicated servers hosted with GoDaddy. GoDaddy使用智能SMTP中继,甚至是使用GoDaddy托管的专用服务器。 The limit is based on how many emails are going through the Smart relay. 限制基于通过智能中继的电子邮件数量。

If you have a valid reason for needing to send more emails and you can verify that your site isn't spamming and that all of the emails are opt-in, support will increase the limit for you if you give them an estimate of how many emails you need to send out. 如果您有正当理由需要发送更多电子邮件,并且您可以验证您的网站不是垃圾邮件并且所有电子邮件都是选择加入的,那么如果您给他们一个估计的数量,支持会增加您的限制您需要发送的电子邮件。

I've been using the open source project phpmailer for about seven years-- it is terrific! 我一直在使用开源项目phpmailer大约七年 - 这太棒了! You could use it to connect to an offsite SMTP server. 您可以使用它来连接到非现场SMTP服务器。

If you need to use a third party mail service I'd recommend dropping the use of mail() and replace with the SwiftMailer library. 如果您需要使用第三方邮件服务,我建议不要使用mail()并替换SwiftMailer库。 It's a feature rich component (supports authentication, attachments, encryption etc) we've used it in a few places. 它是一个功能丰富的组件(支持身份验证,附件,加密等),我们在一些地方使用过它。 It's also free and open source. 它也是免费和开源的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM