Sending E-mail from Your MultiValue Programs - Part 3: Sendmail
In the last article I showed you an easy way to send e-mails using Windows, but many developers are running their systems on Linux/Unix. For various reasons, this is probably the article you are most looking forward to, besides the one on how to send attachments.
I had a few people ask about which articles are coming up, so I thought I'd mention them now.
- Sending using Exchange, Gmail, and other e-mail servers.
- Creating attachments.
- Create Html and Text e-mails.
- Retrieving, consuming, and interacting with POP3 e-mail boxes.
Now that I have that out of the way, lets get to the topic at hand — sending e-mails using Sendmail.
Why Sendmail? Why not postfix, qmail, or exim? Well, the simple answer is that most Linux/Unix systems already have it installed. This means there is no extra software to install and manage, outside what is already included.
Sendmail can be a bit complex to setup, and it had some security issues in the past if the setup was not done correctly. While this can be a concern, it is also to be expected from a SMTP server that has been around since the beginning days of e-mail.
If you have looked around for how to get Sendmail up and running, you likely found TONS of articles, tutorials, and tech tips explaining the best way to do it. This can be a bit daunting, but don't be scared off. It is really quite easy to get a simple SMTP server setup with Sendmail.
Getting Sendmail setup and running.
The following setups are the simplest I've found to setup and configure a simple Sendmail SMTP server. I am not including setups to configure any security, filters, or user mailboxes. This is purely for configuring Sendmail for sending e-mail only. So if you plan on using Sendmail as your public Internet SMTP server, or plan on receiving e-mails using Sendmail, you'll need to dig deeper into all those articles on the Internet.
The first setup is to edit the "/etc/mail/Sendmail.mc" file. Find the following configuration options, and change the "mydomain.com" to your e-mail domain name:
MASQUERADE_AS(mydomain.com)dnl MASQUERADE_DOMAIN(mydomain.com)dnl LOCAL_DOMAIN(`localhost.localdomain')dnl FEATURE(masquerade_envelope)dnl FEATURE(masquerade_entire_domain)dnl
If any of these lines have a "dnl" at the beginning of the line, then remove it. Save the file, compile it, and restart the Sendmail service.
# m4 /etc/mail/Sendmail.mc > /etc/Sendmail.cf # service Sendmail restart
Once it is configured, actually sending the e-mail with Sendmail is quite easy. All you need to do is send the completed e-mail into STDIN, and supply the e-mail address:
/usr/sbin/Sendmail email@example.com < mail.txt
Just like in the IIS version, the only trick is writing the e-mail to the OS. You can see full sample code in figure 1. You can also download the source code from the Intl-Spectrum web site. There are few programs that are used within this program to make generic access to OS commands. These can be found on the Intl-Spectrum web site as well.
SUBROUTINE SEND.EMAIL(TO.ADDR,FROM.ADDR,SUBJECT,MSG) EQU AM TO CHAR(254), VM TO CHAR(253), SVM TO CHAR(252) * EMAIL.DATE = OCONV(DATE(),"DWA")[1,3] EMAIL.DATE = EMAIL.DATE :", ": OCONV(DATE(),"DD") EMAIL.DATE = EMAIL.DATE :" ": OCONV(DATE(),"DMA")[1,3] EMAIL.DATE = EMAIL.DATE :" ": OCONV(DATE(),"DY") EMAIL.DATE = EMAIL.DATE :" ": OCONV(TIME(),"MT") EMAIL.DATE = EMAIL.DATE :" MST" * EMAIL.ITEM = "" EMAIL.ITEM<1> = "To: ": TO.ADDR EMAIL.ITEM<2> = "From: ": FROM.ADDR EMAIL.ITEM<3> = "Subject: ": SUBJECT EMAIL.ITEM<4> = "Date: ": EMAIL.DATE EMAIL.ITEM<5> = "MIME-Version: 1.0" EMAIL.ITEM<6> = "Context-Type: text/plain" EMAIL.ITEM<7> = "Context-Transfer-Encoding: 7bit" EMAIL.ITEM<8> = "" EMAIL.ITEM<9> = MSG * *** Write to File System * EMAIL.ID = "email.txt" CALL IS.WRITE.OS("/temp",EMAIL.ITEM,EMAIL.ID) * *** Execute Os Command * OS.CMD = "/usr/sbin/sendmail ": TO.ADDR :" < ": EMAIL.ID CALL IS.EXECUTE.OS(OS.CMD,"") RETURN END
Drawbacks to Sendmail
One of the major drawbacks to Sendmail is how many different ways there are to configure it. While that is one of its advantages, it also makes it easy to misconfigure. Most security holes found in Sendmail are due to misconfiguration or ignorance of how to configure Sendmail correctly.
Since Sendmail has been around a long time, there are answers and solutions to just about everything that can be done with Sendmail. Make sure you plan your settings correctly.
In the next article I will talk about how to send an e-mail using MS Exchange, Gmail, and other e-mail servers.