Table of Contents
Sending Email From Service Console on VMware ESX
Summary: Sending email with the MIME lite module on ESX hosts
Date: Around 2008
Refactor: 6 January 2025: Checked links and formatting.
Introduction
Although it's not enabled by default you can send email from the service console from an ESX server. You'll need a perl script and a MIME module, that's all. This is the script, copy it to /usr/local/bin:
#!/usr/bin/perl -w # ====================================================================== # # Perl Source File -- Created with SAPIEN Technologies PrimalScript 3.1 # # NAME: smtp_send.pl # # ORIGINAL AUTHOR: Scott Herold , RapidApp # ORIGINAL DATE : 11/30/2004 # # MODIFY AUTHOR: Jeremy Pries, Xcedex (jpries-at-xcedex-dot-com) # MODIFY DATE : 4/20/2005 # # MODIFY AUTHOR: Duncan Epping, Yellow-Bricks.com # MODIFY DATE : 9/7/2007 # MODIFIED: Line 99 changed TEXT to TEXT/HTML # # PURPOSE: This is a small script that can send emails through an # external smtp gateway. This is useful when sending log files from an # ESX host to an administrator or group on a scheduled basis. # # Handles piped (|) input, command line message content and file attachments # # PREREQUESITES: The MIME-Lite module is required for this script # to function. # # http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm # # This module must be placed in the following directory of your ESX host: # (Note, the directory will need to be created and is case sensative) # # /usr/lib/perl5/5.6.1/MIME # # ====================================================================== use MIME::Lite; use Sys::Hostname; use Getopt::Long; # Variables to configure # # From Address. By default it just uses a hostname lookup. You may change this to something else if desired. my $fromAddress = hostname(); # Set your smarthost here my $smartHost = "smarthost.example.com"; ### Command Line Arguments if (@ARGV < 2) { print "\n"; print "Usage $0: [-t address] [-s subject] [-m Body] [-a path] [-f address] [-r smarthost]\n\n"; print "-Enclose any options that contains spaces in quotes\n"; print "-All options may be shortened to one character. Ex. -t instead of -toAddress\n\n"; print "Options:\n"; print " -toAddress rcpt to address (required)\n"; print " -subject subect (required)\n"; print " -messageBody body (optional)\n"; print " -attach full path to attachment (optional)\n"; print " -fromAddress mail from address (optional)\n"; print " -relay smarthost/relayhost used to deliver mail. (optional)\n\n"; print "-If no fromAddress is specified, the address $fromAddress will be used\n\n"; print "-If no relay is specified, the variable $smartHost in the script is used.\n"; print " You may wish to change the value of this variable to prevent need to specify it on the command line.\n\n"; print "As of version 0.2, script will handle message body input via a pipe. If input is received via a pipe\n"; print " and message body on command line, the command line text will precede the pipe input in the body.\n"; print "\n"; exit(1); } my $toAddress = ''; my $subject = ''; my $messageBody = ''; my $attach = 'none'; GetOptions ('toAddress=s' => \$toAddress,'subject=s' => \$subject, 'messageBody=s' => \$messageBody, 'attach=s' => \$attach, 'fromAddress=s' => \$fromAddress, 'relay=s' => \$smartHost); unless (-t) { $messageBody = $messageBody . "\n"; while ( $line = <STDIN> ) { chomp $line; $messageBody = $messageBody . $line . "\n"; } } print "smartHost: $smartHost\n"; print "to: $toAddress\n"; print "subject: $subject\n"; print "attach: $attach\n"; ### Default Email header stuff $msg = MIME::Lite->new( From =>$fromAddress, To =>$toAddress, Subject =>$subject, Type =>'multipart/mixed' ); ### The text message portion of the email. Remember \n to break lines $msg->attach(Type =>'TEXT/HTML', Data =>$messageBody ); ### Attach a file if necessary if ($attach ne "none") { ### Path is full path to file, Filename is file name as attached (Can be ### different than the source file name) $msg->attach(Type =>'applications/zip', Path =>$attach ); } ### Mail type and SMTP server. Send email based off those settings MIME::Lite->send('smtp', $smartHost); $msg->send;
The required module can be downloaded from the url provided in the script.
Don't forget to make the script an executable after creating it: [root@esx /usr/local/bin]# chmod +x smtp_send.pl
Installing MIME-Lite
Unpack the downloaded package and copy the lib/MIME/Lite.pm to this directory on your ESX host:
/usr/lib/perl5/5.8.8/MIME
Note that the perl version can differ over ESX versions and that the MIME directory has to be created manually and is case sensitive.
Open the firewall
/usr/sbin/esxcfg-firewall -o 25,tcp,out,SMTP
If required, this is how to close the firewall again:
/usr/sbin/esxcfg-firewall -c 25,tcp,out,SMTP
Sending email
Send an email using this command and syntax:
/usr/local/bin/smtp_send.pl -t sjoerd_@_getshifting.com -s "This is the subject" --f `hostname -s`@getshifting.com -m "`cat /var/log/logfile.txt`" -r 10.10.10.10
Options:
- -t = Receiver
- -s = Subject
- -f = Sender
- -m = Message body
- -r = Mail relay host
If you get this error message:
5.1.7 Invalid address at ./smtp_send.pl line 114
Check both to and from address. Note that the sender address should have a valid email address syntax, like name@address.com.