Send email from gmail account via command line


/ Published in: Perl
Save to your folder(s)

This scripts can be used to send email from your gmail account by authenticating. It can be modified to login to any SMTP server.


Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Mail::POP3Client;
  5. use IO::Socket::SSL;
  6. use CGI qw(:standard);
  7. use Net::SMTP::TLS;
  8. # There will be a bug using the Net::SMTP::TLS module
  9. # So the fix i found here:
  10. # http://crunchbang.org/forums/viewtopic.php?pid=361299
  11. # invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line
  12. # replace this: m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))$}i
  13. # with this: m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))}i
  14. use Term::ReadKey;
  15. use Switch;
  16. use Mail::IMAPClient;
  17. use Term::ANSIColor qw(:constants);
  18. use Crypt::DES;
  19. use Crypt::Lite;
  20.  
  21. # Declaring all variables
  22. my $cgi;
  23. my $username;
  24. my $password;
  25. my $mailhost;
  26. my $port;
  27. my $pop;
  28. my $count;
  29. my $i;
  30. my $uname;
  31. my $pass;
  32. my $emailto;
  33. my $emailbody;
  34. my $mailer;
  35. my $emaillogin;
  36. my $uname1;
  37. my $pass1;
  38. my $choice = '';
  39. my $emailbodyfin;
  40. my $emailbodyplain;
  41. my $enckey1;
  42. my $cipher;
  43. my $ciphertext1;
  44. my $encrypted;
  45. my $crypt;
  46. my $emailbodyplain1;
  47. my $enckey2;
  48. my $decryptedmsg = '';
  49. my $dcrypt;
  50. my $decrypted;
  51. my $encryptedmsg = '';
  52. my $enckey3;
  53. my $emailbodyplain2;
  54.  
  55. # This is the main control section where
  56. # the respective modules are called from.
  57. # Ideally its good not to declare any
  58. # module in this.
  59. while ($choice ne '5') {
  60. START:
  61. system("clear");
  62.  
  63. print BOLD, BLUE, "\n\n\n\n\tJSINIX Gmailer\n\n", RESET;
  64. print "\n\t1. Send email".
  65. "\n\t2. Check email".
  66. "\n\t3. Encrypt email".
  67. "\n\t4. Decrypt email".
  68. "\n\t5. Quit(q)";
  69.  
  70. print "\n\n\tChoice: ";
  71. $choice = <STDIN>;
  72. chomp($choice);
  73.  
  74. switch ($choice) {
  75.  
  76. case '1'
  77. {
  78. system("clear");
  79. mail_send();
  80. print "\n\tPress enter to continue"; <STDIN>;
  81. goto START;
  82. }
  83.  
  84. case '2'
  85. {
  86. system("clear");
  87. mail_check_pop();
  88. print "\n\tPress enter to continue"; <STDIN>;
  89. goto START;
  90. }
  91.  
  92. case '3'
  93. {
  94. system("clear");
  95. mail_encrypt();
  96. print "\n\tPress enter to continue"; <STDIN>;
  97. goto START;
  98.  
  99. }
  100.  
  101. case '4'
  102. {
  103. system("clear");
  104. mail_decrypt();
  105. #encdec('test','test');
  106. print "\n\tPress enter to continue"; <STDIN>;
  107. goto START;
  108.  
  109. }
  110.  
  111. case '5'
  112. {
  113. system("clear");
  114. exit();
  115. }
  116.  
  117. case 'q'
  118. {
  119. system("clear");
  120. exit();
  121. }
  122.  
  123. }
  124. # The while loop ends here.
  125. }
  126.  
  127. # This module can connect to the
  128. # servers of google/gmail using POP3
  129. # There is one more module named
  130. # mail_check_imap that you can leverage
  131. # IMAP to check the emails.
  132. sub mail_check_pop {
  133. $cgi = new CGI;
  134.  
  135. ($uname1, $pass1) = get_credentials();
  136.  
  137. $mailhost = 'pop.gmail.com';
  138. $port = '995';
  139.  
  140. $pop = Mail::POP3Client->new(USER=> $uname1,
  141. PASSWORD => $pass1,
  142. HOST => $mailhost,
  143. PORT => $port,
  144. USESSL => 'true',
  145. DEBUG => 0,) or die("\n\tERROR: Unable to connect to mail server.\n");
  146.  
  147. $count = $pop->Count();
  148.  
  149. if (($pop->Count()) < 1)
  150. {
  151. print "\n\tNo messages...\n";
  152. print "\n\tPress enter to continue"; <STDIN>;
  153. goto START;
  154. }
  155.  
  156. print "\n\tThere are $count Messages in your inbox\n";
  157.  
  158. print "How many messages you want to print: ";
  159. my $eno = <STDIN>;
  160. chomp($eno);
  161.  
  162. for(my $i = 1; $i <= $eno; $i++)
  163. {
  164. print "\n\n\nMail number $1\n";
  165. foreach($pop->Head($i))
  166. {
  167. /^(From|Subject|Email):\s+/i && print $_, "\n";
  168. }
  169. print 'Message: '.$pop->Body($i);
  170. }
  171.  
  172. $pop->Close();
  173.  
  174. }
  175.  
  176. # This module is used to send emails
  177. # from ur gmail account. We connect to
  178. # the SMTP server of gmail using the credintials
  179. # and send email to anyone.
  180. sub mail_send {
  181.  
  182. print "\n\tLogin ID: ";
  183. my $uname2 = get_user();
  184. print "\n\tPassword: ";
  185. my $pass2 = get_pass();
  186.  
  187. print "\n\tMail To: ";
  188. my $emailto = <STDIN>;
  189. chomp($emailto);
  190.  
  191. print "\n\tBody: ";
  192. $emailbodyplain = get_body();
  193.  
  194. print "\n\tWant to encrypt body ?(y/n)";
  195. my $echoice = <STDIN>;
  196. chomp($echoice);
  197.  
  198. if ($echoice eq 'y') {
  199.  
  200. print "\n\tEncryption key: ";
  201. $enckey1 = get_pass();
  202. $emailbodyfin = encrypt_body($emailbodyplain, $enckey1);
  203.  
  204. }
  205. elsif ($echoice eq 'n') {
  206. $emailbodyfin = $emailbodyplain;
  207. }
  208. else {
  209. print "\n\tWrong selection my friend";
  210. }
  211.  
  212. my $mailer = new Net::SMTP::TLS(
  213. 'smtp.gmail.com',
  214. Hello => 'smtp.gmail.com',
  215. Port => 587,
  216. User => $uname2,
  217. Password=> $pass2);
  218.  
  219. $mailer->mail($emailto);
  220. $mailer->to($emailto);
  221. $mailer->data;
  222. $mailer->datasend($emailbodyfin);
  223. $mailer->dataend;
  224. $mailer->quit;
  225. print "\n\tEmail sent.";
  226. }
  227.  
  228. # This module does the function of encryption.
  229. # Uses Symmetric encryption.
  230. sub mail_encrypt {
  231.  
  232. print "\n\n\tEncrypt message\n\n";
  233.  
  234. print "\n\tPlain text/body: ";
  235. $emailbodyplain2 = get_body();
  236.  
  237. print "\n\tEncryption key: ";
  238. $enckey3 = get_pass();
  239.  
  240. my $encryptedmsg1 = encrypt_body($emailbodyplain2, $enckey3);
  241.  
  242. print "\n\tEncrypted message: "; print $encryptedmsg1;
  243.  
  244. }
  245.  
  246. # This sections is used to decrypt the
  247. # encrypted message using the same symmetric
  248. # key that was used to encrypt.
  249. sub mail_decrypt {
  250.  
  251. print "\n\n\tDecrypt message\n\n";
  252.  
  253. print "\n\tEncrypted text/body: ";
  254. $emailbodyplain1 = get_body();
  255.  
  256. print "\n\tDecryption key: ";
  257. $enckey2 = get_pass();
  258.  
  259. my $decryptedmsg1 = decrypt_body($emailbodyplain1, $enckey2);
  260.  
  261. print "\n\tDecrypted message: "; print $decryptedmsg1;
  262. }
  263.  
  264. # Gets the username and returns it.
  265. sub get_user {
  266.  
  267. my $emaillogin = <STDIN>;
  268. chomp($emaillogin);
  269.  
  270. return $emaillogin;
  271. }
  272.  
  273. # Gets the password without echoing it on the STDOUT.
  274. sub get_pass {
  275.  
  276. ReadMode('noecho');
  277. chomp(my $password = <STDIN>);
  278. ReadMode(0);
  279.  
  280. return $password;
  281. }
  282.  
  283. # This is the module to check email using
  284. # IMAP.
  285. sub mail_check_imap {
  286.  
  287. my ($uname3, $pass3) = get_credentials();
  288.  
  289. my $imap = Mail::IMAPClient->new ( Server => 'imap.gmail.com',
  290. User => $uname3,
  291. Password => $pass3,
  292. Port => 993,
  293. Ssl => 1,
  294. Uid => 1 )
  295. or die "\n\tCould not connect to server, terminating...\n";
  296. }
  297.  
  298. # Gets the body for the email. Can be used to
  299. # get and return any string/text.
  300. sub get_body {
  301.  
  302. my $emailbody = <STDIN>;
  303. chomp($emailbody);
  304.  
  305. return $emailbody;
  306. }
  307.  
  308. $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' );
  309.  
  310. # This section returns body after encryption.
  311. sub encrypt_body {
  312.  
  313. my ($pbody1, $epass1) = $_;
  314. $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' );
  315. $encrypted = $crypt->encrypt($pbody1, $epass1);
  316.  
  317. return $encrypted;
  318. }
  319.  
  320. # This section decrypts the text.
  321. sub decrypt_body {
  322.  
  323. my ($pbody2, $epass2) = $_;
  324. $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' );
  325. $decrypted = $crypt->decrypt($pbody2, $epass2);
  326.  
  327. return $decrypted;
  328. }

URL: www.jsinix.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.