简体   繁体   中英

change smtp port from 25 to 587?

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

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

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

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?

Changing smtp_port only affects how mail() interacts with the server specified by SMTP setting. This isn't the issue. The issue is that:

  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

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 will be able to send to Gmail.

Set smtp_port = 587 in your php.ini. See 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.

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:

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

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

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

ini_set('smtp_port', 587);

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

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.cf this 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.

in main.cf append this settings

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 :wq to exit vim. Back in the shell type sudo vi /etc/postfix/sasl_passwd and enter the following (substitute your gmail address and gmail password):

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

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

sudo postmap /etc/postfix/sasl_passwd
sudo postfix reload

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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