[PATCH] Add SMTP TLS email support to allow gmail use

Bill Denton dentonwe at gmail.com
Thu Jul 26 07:44:24 EDT 2012


Add SMTP TLS to support gmail. This allows me to send get_iplayer
output to myself using gmail as I don't have an email server on the
Ubuntu Linux machine. Been using for a few months. Patch is against
current version in git infradead.org. Requires Perl Net::SMTP::TLS,
which under Ubuntu can be installed by "apt-get install
libnet-smtp-tls-perl" as root.

When using this with gmail, I strongly recommend using an
application-specific password:
http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833

Background information:
http://www.nixtutor.com/linux/sending-mail-through-gmail-with-perl/
http://cpansearch.perl.org/src/RJBS/Email-Send-2.198/lib/Email/Send/SMTP.pm
http://search.cpan.org/~gbarr/libnet-1.22/Net/SMTP.pm
http://search.cpan.org/~awestholm/Net-SMTP-TLS-0.12/lib/Net/SMTP/TLS.pm

diff original/get_iplayer email/get_iplayer

166a167,170
> 	emailsecurity	=> [ 1, "emailsecurity|email-security=s", 'Output', '--email-security <TLS|SSL|none>', "Email security TLS, SSL, none (default: none)"],
> 	emailpassword	=> [ 1, "emailpassword|email-password=s", 'Output', '--email-password <password>', "Email password"],
> 	emailport       => [ 1, "emailport|email-port=s", 'Output', '--email-port <port number>', "Email port number (default: appropirate port for --email-security)"],
> 	emailuser	=> [ 1, "emailuser|email-user=s", 'Output', '--email-user <username>', "Email username"],
2405c2409,2410
< # Reference: http://sial.org/howto/perl/Net-SMTP/
---
> # References: http://sial.org/howto/perl/Net-SMTP/, http://cpansearch.perl.org/src/RJBS/Email-Send-2.198/lib/Email/Send/SMTP.pm
> #
2408,2409c2413,2417
< 	# Check if we have Net::SMTP installed - might not be for the
windows installer
< 	eval "use Net::SMTP";
---
> 	# Check if we have Net::SMTP/Net::SMTP::TLS/Net::SMTP::SSL installed - might not be for the windows installer
> 	my $smtpclass = ( $opt->{emailsecurity} eq "SSL" )  ? 'Net::SMTP::SSL'
> 			: ( $opt->{emailsecurity} eq "TLS" ) ? 'Net::SMTP::TLS'
>                    	: 'Net::SMTP';
> 	eval "use $smtpclass";
2411c2419
< 		main::logger "WARNING: Please download and run latest installer or
install the Net::SMTP perl module to use --email options\n";
---
> 		main::logger "WARNING: Please download and run latest installer or install the Net::SMTP, Net::SMTP::SSL, or NET::SMTP:TLS perl module(s) to use --email options\n";
2417a2426,2433
> 	my $password = $opt->{emailpassword};
> 	my $user = $opt->{emailuser};
> 	my $port = $opt->{emailport};
> 	if ( ! $port ) {
> 		$port = ( $opt->{emailsecurity} eq "SSL" )  ? 465
>                         : ( $opt->{emailsecurity} eq "TLS" ) ? 587
>                         : 25;
> 	}
2435c2451,2462
< 	my $smtp = Net::SMTP->new($smtphost);
---
> 	my $smtp;
> 	if ( $opt->{emailsecurity} ne 'TLS' ) {
> 		$smtp = $smtpclass->new($smtphost);
> 	} else {
> 		eval {
> 			$smtp = Net::SMTP::TLS->new(
> 			$smtphost,
> 			Port    => $port,
> 			User => $user,
> 			Password=> $password);
> 		};
> 	}
2441,2446c2468,2480
< 	$smtp->mail( $sender ) || push @mail_failure, "MAIL FROM: $sender";
< 	$smtp->to( $recipient ) || push @mail_failure, "RCPT TO: $recipient";
< 	$smtp->data() || push @mail_failure, 'DATA';
< 	$smtp->datasend( $message ) || push @mail_failure, 'Message Data';
< 	$smtp->dataend() || push @mail_failure, 'End of DATA';
< 	$smtp->quit() || push @mail_failure, 'QUIT';
---
> 	 # ::TLS has no useful return value, but will croak on failure.
> 	eval { $smtp->mail( $sender ) };
> 	push @mail_failure, "MAIL FROM: $sender" if $@;
> 	eval { $smtp->to( $recipient ) };
> 	push @mail_failure, "RCPT TO: $recipient" if $@;
> 	eval { $smtp->data() };
> 	push @mail_failure, 'DATA' if $@;
> 	eval { $smtp->datasend( $message ) };
> 	push @mail_failure, 'Message Data' if $@;
> 	eval { $smtp->dataend() };
> 	push @mail_failure, 'End of DATA' if $@;
> 	eval { $smtp->quit() };
> 	push @mail_failure, 'QUIT' if $@;


Bill.



More information about the get_iplayer mailing list