Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment