php 将 smtp 端口从 25 更改为 587?

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

change smtp port from 25 to 587?

phpemailport

提问by Ibrahim Azhar Armar

My ISP have blocked port 25 for sending mails from PHP, and instead have allowed port 587 or 465 to be used. how do i force php mail function to use port 587 instead of default 25? BTW : i am on OSX 10.6.6 using MAMP PRO

我的 ISP 阻止了端口 25,用于从 PHP 发送邮件,而是允许使用端口 587 或 465。我如何强制 php 邮件功能使用端口 587 而不是默认的 25?顺便说一句:我在 OSX 10.6.6 上使用 MAMP PRO

UPDATE : i tried changing the settings in php.ini to this

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

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

as i am on mac i don't think this can be the solution for me, and it is not working after i tried. it gives me following error message.

因为我在 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

you see it is still trying to connect via port 25? how do i change it in mac?

你看到它仍在尝试通过端口 25 连接吗?我如何在mac中更改它?

采纳答案by AJ.

Changing smtp_portonly affects how mail()interacts with the server specified by SMTPsetting. This isn't the issue. The issue is that:

更改smtp_port仅影响mail()SMTP设置指定的服务器的交互方式。这不是问题。问题是:

  1. You are using your local machine as the SMTP server - AND
  2. Your ISP is blocking your local SMTP server (postfix) from relaying messages out to Gmail
  1. 您正在使用本地机器作为 SMTP 服务器 - 并且
  2. 您的 ISP 阻止您的本地 SMTP 服务器(后缀)将邮件转发到 Gmail

First, read this thread. It discusses the same exact issue. The upshot is that you need to use a different mail server, preferably your ISPs mail server. What server and port does your ISP tell you to use for outbound mail if you want to use their Email services? You should be able to use this from your PHP running locally just like you would an email client like Thundebird - and you willbe able to send to Gmail.

首先,阅读这个线程。它讨论了完全相同的问题。结果是您需要使用不同的邮件服务器,最好是您的 ISP 邮件服务器。如果您想使用他们的电子邮件服务,您的 ISP 会告诉您将什么服务器和端口用于出站邮件?您应该能够从本地运行的 PHP 中使用它,就像使用像 Thundebird 这样的电子邮件客户端一样 - 您能够发送到 Gmail。

回答by Thilo

Set smtp_port = 587in your php.ini. See http://php.net/manual/en/mail.configuration.php

smtp_port = 587在你的 php.ini 中设置。见http://php.net/manual/en/mail.configuration.php

EDIT

编辑

As AJ noted, this won't fix the problem if you're using your local postfix or sendmail, which you do by specifying smtp = localhost. Try setting that to your ISP's SMTP server address instead.

正如 AJ 指出的那样,如果您使用本地 postfix 或 sendmail(通过指定smtp = localhost. 尝试将其设置为您的 ISP 的 SMTP 服务器地址。

That might lead to the next problem if they also require authentication before allowing you to send mail, which many ISPs do. In that case, your best bet would be the Pear Mail package. That will incidentally also allow you to specify the mail server and port in your script. From the documentation:

如果他们在允许您发送邮件之前还需要身份验证,那么这可能会导致下一个问题,许多 ISP 都这样做。在这种情况下,您最好的选择是Pear Mail 包。顺便说一下,这也允许您在脚本中指定邮件服务器和端口。从文档:

$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.

回答by Paul DelRe

You can edit your php.ini file (if you have access) and set smtp_port = 587or in your code, ini_set('smtp_port', 587).

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

回答by Damien

If you can, try to override smtp_port setting with ini_set(). Should be something like this:

如果可以,请尝试使用 ini_set() 覆盖 smtp_port 设置。应该是这样的:

ini_set('smtp_port', 587);

回答by Ibrahim Azhar Armar

For those of you using MAMP and not able to send the mail from php mail() function because of port 25 being blocked by ISP (in my case) here is some information for you to solve it. as OSX uses postfix to send mails and if you plan to use external smtp server like smtp.gmail.com which i used here is what you should be doing. you need to configure Postfix to use Gmail as a relay host

对于那些使用 MAMP 并且由于端口 25 被 ISP 阻止(在我的情况下)而无法从 php mail() 函数发送邮件的人,这里有一些信息供您解决。因为 OSX 使用 postfix 发送邮件,如果您打算使用外部 smtp 服务器,例如我在这里使用的 smtp.gmail.com,您应该这样做。您需要配置 Postfix 以使用 Gmail 作为中继主机

a) Open MAMP and in postfix change the domain of outgoing mail to smtp.gmail.com

b) open terminal and type sudo vi /etc/postfix/main.cfthis will ask for your admin password enter it and it will open main.cf in vi editor

c) press ctrl+f and come to the end of the file and bring the cursor one line down from the end and press a, the editor will now switch to insert mode to edit the file.

a) 打开 MAMP 并在 postfix 中将外发邮件的域更改为 smtp.gmail.com

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

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

in main.cf append this settings

在 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

press :wqto exit vim. Back in the shell type sudo vi /etc/postfix/sasl_passwdand enter the following (substitute your gmail address and gmail password):

:wq退出 vim。返回 shell 类型sudo vi /etc/postfix/sasl_passwd并输入以下内容(替换您的 gmail 地址和 gmail 密码):

[smtp.gmail.com]:587 [email protected]:mypassword

again press :wqto save and quit the file, and run the following command

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

sudo postmap /etc/postfix/sasl_passwd
sudo postfix reload

hope this helps someone with the same problem which i faced.

希望这可以帮助我遇到同样问题的人。