Quick and Dirty Scripting

A blog that focuses on automating system administration tasks for Linux, Windows, and VMware ESX

Sunday, November 4, 2007

 

Sending email via a Script

One of the most basic tasks that any system administrator needs to accomplish while writing scripts is to send e-mail. Typically this is to send results of script itself or of some event.

The easiest way to send email is if you are using Linux. Almost all Linux servers have the mail command installed by default. This command can be used by a BASH script to send your mail. The problem with the mail program is that it doesn't send attachments. To send attachments, you'll need mutt. Mutt can be installed via apt-get or yum or via tar balls, on all Linux systems. To send an email via Mutt, you simply need to have the following line in your script


mutt -s $subject -c $ccpt_list -a $attachfile $rcpt_list < $email

The variables defined include:
1. A subject ($subject)
2. A receipt list ($rcpt_list). This is defined in BASH as rcpt_list=someone@company.com.
3. The body of the email is stored in the variable/file $email. $email is a file on the server that is piped into the Mutt command. It is defined as email=/tmp/email.tmp echo Hello World >> $email
4. OPTIONAL: An attach file. The variable $attachefile holds the path location of the file to attache
5. OPTIONAL: An CC list of email address.
--

Over in the Microsoft world, there isn't a command line mail program built into any Windows server, but VBScript is. All that you need to send email via VBScript is the following:

Const SMTP_SERVER = "SMTP mail server address"
Const SMTP_PORT = 25

Set objMessage = CreateObject("CDO.Message")
objMessage.Sender = sSender 'Email address of sender
objMessage.To = sRecipients 'Comma separate list of people who will receive the email
objMessage.Subject = sSubject 'The subject of the email
objMessage.Textbody = sBody 'The body of the email.

'This is optional. sAttachment is the fully qualified path to the file to attach
objMessage.AddAttachment sAttachment

objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_SERVER
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTP_PORT
objMessage.Configuration.Fields.Update

objMessage.send
---

With Powershell just being released from Microsoft, you can write a function to send email and include that function in all scripts that need to send email. The function could look something like this:

function send-email($s,$b,$to) {
$from = "send@company.com"; //Who will be sending the email
$domain = "smtp.company.com"; //SMTP server to send the message

$mail = new-object System.Net.Mail.MailMessage;

for($i=0; $i -lt $to.Length; $i++) {
$mail.To.Add($to[$i]);
}
$mail.From = new-object System.Net.Mail.MailAddress($from);
$mail.Subject = $s;
$mail.Body = $b;

$smtp = new-object System.Net.Mail.SmtpClient($domain);
$smtp.Send($mail);
}

To call this function, you just need to do the following:

$SUBJECT="This is the subject";
$EMAILADDR=@("person1@company.com","person2@company.com");
$BODY="Hello World";
send-email -s $SUBJECT -b $BODY -to $EMAILADDR

Sending email is pretty easy and I know that there are a lot of places on the Web that already so you how to send email. Hopefully, though we can build off this to build much more complex scripts. I received an email from VMware concerning their Powershell interface to ESX. I am planning on next week to show how to write some simple scripts to manage an ESX build out with Powershell and we'll be using the send-email functions.

Well this ends my first real blog post. I hope someone out there finds these scripts/functions useful and I will write more next week.

Labels: , , , ,


Archives

October 2007   November 2007   December 2007   January 2008   February 2008   April 2008   October 2009   November 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]