通过命令行从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.
#!/usr/bin/perl use strict; use warnings; use Mail::POP3Client; use IO::Socket::SSL; use Net::SMTP::TLS; # There will be a bug using the Net::SMTP::TLS module # So the fix i found here: # http://crunchbang.org/forums/viewtopic.php?pid=361299 # invalid SSL_version specified at /usr/share/perl5/IO/Socket/SSL.pm line # replace this: m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))$}i # with this: m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))}i use Term::ReadKey; use Switch; use Mail::IMAPClient; use Crypt::DES; use Crypt::Lite; # Declaring all variables my $cgi; my $username; my $password; my $mailhost; my $port; my $pop; my $count; my $i; my $uname; my $pass; my $emailto; my $emailbody; my $mailer; my $emaillogin; my $uname1; my $pass1; my $choice = ''; my $emailbodyfin; my $emailbodyplain; my $enckey1; my $cipher; my $ciphertext1; my $encrypted; my $crypt; my $emailbodyplain1; my $enckey2; my $decryptedmsg = ''; my $dcrypt; my $decrypted; my $encryptedmsg = ''; my $enckey3; my $emailbodyplain2; # This is the main control section where # the respective modules are called from. # Ideally its good not to declare any # module in this. while ($choice ne '5') { START: " 2. Check email". " 3. Encrypt email". " 4. Decrypt email". " 5. Quit(q)"; $choice = <STDIN>; switch ($choice) { case '1' { mail_send(); goto START; } case '2' { mail_check_pop(); goto START; } case '3' { mail_encrypt(); goto START; } case '4' { mail_decrypt(); #encdec('test','test'); goto START; } case '5' { } case 'q' { } } # The while loop ends here. } # This module can connect to the # servers of google/gmail using POP3 # There is one more module named # mail_check_imap that you can leverage # IMAP to check the emails. sub mail_check_pop { $cgi = new CGI; ($uname1, $pass1) = get_credentials(); $mailhost = 'pop.gmail.com'; $port = '995'; $pop = Mail::POP3Client->new(USER=> $uname1, PASSWORD => $pass1, HOST => $mailhost, PORT => $port, USESSL => 'true', $count = $pop->Count(); if (($pop->Count()) < 1) { goto START; } my $eno = <STDIN>; for(my $i = 1; $i <= $eno; $i++) { foreach($pop->Head($i)) { } } $pop->Close(); } # This module is used to send emails # from ur gmail account. We connect to # the SMTP server of gmail using the credintials # and send email to anyone. sub mail_send { my $uname2 = get_user(); my $pass2 = get_pass(); my $emailto = <STDIN>; $emailbodyplain = get_body(); my $echoice = <STDIN>; if ($echoice eq 'y') { $enckey1 = get_pass(); $emailbodyfin = encrypt_body($emailbodyplain, $enckey1); } elsif ($echoice eq 'n') { $emailbodyfin = $emailbodyplain; } else { } my $mailer = new Net::SMTP::TLS( 'smtp.gmail.com', Hello => 'smtp.gmail.com', Port => 587, User => $uname2, Password=> $pass2); $mailer->mail($emailto); $mailer->to($emailto); $mailer->data; $mailer->datasend($emailbodyfin); $mailer->dataend; $mailer->quit; } # This module does the function of encryption. # Uses Symmetric encryption. sub mail_encrypt { $emailbodyplain2 = get_body(); $enckey3 = get_pass(); my $encryptedmsg1 = encrypt_body($emailbodyplain2, $enckey3); } # This sections is used to decrypt the # encrypted message using the same symmetric # key that was used to encrypt. sub mail_decrypt { $emailbodyplain1 = get_body(); $enckey2 = get_pass(); my $decryptedmsg1 = decrypt_body($emailbodyplain1, $enckey2); } # Gets the username and returns it. sub get_user { my $emaillogin = <STDIN>; } # Gets the password without echoing it on the STDOUT. sub get_pass { ReadMode('noecho'); ReadMode(0); } # This is the module to check email using # IMAP. sub mail_check_imap { my ($uname3, $pass3) = get_credentials(); my $imap = Mail::IMAPClient->new ( Server => 'imap.gmail.com', User => $uname3, Password => $pass3, Port => 993, Ssl => 1, Uid => 1 ) } # Gets the body for the email. Can be used to # get and return any string/text. sub get_body { my $emailbody = <STDIN>; } $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' ); # This section returns body after encryption. sub encrypt_body { my ($pbody1, $epass1) = $_; $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' ); $encrypted = $crypt->encrypt($pbody1, $epass1); } # This section decrypts the text. sub decrypt_body { my ($pbody2, $epass2) = $_; $crypt = Crypt::Lite->new( debug => 0, encoding => 'hex8' ); $decrypted = $crypt->decrypt($pbody2, $epass2); }
以上是关于通过命令行从gmail帐户发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章
让 nodemailer 使用不同的 gmail 帐户发送电子邮件