*** This article is from 2008, this does not work for ESXi, look at vCheck from Alan Renouf instead ***
When writing the “Checking the diskspace on your VMFS volumes” blog I assumed that most of you knew how to send an email from the service console. I received a bunch of emails from people who were interested in how to setup the html email functionality. Well, here it is:
A while back VMGuru.com hosted a script which enabled you to send email from the service console. I just browsed their site and it’s nowhere to be found. So I hosted it on my site, mind: I changed the script so that you are able to send HTML emails. This is very useful for the vdfhtml script, that’s the name I gave it… You can download the new smtp_send.pl here. All credits for smtp_send.pl go to Scott Herold and Jeremy Pries. I only changed the following:
$msg->attach(Type =>’TEXT’,
became
$msg->attach(Type =>’TEXT/HTML’,
Upload this script to /usr/local/bin, and upload Lite.pm from the MIME package to /usr/lib/perl5/5.8.0/MIME/, the “5.8.0” directory could be different on your ESX build, it depends on which version of perl is running. Now open up the firewall for smtp out to be able to send email:
esxcfg-firewall -o 25,tcp,out,SMTP
Now you can send html email with the following command, in this example I used the vdf.html ouput file as input for the body of the email.
/usr/local/bin/smtp_send.pl -t [email protected] -s “Diskspace report of the VMFS volumes” -f [email protected] -m “`cat /var/log/vdf.html`” -r 172.10.10.10
-t = Receiver
-s = Subject
-f = Sender
-m = Message body
-r = Mail relay host
You can add this line to the bottom of the vdf-vmfs script and schedule this script via the crontab. Add the following line(I assumed you named the script vdfhtml.sh) to the crontab if you want to receive this email every day at 10:00:
00 10 * * * root /usr/local/bin/vdfhtml.sh
Jaime says
Thanks for posting this. I’m glad to know that I’m not the only person that didn’t know how to do this!
Matt Lydy says
Thanks for posting this step by step guide! I’m so close to getting this running. Everything seems to be working fine but when it sends the email instead of displaying the table it shows ‘cat /var/log/vdf.html’
If I go look at the vdf.html file it is correct but for some reason it isn’t displaying it. Any ideas on what I might have missed?
Matt Lydy says
Woops typo in my code… I was using ‘ instead of `.
Works perfectly now!!! Thank you!
Jaime says
When I try to run the smtp_send.pl script, I get the following error:
Undefined subroutine &Email::Date::Format::email_date called at /usr/lib/perl5/5.8.0/MIME/Lite.pm line 1098. Anyone know how I can correct this? Thanks.
pnikies94 says
Hi,
When i launch the script, i get the following error message :
“can’t find an executable sendmail at /usr/local/bin/smtp_send.pl line 37
Compilation failed in require at /usr/local/bin/smtp_send.pl line 37.
BEGIN failed–compilation aborted at /usr/local/bin/smtp_send.pl line 37.”
do i really need sendmail ?
Thanks
GNi says
If you got error: “can’t find an executable sendmail at /usr/local/bin/smtp_send.pl line 37 etc
Check that you downloaded mime-lite version 3.01 NOT 3.021!
Dinny says
If you don’t want to enable smtp on your esx server permanently – you can turn it on at the start of the script – then turn it off again at the end.
Just add this to the start:
# temporarily enable firewall rule to allow access to smtp
$cmdFirewallOn = “esxcfg-firewall -o 25,tcp,out,SMTP”;
system($cmdFirewallOn);
and this to the end:
# re-apply firewall to block SMTP
$cmdFirewallOn = “esxcfg-firewall -c 25,tcp,out,SMTP”;
system($cmdFirewallOn);
Dinny
Dinny says
Sorry,
If you are running it from the crontab – you will need the full path:
# temporarily enable firewall rule to allow access to smtp
$cmdFirewallOn = “/usr/sbin/esxcfg-firewall -o 25,tcp,out,SMTP”;
system($cmdFirewallOn);
and
# re-apply firewall to block SMTP
$cmdFirewallOn = “/usr/sbin/esxcfg-firewall -c 25,tcp,out,SMTP”;
system($cmdFirewallOn);
Dinny
Duncan Epping says
thanks guys for the additional info!
virtuel says
Thanks a lot for this post. I have been searching for something like this that could integrate directly into Virtual Center and notify groups when a scheduled snapshot is done/completed.
I however need a lot of help with this script. I am very new to the linux world and not sure what needs to be installed where. I have downloaded the necessary files as you have mentioned but how do I install the MIME package on the esx host? Also in an environment with multiple ESX hosts would I have to then install this on all my hosts or is there a way to run this from just one central location and be notified?
Thanks for all your help and sorry if the questions are basic
esxnewbie says
I’ve tried and run this got error below. I’m not sure how to edit the line options below can someone help?
[root@esxtest01 bin]# perl smtp_send.pl
Usage smtp_send.pl: [-t address] [-s subject] [-m Body] [-a path] [-f address] [-r smarthost]
-Enclose any options that contains spaces in quotes
-All options may be shortened to one character. Ex. -t instead of -toAddress
Options:
-toAddress rcpt to address (required)
-subject subect (required)
-messageBody body (optional)
-attach full path to attachment (optional)
-fromAddress mail from address (optional)
-relay smarthost/relayhost used to deliver mail. (optional)
-If no fromAddress is specified, the address dotscocs9vmu1.co.dot.state.fl.us will be used
-If no relay is specified, the variable smarthost.example.com in the script is used.
You may wish to change the value of this variable to prevent need to specify it on the command line.
As of version 0.2, script will handle message body input via a pipe. If input is received via a pipe
and message body on command line, the command line text will precede the pipe input in the body.
Duncan Epping says
this is no error, you need to fill in the options.
Lee says
Is this the correct path for ESX 3.5 for MIME?
/usr/lib/perl5/5.8.0/Encode/MIME
Lee says
Well I created the MIME folder in /usr/lib/perl5/5.8.0/
Now when I try and send an email I get the message of “permission denied”
I am using MIME 3.0.1
/usr/local/bin/smtp_send.pl -t [email protected] -s “This is a test from ESX” -f [email protected] -m “testing 123” -r ExchangeIP
bash: /usr/local/bin/smtp_send.pl: Permission denied
Firewall is open for SMTP. Same issue if I stop firewall service.
Any advice would be appreciated.
Thanks
Lee says
Well I now have email working…. I had to add “perl”…. so the command was………
perl smtp_send.pl -t [email protected] -s “This is a test from ESX” -f [email protected] -m “testing 123″ -r ExchangeIP
I assume this is the standard requirement for ESX 3.5?
Can someone please get back to me?
duncan says
I actually haven’t tested it with 3.5 Lee. Will try it today or tomorrow if I can find the time.
Lee says
Thanks Duncan – appreciate it. In particular if you could advise how to schedule a script that sends an html email report ran from crontab (that references a conf file for email settings etc).
It’s a different story if you run scripts manually as you can specify a configuration file also, however I’m not sure how to do this with crontab.
For example (using your example above)…. what if the below script needed to reference a configuration file for email settings named “email.conf” – how would you do that? Or would that be specified in vdfhtml.sh?
00 10 * * * root /usr/local/bin/vdfhtml.sh
Thanks once again.
Duncan Epping says
I would specify it in vdfhtml.sh. that works like a charm if you specify the full path that is.
jay says
how do i sent mail to multiple recepient…?
i tried puting address seperated with ” ; ” … didnt work 🙁
Duncan Epping says
try a comma “,”.
Remco says
hmmm still wondering why i can’t get the HTML output in the mail: i checked and double checked the command: but i only receive a mail with the line cat /var/log/vdf.html…………
i checked the syntax, ` instead of ‘ but: not working for me. The customer is using ESX 3.5.0 with all the latest patches…..weird!
Remco says
….fixed it ;-)…… the outpul file referenced in the send mail object, had an illegal character…
Amit says
[root@UTIL-CLS02-BL4 bin]# perl /usr/local/bin/smtp_send.pl -t [email protected] -s “Diskspace report of ESX host” -f [email protected] -m “`cat /var/log/vdf.html`” -r 165.113.1.148
cat: /var/log/vdf.html: No such file or directory
I get this error message, email goes through but I see a blank email. the syntax looks correct but there is definitely something wrong, I am running it on ESX 3.5
Any help is appreciated.
Amit says
Thanks for your help Duncan, It worked after running the other script first. Appreciate your help!!
Bobby says
I’m running v 3.0.1 of Lite.pm in the proper location (/usr/lib/perl5/5.8.0) still getting can’t find executable sendmail at line 37… any ideas?
AOPC says
When i run vdfhtml.sh outside crontab it runs fine and sends me the email with the table.
But when i use the same script and try to scheduIe it it runs partially. In the email i see only the header and the footer. the table is missing.
when i view the vdf.html file after it has been run manually it is proper but when it is run via crontab the table is missing . Why isn’t the script running properly when scheduling it ? Please help.
Planet Malaysia says
If you getting error message like “Failed to connect to mail server: Connection refused”, please make sure you run “esxcfg-firewall -o 25,tcp,out,SMTP”
jonatj says
AOPC,
Need to add the full path to vdf in the vdfhtml.sh script. I guess the cron job environment doesn’t have the /usr/sbin in it’s path.
…
echo “” >> “${LOG}”
/usr/sbin/vdf -h -P | grep -E
…
Kcm says
Duncan, do you have an updated version for use with ESX 4? The version of PERL is different on v4 so lite.pm fails.
Joerg says
@Kcm,
just fetch the missing Format.pm from CPAN and the script will works again.
Regards
Joerg
staria says
Hi,
When I launch the command above(/usr/local/bin/smtp_send.pl -t [email protected] ….)I got the following error:
Can’t locate Email/Date/Format.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at /usr/lib/perl5/5.8.0/MIME/Lite.pm line 1092.
But I run:
find / -name Format.pm
/usr/lib/vmware/esx-perl/perl5/site_perl/5.8.0/VMware/Log/Format.pm
cp /usr/lib/vmware/esx-perl/perl5/site_perl/5.8.0/VMware/Log/Format.pm /usr/lib/perl5/5.8.0
And I get the same error.
Can anyone help me?
sanjai says
Staria,
The module Email::Date::Format is being used by the script but its not installed. The remedy is to install the module.
Thomas says
HI
First of all i would like to thank you for posting this. I’m looking for a way to send log per email from the ESX and this seams to be the solution.
I see the last comments are a few month all but with some luck someone will still see my request.
But i have errors when i start the script.
Im using ESXI V4 and perl 5.8.8. I had to create the MIME directory in /usr/lib/perl5/5.8.8/.
I’m using this command :
perl /usr/local/bin/smtp_send.pl -t me@mydomain -s “Test_mail_ESX” -f esx@mydomain -m “test” -r smtp_srv_ip
i get the following errors :
Unquoted string “html” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 2.
Unquoted string “rel” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 4.
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 4, near “”stylesheet” href”
(Missing operator before href?)
Unquoted string “href” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 4.
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 4, near “”http://st.pimg.net/tucs/style.css” type”
(Missing operator before type?)
Unquoted string “type” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 4.
Unquoted string “rel” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6.
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6, near “”alternate” type”
(Missing operator before type?)
Unquoted string “type” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6.
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6, near “”application/rss+xml” title”
(Missing operator before title?)
Unquoted string “title” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6.
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6, near “”RSS 1.0″ href”
(Missing operator before href?)
Unquoted string “href” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 6.
Unquoted string “title” may clash with future reserved word at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8.
Number found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “M”
(Missing operator before 77?)
Number found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “I”
(Missing operator before 73?)
Number found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “M”
(Missing operator before 77?)
Number found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “E”
(Missing operator before 69?)
Bareword found where operator expected at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 10, near “var”
(Missing semicolon on previous line?)
Useless use of push with no values at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 11.
Useless use of push with no values at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 12.
Useless use of push with no values at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 13.
Useless use of push with no values at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 14.
Useless use of push with no values at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 15.
syntax error at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 2, near “html>”
syntax error at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “I”
syntax error at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “M”
syntax error at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 8, near “E”
syntax error at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 10, near “var _gaq ”
Type of arg 1 to push must be array (not anonymous list ([])) at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 11, near “])”
Type of arg 1 to push must be array (not anonymous list ([])) at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 12, near “])”
Type of arg 1 to push must be array (not anonymous list ([])) at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 13, near “])”
Type of arg 1 to push must be array (not anonymous list ([])) at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 14, near “])”
Type of arg 1 to push must be array (not anonymous list ([])) at /usr/lib/perl5/5.8.8/MIME/Lite.pm line 15, near “])”
/usr/lib/perl5/5.8.8/MIME/Lite.pm has too many errors.
Compilation failed in require at /usr/local/bin/smtp_send.pl line 37.
BEGIN failed–compilation aborted at /usr/local/bin/smtp_send.pl line 37.
Thank you if you can help
Fernando Lopes says
Does this solution work on the ESXi 5.1?
Duncan Epping says
No, there is no Service Console in ESXi
Michael says
Hi, I don’t find /usr/bin/perl under esxi 5.5u1 – makes it kind of hard to send emails. How can I install that? Using the image HP is providing for their/my HP ProLiant Microserver Gen8.
Thanks in advance Michael
Duncan Epping says
Note, there is no service console in ESXi. This script won’t work!