How to send email from IIS PHP using gmail SMTP? - php

How can I use IIS6 PHP with gmail smtp. I am running windows 7.
My php.ini file has on the bottom
SMTP = smtp.gmail.com
smtp_port = 25
auth_username = [username]
auth_password = [password]
sendmail_from = [username]#gmail.com
When I try it in php, I get the error:
Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Users\Me\Desktop\App\App\script\config\common.php on line 60
Right now, the server is on my home wifi network (router, modem).
However if I connect my server to my LTE phone data plan, then it works fine.
Does anyone know what the problem is? Maybe a firewall thing?
Thanks

In this particular circumstance, I suspect your LTE provider is firewalling port 25.
Gmail does have some alternative ways to send mail that might work. Take a look here and see if any of these scenarios will meet your needs.
https://support.google.com/a/answer/176600?hl=en

Related

What will be the settings in php.ini for sending an email?

It causes the following error related to a mail server or php.ini file.
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in E:\wamp\www\pr\mail.php on line 14
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in E:\wamp\www\pr\mail.php on line 15
First, you need to have a mail server setup somewhere. For that, you need the Hostname, port (usually 25), and possibly username and password. Google for example offers SMTP services (Send email using the GMail SMTP server from a PHP page), but you can use other servers if you have.
The error tells you that you don't have a mailserver setup on your machine, or it does not listen to the 25 port. If it's a shared hosting, you need to call the hosting company to assist you with this. If it's a dedicated server, then you may want to install a mailserver on that machine. If you don't want to install a mailserver, you can use Google or other provider, and you'll have to follow the steps provided here: smtp configuration for php mail or in the other link above

Failed to connect to mailserver

I have an error while sending e-mail through a php script. Although I changed all the required settings in php.ini like
SMTP "localhost" PHP_INI_ALL
smtp_port "25" PHP_INI_ALL
sendmail_from NULL PHP_INI_ALL
sendmail_path NULL PHP_INI_SYSTEM
I am using wamp. The received error is this:
[function.mail]: Failed to connect to mailserver at "PHP_INI_ALL" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\mail.php on line 10
Why does this error occur ?
WAMP runs on Windows, and windows does not have a Mail Server installed by default.
I would suggest hMailer as I use it myself, but it is not exactly childs play to setup.
Alternatively you could use phpMailer which is a php based utility that will help you send mail using for example a yahoo/gmail etc account.
Wampp doesn't come with a default mail server. You have to install one yourself.
If you only need a mailserver for development purposes you can download the 'smtp4dev' from here: http://smtp4dev.codeplex.com/
From an answer on SO (https://stackoverflow.com/a/7011373/1379394)
t's a beautiful little piece of software that sits in your task bar
and lets you see all email that is being sent to it. It doesn't
actually send email anywhere, so you can work with live data without
fear of spamming someone.
Then, when you want your application to actually send email (when you
run it on the server), you will need an SMTP server address instead of
"localhost". If you don't know which SMTP to use, speak to your
network administrator or ISP.
Hope that will help you.

Warning: mail() [function.mail]: Failed to connect to mailserver

Getting error message
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in
Ive opened up php.ini and i believe i need to add
SMTP "localhost" PHP_INI_ALL
smtp_port "25" PHP_INI_ALL
sendmail_from NULL PHP_INI_ALL
sendmail_path NULL PHP_INI_SYSTEM
Im not sure where to add it though? Also correct me if anything i believe i need to do is incorrect. Im currently developing website on localhost so if anything i need to please tell me.
EDIT**
My local host is XAMPP amd i believe this has a mail server
You're probably not running an SMTP server on your localhost.
If you only need an SMTP server for development purposes, and you're running Windows, install smtp4dev, which can be found here: http://smtp4dev.codeplex.com/
It's a beautiful little piece of software that sits in your task bar and lets you see all email that is being sent to it. It doesn't actually send email anywhere, so you can work with live data without fear of spamming someone.
Then, when you want your application to actually send email (when you run it on the server), you will need an SMTP server address instead of "localhost". If you don't know which SMTP to use, speak to your network administrator or ISP.
Last but not least, if you access your email through an email program, you can probably find the "Outgoing server" in your settings somewhere, although that depends on which mail program you use. An "SMTP server" and an "Outgoing server" are exactly the same thing.
You can't send mail from your local computer if you don't have a mail server running on it. Just like everyone else has said: Make sure you have a mail server that is properly configured and up and running on your local computer, or install it on a remote computer and change the IP address/port to point to the remote computer.
You can add smtp configuration at any place, but it is a good idea to search for [mail function] section in that file and add that below. Also,
SMTP = *smtp server addr here*
smtp_port = 25
If you are not using XAMPP or some other packet of that kind, it's unlikely that you have smtp server up and running on localhost.
you have to install a Mail Server Program to your comp.. Localhost does not support send mail in default way

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:
You are using your local machine as the SMTP server - AND
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.
You can edit your php.ini file (if you have access) and set smtp_port = 587 or in your code, 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.

Why mail() PHP function does not work with WAMP default installation?

I have a default installation of WAMP Server 2.0.
I'm trying to send email using this simple script:
<?php
if (mail('my_email#gmail.com', 'My Title', 'Some Text')) {
echo "OK";
} else {
echo "Why ??";
}
?>
Unfortunately, I get the following warning:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\My_Path\send_email.php on line 3
Why ??
What could be the reason for that ?
I expected sending email to be a very simple task ... :(
To be able to send email you need an outgoing email server (MTA). In most Linux systems there exists one by default, and PHP will use it by submitting mail to sendmail, a Linux app/alias for submitting mail to whichever MTA you have installed.
Windows doesn't include an MTA by default. In Windows, to be able to send mail from PHP you need to have access to some outgoing email server and tell PHP the address and port of it. This is done in php.ini using the SMTP and smtp_port settings. It will default to localhost on port 25. Unless you have set up a mail server on that machine yourself, this will fail.
If your ISP gives you an outgoing mail server, for example, you could use its address and port number. Or, if you're serious about sending mail, you'd set up your own mail server on the local machine or somewhere in your local network.
Short answer: no SMTP server is configured for the local computer (localhost). Windows does not ship with a built-in SMTP server ready to go out of the box. You can relay mail through a different host (using the SMTP php.ini directive) - but it's rare where you'll find an open relay for you test environment mail messages.
Instead of using mail(), you can use a script like PHPMailer which can connect directly to your outgoing email server with proper authentication. Here's a quick snippet for Gmail (though it's not complete) and a full example.
You can use "Fake Sendmail": http://glob.com.au/sendmail/
So you don't need a smtp server on your test machine, you only have to set the path to the program in your php.ini
Ciao!
Stefan

Categories