<

Trouble Sending Mail from Your Script?

Published on
10,154 Points
3,154 Views
5 Endorsements
Last Modified:
Approved
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!
5
Comment
Author:gr8gonzo
2 Comments
LVL 38

Expert Comment

by:younghv
Nicely written article. Good style and information.

"Yes" vote above.
0
LVL 29

Expert Comment

by:Bernard S.
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"
0

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

Suggested Articles

An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month