Trouble Sending Mail from Your Script?

gr8gonzoConsultant
CERTIFIED EXPERT
Published:
Updated:
One of the most common programming questions that I hear comes from developers who are creating scripts that SHOULD send out an email, but it doesn't seem to work. The email never goes out, or the script times out, or throws mail errors. The most common cause of this has nothing to do with your code, but rather due to your internet provider.

In order to send out a standard email, a program has to first connect to a mail server on port 25. Once connected, the program sends several commands that ultimately send out the email and close the connection. However, many internet providers will prevent YOU from connecting to ANY mail server on port 25 except for their own. So if you are on the East Coast and use Cox Communications, for example, and you try to connect to mail.myserver.com on port 25 (let's assume mail.myserver.com is some mail server that you own and run), Cox will block the attempt. But if you try to connect to smtp.east.cox.net on port 25, it will work just fine (and they will let you send mail using any "From" address you want, although you have to keep Sender Policy Framework in mind).

The reason for this is not because your internet provider is trying to be difficult. There's actually a very good reason for it - to help fight viruses and internet worms.

Many of the most well-known internet worms and viruses spread by emailing themselves everywhere. If your computer got infected, the virus would look at your address book and then try to send itself directly to the recipients' mail servers. So if you knew joe@yahoo.com, then the virus would try to connect to mail.yahoo.com on port 25 and send itself to Joe, pretending to be an email from you. Not only was the spreading of viruses a bad thing itself, but it also ate up a LOT of internet provider bandwidth because the provider would have thousands of infected customers unknowingly sending out millions of virus-laden emails.  So the internet providers decided that they would block port 25 to all other mail servers, rendering these worms and viruses virtually useless. If someone got infected, then the worm/virus couldn't connect to other mail servers to spread.

So what can you do about it?

#1. Have your program send email out through the internet provider's own SMTP mail server. As mentioned before, if you make use of Sender Policy Framework (SPF), you may need to update your SPF policy to allow your internet provider's servers to send out email on behalf of your domain.

#2. If you run your own mail server or have access to system administration resources, you can often set up an alternate, non-standard port (e.g. 2500) and have its traffic route back to the original mail service (sometimes the mail server will have an option to listen on multiple ports). This allows your scripts to connect to the mail server on the non-standard port, which bypasses the block from the internet provider.

#3. Some internet providers will offer "business-class" plans or upgrades that will allow you to connect to anything on port 25. The reason behind this is that business connections cost more and have valid uses for this port and the blocking technique is targeted at home users. So you can call your internet provider to ask whether or not you can upgrade to this service.

Hopefully this clears up confusion about your mail problems - good luck!

Copyright © 2012 - Jonathan Hilgeman. All Rights Reserved. 
5
3,938 Views
gr8gonzoConsultant
CERTIFIED EXPERT

Comments (2)

CERTIFIED EXPERT
Author of the Year 2011
Top Expert 2006

Commented:
Nicely written article. Good style and information.

"Yes" vote above.
CERTIFIED EXPERT

Commented:
Excellent.
Just an awkward problem (and solution) I stumbled against today
- On one Linux box of mine, I use Mutt to send mails and attachments by scripts
- With my configuration, all sent mail is kept in a file called "sent" (yes)
- Since this file grew too much, I simply emptied it
- but then Mutt refused to send any mail, arguing that "sent is not a valid mailbox file" (which obviously was true!)
- SOLUTION: just copy an existing mailbox file (eg, "mbox") over "sent"

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.