php mail() not working windows 2003, IIS SMTP - php

I'm getting this problem:
PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for chris.mahan#gmail.com in c:\inetpub\wwwroot\mailtest.php on line 12
from this script:
<?php
$to = "chris.mahan#gmail.com";
$subject = "test";
$body = "this is a test";
if (mail($to, $subject, $body)){
echo "mail sent";
}
else {
echo "problem";
}
?>
section from php.ini on the server:
[mail function]
; For Win32 only.
SMTP = server.domain.com; for Win32 only
smtp_port = 25
; For Win32 only.
sendmail_from = support#domain.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
(note that "server" and "domain" refer accurately to the actual server and domain name)
In IIS, SMTP is running. Under "Access" tab, "Relay" button, the Select which computers may relay through this virtual server is set to checkbox "only the list below" and on the list is "127.0.0.1(xxx.xxx.xxx.xxx)" (x's representing actual server IP address).
Server is running Windows Server 2003 Service Pack 2, fully patched as of 5 PM Sept 1st 2008. I assume it is running IIS7 (how to check?).
Any ideas?
In reponse to Espo: This machine is hosted at a datacenter. We do not want to use a gmail account (were doing it, want to move away from that). Windows server 2003 comes with its own SMTP server.
Update: Per Yaakov Ellis' advice, I dropped all relay restrictions and added the server IP to the allowed list (using the reverse DNS button provided) and the thing started working.
Thanks to both Espo and Yaakov for helping me out.

Try removing the IP restrictions for Relaying in the SMTP server, and opening it up to all relays. If it works when this is set, then you know that the problem has to do with the original restrictions. In this case, it may be a DNS issue, or perhaps you had the wrong IP address listed.

You are using the wrong SMTP-server. If you you are only going to send emails to your gmail-account, have a look at my answer here.
If you also need to send email to other accounts, ask you ISP for your SMTP-details.
EDIT: I think it is always better to use the ISP SMTP-server as they (should) have people monitoring the mail-queues, checking for exploits and updating the mail-software. If you business is developing web-applications it is almost always best to stick with what you do, and let other people do their stuff (eg running mailservers).
If you still for some reason want to use you local SMTP server, the first thing would be to rule out the php-part. Try folowing KB153119 and then check you SMTPServer IISlog for errors.
EDIT2:
That KB-article says it is for exchange, but the same commands are used for other SMTP-servers (including IIS) as well, so please try and see if you can send mails using the examples from the article.

#Espo: I'll do that re KB153119. Thanks.
About the mail server: I hear you.
I'll update when I uncover more.

#Espo, the article in question relates to Exchange servers, not IIS7.0 SMTP server.
From the summary: This article describes how to telnet to port 25 on a computer that runs Simple Mail Transfer Protocol (SMTP) services to troubleshoot SMTP communication problems. The information in this article, including error messages, only applies to issues when attempting to resolve SMTP communication issues with Microsoft Exchange-based servers and is not intended for general troubleshooting purposes.

I had the same problem, php 5 on iis6, 2003 server. Php always failed when trying to use mail().
I've managed to get it accepting mail from php by changing the Relay Restrictions from 'Only the list below' (which is empty by default) to 'All except the list below' .
The relay restrictions can be found in the Access tab in the smtp servers properties screens.
Of course if the server is open to the internet then one would have to be more sensible about these relaying restrictions but in my case this is on a virtual server on a dev box.
hope that helps.

Related

SMTP configuration not working in production

I'm trying to send an email when a form is submitted. I'm using PHPMailer to send the mail using the below configuration.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'user#example.in';
$mail->Password = 'password';
$mail->setFrom("user#example.in" , "User");
$mail->addAddress('receiver#example.in', 'Receiver');
$mail->addBCC('anotheruser#somedomain.com', 'Another user');
$mail->AddReplyTo('user#example.in', 'User');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send())
echo "Your request has been received. We will soon contact you.";
else echo "Unable to send your request. Please try again";
This works fine in localhost. But, when I deploy it to my server (example.in) I get the below exception.
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
--EDIT--
I tried connecting to the SMTP server using telnet command, but I'm unable to add the recipient. I get the below error?
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Escape character is '^]'.
220 fbs-ho-mailserver.example.in ESMTP Service (Lotus Domino Release 8.5.3FP3) ready at Fri, 16 Sep 2016 11:36:01 +0530
HELO example.in
250 fbs-ho-mailserver.example.in Hello example.in ([111.91.127.222]), pleased to meet you
MAIL from: marketing#example.in
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons.
-- EDIT --
I was able to setup this account in outlook. I'm really confused what's happening.
Your error says:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
So let's look at https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting :
"SMTP Error: Could not connect to SMTP host."
This may also appear as SMTP connect() failed or Called Mail() without
being connected in debug output. This is often reported as a PHPMailer
problem, but it's almost always down to local DNS failure, firewall
blocking (for example as GoDaddy does) or other issue on your local
network. It means that PHPMailer is unable to contact the SMTP server
you have specified in the Host property, but doesn't say exactly why.
It can also be caused by not having the openssl extension loaded (See
encryption notes below).
Some techniques to diagnose the source of this error are discussed
below.
GoDaddy
Popular US hosting provider GoDaddy imposes very strict (to the point
of becoming almost useless) constraints on sending email. They block
outbound SMTP to ports 25, 465 and 587 to all servers except their
own. This problem is the subject of many frustrating questions on
Stack Overflow. If you find your script works on your local machine,
but not when you upload it to GoDaddy, this will be what's happening
to you. The solution is extremely poorly documented by GoDaddy: you
must send through their servers, and also disable all security
features, username and password (great, huh?!), giving you this config
for PHPMailer:
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
GoDaddy also refuses to send with a From address belonging to any aol,
gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This
is because all those domains deploy SPF and DKIM anti-forgery
measures, and faking your from address is forgery.
You may find it easier to switch to a more enlightened hosting
provider.
The problem - rather the two unrelated problems - that you're experiencing are quite straightforward:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
and you have verified that the server is indeed accepting connections:
I tried connecting to the SMTP server using telnet command
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Your script cannot connect to the SMTP server when run from its production server.
The likely cause is that the production server has a firewall that, to avoid abuse, prevents any connection to the outside. The server can serve Web requests, but no more.
If your test had verified that port 25 was not responding, then (after checking that the host address was correct) you could have tried telnet mail.example.in 587 instead. If that worked, it could have meant that the server is not accepting insecure connections (port 25) but is accepting secure connections. With PHPMailer you could then have tried activating secure connection:
$mail->SMTPSecure = 'tls';
or
$mail->SMTPSecure = 'ssl';
If that does not work, you might still have a firewall issue; or you might need to look at phpinfo() and verify you do have OpenSSL support available in PHP.
What you need to do
ask the IT people that maintain the production server to open the firewall;
more promisingly, ask them how to send emails from that server. Chances are that you need to use the mail() function, or use localhost or 127.0.0.1 as SMTP server. Then the emails will go out through your production server's service network.
They might tell you that port 25 is not allowed, but port (say) 465 or 567 would be allowed. You will have to update your configuration and/or add TLS/SSL accordingly (see above).
or you might be allowed to connect to a third party SMTP server of which you will have to supply the IP address, to allow the IT guys to open a suitable firewall window. Then the emails will go out through the third party server.
The second problem (possibly NOT a problem)
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons
Also to avoid abuse, SMTP Servers will not let everyone connect and send emails, but only their own customers. I see that in the PHPMailer configuration you specified an user and a password. In the telnet session you did not. So it might well be that PHPmailer could send, but not connect, while your telnet can connect, but not send.
Once you solve the connection problem, your authentication problem will either be solved or will have gone away (because you'll be using a different server supplied to you by the IT guys, for example localhost).
The third problem (might never arise)
A third way of abusing services is over-use - sending too many emails to too many people. Verify with the IT guys what the acceptable policies are for sending emails.
Problems, problems
Other things to look into are the credibility of the source (you might want to send emails on behalf of some domain which has not designated your SMTP server of choice as permitted sender), and the confidentiality of the data (even with TLS/SSL connections, if you are given localhost as the SMTP server, your IT guys will have complete, unfettered, undetectable access to any email you send. You might, or might not, be okay with that).
1st quote (from ibm community):
john.hh#gmail.com was a member of a group (Reject) that was listed in
gmail.com's "Deny messages intended for the following internet
addresses" field (in the destination server's Domino Configuration
document's Router/SMTP, Restrictions and Controls, SMTP Inbound
Controls tab's "Inbound Intended Recipient's Controls" section).
Removing Mary's hierarchical name (Mary Jones/ABC) from the members
list in the Reject (group) document allows Mary to receive messages
from the Internet.
2nd quote:
Most mail servers, to prevent them being used as anonymous spam
relays, are configured only to relay mail from certain hosts.
It's a bad idea to use your own SMTP.
Depending of what you have to do with it, you have some great chances to have your emails blocked in some ways or marked as SPAM. And you will have to spend some times to keep your server up to date.
Use online services that are white-listed for every provider and that expose API to send your transactionnal mails :
https://www.mailjet.com/
http://mailchimp.com/
...
They often propose a free account for small volume (under 2000 emails per days).
Using the API is quite trivial and can be put in place in some minutes (ex : https://dev.mailjet.com/)
Ask to your hosting provider if smtp is enabled on not on that server.I had same issue before for smtp and curl both.Contact hosting provider.
What's the phpmailer version?
You might forget to add this line
$mail->IsSMTP(); // use SMTP
after you initiate $mail.
Have a try.
So you have a GoDaddy server and you want to send email through your newly acquired SMTP Relay. Well “you can’t” is probably what you are going to find if you do some poking around Google.
If you poke around a little more you will probably find GoDaddy’s official solution is to use their internal mail relay. This is not ideal, because of arbitrary barriers they have set, such as this one:
Our dedicated servers have an outbound email limit of 1000 per day. If
you need to send more than 1000 emails per day, please call Customer
Support or open a support ticket to request a higher limit.
Quick and Working Solution: Get SMTP Relay that supports sending emails using HTTP API.
My Suggestion: (A simple and effective working solution, I am using personally)
Signup for an account in Sendgrid. Sendgrid provides free 12k emails per month, and I think that should suffice any basic needs.
First Step: Sendgrid will ask you to add few DNS records to authenticate you for sending emails on behalf of that host. So do it.
Second Step Step: Configure White Lables https://sendgrid.com/docs/User_Guide/Settings/Whitelabel/index.html (This is not important, but it will give professional look to the emails you are sending)
Third Step: Generate API keys
You can manage your API Keys from the SendGrid Customer Portal. Additionally, you can manage your API keys via the API itself.
Forth Step: Go ahead and write our codes.
Sending mails using PHP on sendgrid using this library available on github is easy:
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
require 'vendor/autoload.php';
$sendgrid = new SendGrid("SENDGRID_APIKEY");
$email = new SendGrid\Email();
$email->addTo("test#sendgrid.com")
->setFrom("you#youremail.com")
->setSubject("Sending with SendGrid is Fun")
->setHtml("and easy to do anywhere, even with PHP");
$sendgrid->send($email);
How this works
GoDaddy blocks port 25, 2525, 587 and 485 which are ideal ports for SMTP. So we are using the port 80/443 and requesting the sengrid's API to send the email on our behalf behalf using port 80, and yes, we fooled GoDaddy.
I think within a few steps we can get this working properly.
In your Godaddy/Hostgator Cpanel > Select MX records > Select Domain in question > Select "Remote Mail Exchanger"
You can test mail delivery at this time, issue may be resolved.
I am sure it is set up right but trust me when it comes to good ole Cpanel, PhpMailer and Godaddy/Hostgator. It's a place where common sense makes no sense.
replace:
$mail->Host = 'mail.example.in';
with:
$mail->Host = 'localhost';
I believe this should resolve any issues assuming things like login credentials are valid.
I faced similar timeout issue , the reason was my hosting provider (Linode) .
TLDR; SMTP restrictions are in place by default for Linode on accounts created after November 5th 2019. You'll need to configure rDNS for your instance and open a Support ticket confirming CAN-SPAM compliance, and Support should lift the restrictions pretty quickly. I guess many people face this issue because of smtp restrictions
Timeout error is typical of packets dropped, so could be firewall issues between web server and SMTP server.
If you got ssh access to your server, you can try to connect to smtp using:
telnet <smtp server> 25
On timeout error even with telnet, you're quite sure that the problem is on network/firewall side.
You need to change some setting in your php.ini :
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds.
Were your PHP.ini is depends on your enviroment, more information:
reference
OR
Place this at the top of your PHP script and let your script loose
ini_set('max_execution_time', 600); //600 seconds = 10 minutes or set your time
I got your point. Same issue happen with me , I have just add one line and its working for me. please use ,
$mail->SMTPSecure = 'ssl';
and configure your setting according to ssl . Also properly check mail configure at your serve end.

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

PHP Mail Error in Microsoft WebMatrix

I am a newbie in Web Development an I am currently learning PHP and MySQL. I have read HeadFirst PHP and MySQL, and tried the examples. But The PHP Mail() function doesn't work on my Local Machine. I have tried the script on a web server but nothing happens, the mail isn't sent. Please Help me. I have configured the PHP.INI file to send emails but still the problem persists.
<?php
$to = "me#me.com";
$sub = "hello";
$msg = "Hello, how are you?";
//Mail Function
mail($to,$sub,$msg);
?>
I am using WebMatrix with PHP 5.2 installed. Please help me, I am trying out this one since last 2 hours! I am stuck!
mail() uses 'localhost' to send - it generally assumes it's on Linux.
You will need to aquire a basic SMTP server and run it on windows, OR you may be able to use the SMTP server of your're ISP.
Whichever option, you will need to edit your php.ini, you will find:
[mail function]
; For Win32 only.
;SMTP =
you must set SMTP to the ip/port of a mailserver - again wither run one locally or use your ISP.
EDIT
You could try this approach - I have personally never tried to use GMail for sending: http://www.linuxquestions.org/questions/programming-9/php-pear-mail-packege-support-security-through-ssl-586976/
But The PHP Mail() function doesn't work
Yes it does. The problem is either with how you configured PHP or with the MTA you configured it to use. You'd need to provide details of both for us to understand why mail is not getting sent.
The above answer also is applicable to those who use a simulated local IIS e.g. the WebMatrix on IIS Express users who reside within a corporate network with a SMTP machine available.
To be able to send emails out from within it one needs to edit the PHP.INI file (found typically in \Program Files (x86)\IIS Express\PHP\v5.3) and replace 'localhost' with the SMTP server IP or DNS name.

PHP mail() failing to send to internal addresses

I am trying to send out password reset links for when users forget their password to login to a system I am creating. The problem is, the smtp server is supposedly not configured on the server my system is hosted on. So whenever I try to use the php mail() function to send an email to an internal email address, the emails fail to send, but outside email address (tested with a gmail account), the emails go through. I believe this is because my server is sending directly out to the internet instead of passing through an internal smtp server to resolve where our domain emails should be sent. I was wondering if anyone knew how to configure this on an Xserve or if they knew how to specifically tell the php mail() function where to initially send the email. Any help or pointing in the right direction would be extremely helpful.
Thank you!
mail() doesn't send mail, it just hands things over to the local SMTP server. It does NOT reach out to the recipient's server to deliver the mail. In real world terms, mail() walks your letter down the street and drops it into the neighborhood mailbox. After that, it has absolutely nothing more to do with mail delivery.
Check your local SMTP server's logs to see why the local mails aren't being delivered. There should be a line or two saying why it's registered. Perhaps the local MTA (mail-transfer agent, aka the local "mail man") isn't configured properly.
You can control mail() with it's settings.
This might not solve your overall problem, but hopefully it's useful. This related answer has more information.
We just addressed this problem internally here. Hopefully this will help you as well.
Our environment:
Ubuntu 12.04 LTS
PHP 5.3.10
We could telnet into our SMTP server and send mail from our web server, so it wasn't a permissions issue. When attempting to mail externally, all works perfectly. When attempting to mail internally, silent failure.
Our PHP is using sendmail, which by default, attempts to relay mail to 127.0.0.1. Point this at your SMTP server by editing /etc/mail/sendmail.cf. Update the line from:
# "Smart" relay host (may be null)
DS
to
# "Smart" relay host (may be null)
DSyour.smtp.server.com
Restart sendmail and try sending yourself an email via PHP.
This is something that occurs on Parellels’ PLESK server administration software.
Find your ‘main.cf’ configuration file for PostFix, which for CentOS 6, is located at
/etc/postfix/main.cf
If you can’t find it, do a
which postfix
SSH command to at least see where Postfix is on your server.
Then, open the file up through a text editor, or in the Linux shell, and make these lines (should be at the end of the file, around line 677) :
virtual_mailbox_domains = $virtual_mailbox_maps, hash:/var/spool/postfix/plesk/virtual_domains
virtual_alias_maps = $virtual_maps, hash:/var/spool/postfix/plesk/virtual
virtual_mailbox_maps = hash:/var/spool/postfix/plesk/vmailbox
commented out like this :
#virtual_mailbox_domains = $virtual_mailbox_maps, hash:/var/spool/postfix/plesk/virtual_domains
#virtual_alias_maps = $virtual_maps, hash:/var/spool/postfix/plesk/virtual
#virtual_mailbox_maps = hash:/var/spool/postfix/plesk/vmailbox
Then, restart the Postfix service
sudo service postfix restart
Apache while your at it (can’t hurt), and voila! Your email address should be receiving those emails now. This also doesn’t affect any of your regular emails or anything else, either.

Configure WAMP server to send email

Is there a way that I can configure the WAMP server for PHP to enable the mail() function?
Configuring a working email client from localhost is quite a chore, I have spent hours of frustration attempting it. I'm sure someone more experienced may be able to help, or they may perhaps agree with me.
If you just want to test, here is a great tool for testing mail locally, that requires almost no configuration:
http://www.toolheap.com/test-mail-server-tool/
Install Fake Sendmail (download sendmail.zip).
Then configure C:\wamp\sendmail\sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=user#gmail.com
auth_password=your_password
The above will work against a Gmail account.
And then configure php.ini:
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
Now, restart Apache, and that is basically all you need to do.
Using an open source program call Send Mail, you can send via wamp rather easily actually. I'm still setting it up, but here's a great tutorial by jo jordan. Takes less than 2 mins to setup.
Just tried it and it worked like a charm! Once I uncommented the error log and found out that it was stalling on the pop3 authentication, I just removed that and it sent nicely. Best of luck!
You need a SMTP server to send your mail. If you have one available which does not require SMTP authentification (maybe your ISP's?) just edit the 'SMTP' ([mail function]) setting in your php.ini file.
If this is no option because your SMTP server requires authentification you won't be able to use the internal mail() function and have to use some 3rd party class which supports smtp auth. e.g. http://pear.php.net/package/Mail/
I tried Test Mail Server Tool and while it worked great, you still need to open the email on some client.
I found Papercut:
https://github.com/ChangemakerStudios/Papercut-SMTP
(updated URL for 2021)
For configuration it's easy as Test Mail Server Tool (pratically zero-conf), and it also serves as an email client, with views for the Message (great for HTML emails), Headers, Body (to inspect the HTML) and Raw (full unparsed email).
It also has a Sections view, to split up the different media types found in the email.
It has a super clean and friendly UI, a good log viewer and gives you notifications when you receive an email.
I find it perfect, so I just wanted to give my 2c and maybe help someone.
Sendmail wasn't working for me so I used msmtp 1.6.2 w32 and most just followed the instructions at DeveloperSide. Here is a quick rundown of the setup for posterity:
Enabled IMAP access under your Gmail account (the one msmtp is sending emails from)
Enable access for less secure apps. Log into your google account and go here
Edit php.ini, find and change each setting below to reflect the following:
; These are commented out by prefixing a semicolon
;SMTP = localhost
;smtp_port = 25
; Set these paths to where you put your msmtp files.
; I used backslashes in php.ini and it works fine.
; The example in the devside guide uses forwardslashes.
sendmail_path = "C:\wamp64\msmtp\msmtp.exe -d -C C:\wamp64\msmtp\msmtprc.ini -t --read-envelope-from"
mail.log = "C:\wamp64\msmtp\maillog.txt"
Create and edit the file msmtprc.ini in the same directory as your msmtp.exe file as follows, replacing it with your own email and password:
# Default values for all accounts
defaults
tls_certcheck off
# I used forward slashes here and it works.
logfile C:/wamp64/msmtp/msmtplog.txt
account Gmail
host smtp.gmail.com
port 587
auth on
tls on
from ReplaceWithYourEmail#gmail.com
user ReplaceWithYourEmail#gmail.com
password ReplaceWithYourPassword
account default : gmail
I used Mercury/32 and Pegasus Mail to get the mail() functional. It works great too as a mail server if you want an email address ending with your domain name.

Categories