将SMTP端口从25更改为587?

8

我的ISP已经阻止从PHP发送邮件的25号端口,并允许使用587或465号端口。我该如何强制PHP邮件函数使用587号端口,而不是默认的25号端口?顺便说一下:我正在使用MAMP PRO的OSX 10.6.6。

更新:我尝试更改php.ini中的设置为

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 587

由于我使用的是Mac,我不认为这对我是解决方案,并且在尝试后它并未起作用。它给了我以下错误消息。

May  6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2822]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May  6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2823]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May  6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2827]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May  6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2825]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out
May  6 20:32:25 Ibrahim-Armars-MacBook-Pro postfix/smtp[2828]: connect to alt2.aspmx.l.google.com[74.125.159.27]:25: Operation timed out

你看它仍在尝试通过25号端口连接?我该如何在Mac上更改它?

5个回答

5

更改smtp_port仅会影响mail()与由SMTP设置指定的服务器交互。这不是问题所在。问题在于:

  1. 您正在使用本地计算机作为SMTP服务器 - 并且
  2. 您的ISP阻止您的本地SMTP服务器(postfix)中继邮件到Gmail

首先,阅读此主题。它讨论了完全相同的问题。要点是您需要使用其他邮件服务器,最好是您的ISP邮件服务器。如果您想要使用他们的电子邮件服务发送出站邮件,您的ISP告诉您使用哪个服务器和端口?您应该能够像使用类似Thundebird的电子邮件客户端一样从本地运行的PHP中使用此服务 - 您能够发送到Gmail。


我使用了Gmail的SMTP服务器,并编辑了一些Postfix配置文件,现在它完美地工作了。谢谢你的提示。 - Ibrahim Azhar Armar

3
在你的php.ini中设置smtp_port = 587。详见http://php.net/manual/en/mail.configuration.php 编辑 正如AJ所指出的,如果您使用本地的postfix或sendmail,这并不能解决问题,因为您要指定smtp = localhost。尝试将其改为您的ISP的SMTP服务器地址。
如果他们在允许您发送邮件之前需要身份验证,那么这可能会导致下一个问题,而许多ISP都需要这样做。在这种情况下,您最好使用Pear Mail package。这也将允许您在脚本中指定邮件服务器和端口。从文档中得知:
$params["host"] - The server to connect. Default is localhost.
$params["port"] - The port to connect. Default is 25.
$params["auth"] - Whether or not to use SMTP authentication. Default is FALSE.
$params["username"] - The username to use for SMTP authentication.
$params["password"] - The password to use for SMTP authentication.

它说仅适用于 Windows,但你看我用的是 Mac,我该如何在 Mac 上更改它? - Ibrahim Azhar Armar
请参阅PHP手册中的注释:“尽管此页面所说的“SMTP”和“smtp_port”设置仅在Windows下使用,但所有Drupal站点都使用这些设置发送电子邮件,无论它们运行在哪个操作系统上。” - AJ.

1
你可以编辑你的php.ini文件(如果有权限),并设置smtp_port = 587或在你的代码中,ini_set('smtp_port', 587)

0

对于那些使用MAMP但由于ISP阻止了端口25而无法通过php mail()函数发送邮件的人(在我的情况下),这里有一些信息可以帮助你解决问题。因为OSX使用postfix来发送邮件,如果你计划使用外部smtp服务器,比如我使用的smtp.gmail.com,这是你应该做的。你需要配置Postfix以使用Gmail作为中继主机。

a)打开MAMP,在postfix中将发件人域更改为smtp.gmail.com

b)打开终端并输入sudo vi /etc/postfix/main.cf,这将要求输入管理员密码,输入后它将在vi编辑器中打开main.cf文件

c)按下ctrl+f并到达文件末尾,将光标移动到末尾的下一行并按a,编辑器现在将切换到插入模式以编辑文件。

在main.cf中添加以下设置

relayhost = [smtp.gmail.com]:587

smtp_tls_security_level = verify
#smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_session_cache_database = btree:/var/run/smtp_tls_session_cache

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

按下:wq退出vim。回到shell中,键入sudo vi /etc/postfix/sasl_passwd并输入以下内容(替换为您的gmail地址和gmail密码):

[smtp.gmail.com]:587 user@gmail.com:mypassword

再次按下:wq保存并退出文件,然后运行以下命令

sudo postmap /etc/postfix/sasl_passwd
sudo postfix reload

希望这能帮助到遇到同样问题的人。

0
如果可以的话,尝试使用ini_set()覆盖smtp_port设置。应该像这样:
ini_set('smtp_port', 587);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接