Overview
Sendmail is subject to SMTP quota limits. DreamHost recommends that you configure any scripts relying on Sendmail to use SMTP authentication.
Sendmail is a mail transfer agent (MTA) that is a well known project of the open source, free software, and Unix communities. It is distributed both as free software and proprietary software. It is used to route email over the Internet to a specific recipient, and can be done by running the sendmail command in a Shell environment. It can also be used within a programming script to send email.
This article covers a few simple examples of Sendmail.
In the following examples, you only need to adjust the text in bold.
Using sendmail from the command line
The following are two examples on how to send email from the command line using sendmail.
Simple example
View the SSH article for detailed instructions on how to log in to your web server via SSH. Once logged in, you can run the following command to send email:
[server]$ /usr/sbin/sendmail youremail@example.com Subject: Test Send Mail Hello World control d (this key combination of control key and d will finish the email.)
The above example sends an email as follows:
- The first line indicates the email address sendmail will send the email TO.
- The Subject is 'Test Send Mail'.
- The body of the message says 'Hello World'.
- When the message is received, the FROM email address will appear as your user@server. For example, username@servername.dreamhost.com.
Adding To and From addresses
This example is the same as the one above, but adds a specific To and From address:
[server]$ /usr/sbin/sendmail youremail@example.com To: differentemail@example.com From: anyone@example.com Subject: Test Send Mail Hello World control d (this key combination will finish the email.)
The above example sends an email as follows:
- The first line indicates the email address sendmail will send the email TO.
- The email you enter into the To field does not receive an email. It only shows as the To address when the actual recipient receives the email. The recipient is still the first email you enter on the first line when running the command, and can also be the same if you wish.
- The From address is what you entered: anyone@example.com.
- The Subject is what you entered: Test Send Mail.
- The body of the message is what you entered: Hello World.
Sendmail in a cgi script
You can also use sendmail in a script as well. This can be done in several languages. The following example shows a perl cgi script.
Create a file name perltest.pl with the following code and make sure the permissions on the file are set to 755 or it will not run:
Note how the To, From, and Subject fields do not end unless the line is ended with \n.
#!/usr/bin/perl use strict; my($mailprog) = '/usr/sbin/sendmail'; my($from_address) ='AnyoneYouWant@example.com'; my($to_address) ='email@example.com'; open (MAIL, "|$mailprog -t $to_address") || die "Can't open $mailprog!\n"; print MAIL "To: $to_address\n"; print MAIL "From: $from_address\n"; print MAIL "Subject: Hello, self. Nice to email myself."; print MAIL "This is still subject line.\n"; print MAIL "This is the body of my message.\n"; print MAIL "Pretty boring, yes?\n"; print MAIL "Goodbye.\n"; close (MAIL);
This example sends an email to email@example.com.