Shell Script SendMail

Recently i've been working on a shell script to be able to send automated email messages pulled on information on a query. Everything was all sex-nuts and fun until I tried sending the mail without exposing my user-account in the header of the email. This isn't too much of a big issue except when it's received by a mail server like Hotmail which shows the receipt address in your face to avoid spoofing (which makes sense).

To bypass this issue you'd have to enable a flag on the sendmail or exim command (usually -f) to specify the receipt email address, which however can only be used by "trusted users".

Sadly I am not a trusted user on this tools server, so I had to figure out a new way to be able to do this.

I found out that I could create a Perl script to create an SMTP object for the mail server and send mail directly from here. Here is what I came up with:

#!/usr/bin/perl -w

use Net::SMTP;
use Email::Valid;

use Sys::Hostname;
$host = hostname;

$from = ' This email address is being protected from spambots. You need JavaScript enabled to view it. ';
$replyto = ' This email address is being protected from spambots. You need JavaScript enabled to view it. ';
$subject = "Subject this!";

$mailer = Net::SMTP->new('mail.themailserver.com') ;

$mailer->mail($from);
$mailer->recipient($address);

$mailer->to($address);

$mailer->data();
$mailer->datasend('To:'.$address);
$mailer->datasend("\n");
$mailer->datasend("From: ".$from."\n");
$mailer->datasend("Reply-To: ".$replyto."\n");
$mailer->datasend("Subject:".$subject."\n");
$mailer->datasend("\n");
$mailer->datasend($message."\n");
$mailer->dataend();

$mailer->send();


Et voila! Most of the code is email filler, but you get the idea of what's needed. Only a few lines to create the object with the mail server provided and then to fill the object with the formatted data. Weee!