通过命令行从gmail帐户发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过命令行从gmail帐户发送电子邮件相关的知识,希望对你有一定的参考价值。

This scripts can be used to send email from your gmail account by authenticating. It can be modified to login to any SMTP server.
  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, " JSINIX Gmailer ", RESET;
  64. print " 1. Send email".
  65. " 2. Check email".
  66. " 3. Encrypt email".
  67. " 4. Decrypt email".
  68. " 5. Quit(q)";
  69.  
  70. print " Choice: ";
  71. $choice = <STDIN>;
  72. chomp($choice);
  73.  
  74. switch ($choice) {
  75.  
  76. case '1'
  77. {
  78. system("clear");
  79. mail_send();
  80. print " Press enter to continue"; <STDIN>;
  81. goto START;
  82. }
  83.  
  84. case '2'
  85. {
  86. system("clear");
  87. mail_check_pop();
  88. print " Press enter to continue"; <STDIN>;
  89. goto START;
  90. }
  91.  
  92. case '3'
  93. {
  94. system("clear");
  95. mail_encrypt();
  96. print " Press 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 " Press 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(" ERROR: Unable to connect to mail server. ");
  146.  
  147. $count = $pop->Count();
  148.  
  149. if (($pop->Count()) < 1)
  150. {
  151. print " No messages... ";
  152. print " Press enter to continue"; <STDIN>;
  153. goto START;
  154. }
  155.  
  156. print " There are $count Messages in your inbox ";
  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 " Mail number $1 ";
  165. foreach($pop->Head($i))
  166. {
  167. /^(From|Subject|Email):s+/i && print $_, " ";
  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 " Login ID: ";
  183. my $uname2 = get_user();
  184. print " Password: ";
  185. my $pass2 = get_pass();
  186.  
  187. print " Mail To: ";
  188. my $emailto = <STDIN>;
  189. chomp($emailto);
  190.  
  191. print " Body: ";
  192. $emailbodyplain = get_body();
  193.  
  194. print " Want to encrypt body ?(y/n)";
  195. my $echoice = <STDIN>;
  196. chomp($echoice);
  197.  
  198. if ($echoice eq 'y') {
  199.  
  200. print " Encryption 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 " Wrong 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 " Email sent.";
  226. }
  227.  
  228. # This module does the function of encryption.
  229. # Uses Symmetric encryption.
  230. sub mail_encrypt {
  231.  
  232. print " Encrypt message ";
  233.  
  234. print " Plain text/body: ";
  235. $emailbodyplain2 = get_body();
  236.  
  237. print " Encryption key: ";
  238. $enckey3 = get_pass();
  239.  
  240. my $encryptedmsg1 = encrypt_body($emailbodyplain2, $enckey3);
  241.  
  242. print " Encrypted 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 " Decrypt message ";
  252.  
  253. print " Encrypted text/body: ";
  254. $emailbodyplain1 = get_body();
  255.  
  256. print " Decryption key: ";
  257. $enckey2 = get_pass();
  258.  
  259. my $decryptedmsg1 = decrypt_body($emailbodyplain1, $enckey2);
  260.  
  261. print " Decrypted 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 " Could not connect to server, terminating... ";
  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. }

以上是关于通过命令行从gmail帐户发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 GMail 帐户发送电子邮件? [复制]

通过显示错误的 gmail 服务帐户发送

通过 PHP 从免费的 Gmail 帐户发送 [重复]

让 nodemailer 使用不同的 gmail 帐户发送电子邮件

使用 nodejs 中的服务帐户域范围委派通过 Google Apps Gmail 发送邮件

C#和ASP.NET通过Gmail账户发送邮件的代码