Created
September 21, 2011 16:23
-
-
Save dvinciguerra/1232529 to your computer and use it in GitHub Desktop.
Gmail::SendMail.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# (c) 2011 Bivee. All rights reserveds. | |
# author dvicniguerra < dan.vinciguerra at gmail.com > | |
# | |
# This script is distributed under Perl Itself License | |
# | |
package Gmail::SendMail; | |
use strict; | |
use warnings; | |
# default constructor | |
sub new { | |
return bless { | |
server => 'smtp.gmail.com', | |
port => 587, | |
}, shift || ref shift; | |
} | |
sub configure { | |
my ( $self, %param ) = ( shift, @_ ); | |
eval { | |
$self->{username} = $param{username} || ""; | |
$self->{password} = $param{password} || ""; | |
}; | |
if ( $@ ) { | |
die "Error setting authentication credentials: $@ "; | |
} | |
} | |
# accessor to server attribute | |
sub server { | |
$_[0]->{server} = $_[1] if defined $_[1]; | |
return shift->{server}; | |
} | |
# accessor to port attribute | |
sub port { | |
$_[0]->{port} = $_[1] if defined $_[1]; | |
return shift->{port}; | |
} | |
# accessor to username attribute | |
sub username { | |
$_[0]->{username} = $_[1] if defined $_[1]; | |
return shift->{username}; | |
} | |
# accessor to password attribute | |
sub password { | |
$_[0]->{password} = $_[1] if defined $_[1]; | |
return shift->{password}; | |
} | |
# accessor to from attribute | |
sub from { | |
$_[0]->{from} = $_[1] if defined $_[1]; | |
return shift->{from}; | |
} | |
# accessor to "to" attribute | |
sub to { | |
$_[0]->{to} = $_[1] if defined $_[1]; | |
return shift->{to}; | |
} | |
# accessor to body attribute | |
sub body { | |
$_[0]->{body} = $_[1] if defined $_[1]; | |
return shift->{body}; | |
} | |
# send message method | |
sub send { | |
my $self = shift; | |
eval { | |
use Net::SMTP::TLS; | |
# config mailer | |
my $mail = Net::SMTP::TLS->new( | |
$self->server, | |
Hello => $self->server, | |
Port => $self->port, | |
User => $self->username, | |
Password=> $self->password, | |
); | |
# config mail | |
$mail->mail( $self->from ); | |
$mail->to( $self->to ); | |
$mail->data; | |
$mail->datasend( $self->body ); | |
$mail->dataend; | |
$mail->quit; | |
}; | |
if ( $@ ) { | |
die "Error sending mail: $@ "; | |
} | |
} | |
package main; | |
# getting new object instance | |
my $app = Gmail::SendMail->new; | |
# setting user authentication | |
$app->configure( | |
username => 'dan.vinciguerra@gmail.com', | |
password => 'teste', | |
); | |
# setting message informations | |
$app->from( 'dan.vicniguerra@gmail.com' ); | |
$app->to( 'allana.masters@gmail.com' ); | |
$app->body( qq{ | |
Hi pretty, | |
Do you want to go to party with me today? :) | |
see ya, | |
Daniel Vinciguerra | |
This message was sent using Perl. ;) | |
} ); | |
# lets rock baby ;) | |
$app->send; | |
__END__ | |
=head1 NAME | |
Gmail::SendMail - Exemplo Gmail sender | |
=head1 DESCRIPTION | |
Esta é uma implementação simples de uma classe para | |
trabalhar com envios de emails utilizando como base | |
o servidor do gmail.com. | |
=head1 SINOPSYS | |
use Gmail::SendMail; | |
my $app = Gmail::SendMail->new; | |
$app->configure( | |
username => 'usuario_gmail', | |
password => 'senha_gmail', | |
); | |
$app->from( 'foo@gmail.com' ); | |
$app->to( 'bar@gmail.com' ); | |
$app->body( 'Minha bicicleta amarela!' ); | |
$app->send; | |
=head1 SEE ALSO | |
L<Net::SMTP::TLS>, L<perl> | |
=head1 AUTHOR | |
Daniel Vinciguerra < dan.vinciguerra at gmail.com > | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment