Perl

How to send a mail using Perl and sendmail in Linux

Sending
User Rating 5 (1 vote)
sendmail in LinuxThere will be some situations where you will not be allowed:
  • to send mail outside of your company/organization
  • to open any mail client like Gmail, yahoo or Hotmail using browser

But you wish to send mail to your outside friend. Either you will use some proxy settings or will request administrator to allow sending or receiving any mail from outside. What if we can do it using an in-built command or through script?

Yes it is possible and it’s much more easy if you are using Linux rather than Windows (I know even now most of us are using Windows to read this article 😉 ).

This code will work only at Linux variants. If you want to use sendmail in Windows and Linux also with command line option instead of writing a Perl script, you need to download sendEmail readymade script written in Perl by Brandon Zehm  .

  • For Linux users, just untar it and run as you run other Perl script, provided your Perl interpreter is at /usr/bin/perl else change the path at script accordingly
  • For Windows users, extract the code and run it at command line. Make sure, you have installed Perl interpreter.

Let’s see the explanation using codes:

#!/usr/bin/perl
#################################################################################
# Date: July, 2012
# Description: This script will use inbuilt command "sendmail" with -t option mainly.
# You can send an email to anyone from any valid email id, even if it’s not you.
# You can use it to prank your friends too or to do some serious stuffs.
#################################################################################
use strict;
use warnings;

# You can put as many email ids as you wish in to, cc, bcc
# But, be sure to use commas under one string. All mail ids are fake here, used just for demo :P
# except admin@aliencoders.org ;)
my %config = (mail_from =>'ranjan2012@gmail.com',
mail_to => 'jaszi@gmail.com, sanjeejas@gmail.com',
 mail_cc => 'jasse@gmail.com, jassi@hotmale.com',
 mail_bcc => 'nazibal700@gmail.com, jasze@gmall.com',
 mail_subject => "Just testing Sendmail"
 );

#Call method to send preformatted email
mail_target();

sub mail_target{
#Use \n\n to make sure that it will render as HTML format
my $message = "From: $config{mail_from}\nTo: $config{mail_to}\nCc: $config{mail_cc}\nBcc: $config{mail_bcc}\nSubject: $config{mail_subject}\nContent-Type: text/html;\n\n";<br />
$message.= "<strong>Hi Dear,</strong> <br />If someone knows your email id, he/she can use it without your permission if he/she is using sendmail under Linux :D&lt;br&gt; Only issue is it says via which server. Through this you can find out from which server and location someone has used your email id.\n";<br />
&nbsp;
my $MAIL_CMD = "/usr/sbin/sendmail -t -i";
if(!open(SENDMAIL, "| $MAIL_CMD")){
print "Unable to open an input pipe to $MAIL_CMD:";
exit(1);
}
print SENDMAIL $message."\n";
close(SENDMAIL);
}
If it gives error like Can’t exec “/usr/lib/sendmail” then :
  • Verify if you are using right sendmail path by using “which sendmail” command at linux box. You can see the sendmail path.
  • If it says no command found then, type whereis sendmail and you will surely get the lists of sendmail. Use any of these.
You can hide the server and host information also. But you may see “This message may not have been sent by: “ if it’s Gmail or any good email clients 😀Any alternatives?
Yes, try any of these listed modules in Perl:

How we can trace out that this mail is from the original sender or someone used his/her id using such scripts?
If you click at show original option under Gmail, you can see, who sent you the mail and from where you got the mail.
Tips:
  • You can use anyone’s email id at from section but use yours only. I used my friends id to prank them (but it’s not ethical though)
  • It even didn’t check mail existence so you can  write from email address as bill-gates@msft.com
  • In case you forgot to use proper email format like bill-gates instead of bill-gates@msft.com then it would embed it’s server name as domain name like bill-gates@your-server-name.com
  • You can even attach files
  • You can send mails to many under to, cc and bcc. (Usually to should have only one email id)
Note: Use it to send mail from your mail id only. Using other’s identity for wrong doing is an offensive crime. Do those things at your own risk. Our work was to make you aware about working sendmail only. (Never say that I didn’t warn you before doing anything unethical)How we can hide host name so that no one can get the original trace even using header information.
With the help of masquerading, your outgoing email would appear  from user@aliencoders.org instead of realunixuser@server01.aliencoders.org. This will also hide your internal user name or host name from rest of the world!
So this feature rewrites the hostname in the address of the outgoing mail. This can also be used when you have centralized mail server i.e. mail hub.

Masquerading Sendmail configuration (sendmail.mc not sendmail.cf)
Open your sendmail config file which will be located at  /etc/mail/sendmail.mc:

[vim]
# vi /etc/mail/sendmail.mc
[/vim]

Append/add/modify these lines as follows: (usually these lines will be commented, so just uncomment it)
[vim]
MASQUERADE_AS(aliencoders.org)dnl
FEATURE(masquerade_envelope)dnl
FEATURE(masquerade_entire_domain)dnl
MASQUERADE_DOMAIN(aliencoders.org)dnl
[/vim]

Save and close the file. Replace domain name aliencoders.org with your actual domain name. Update and restart sendmail server (use as root):

Before restarting the server, let’s sendmail.inc make sendmail configuration file ready using m4 command. The m4 utility is a macro processor intended as a front end for C, assembler, and other languages. Type man m4 in Linux box for more details. So, don’t edit sendmail.cf manually.

[vim]
# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
# /etc/init.d/sendmail restart
[/vim]

Done! Now you can hide your real user name, server details and can enjoy this beautiful feature of Linux. (Do pranks or unethical things at your own risk 😉 )

Disclaimer: This article is for educational purpose only. We are not responsible if you get sued or fired from your company while using this script or such knowledge. Use it at your own risk!

Share your Thoughts