· 8 years ago · Aug 12, 2018, 03:16 AM
1#!/usr/bin/perl
2
3# (c) 2011 Bivee. All rights reserveds.
4# author dvicniguerra < dan.vinciguerra at gmail.com >
5#
6# This script is distributed under Perl Itself License
7#
8
9package Gmail::SendMail;
10
11use strict;
12use warnings;
13
14# default constructor
15sub new {
16 return bless {
17 server => 'smtp.gmail.com',
18 port => 587,
19 }, shift || ref shift;
20}
21
22sub configure {
23 my ( $self, %param ) = ( shift, @_ );
24
25 eval {
26 $self->{username} = $param{username} || "";
27 $self->{password} = $param{password} || "";
28 };
29 if ( $@ ) {
30 die "Error setting authentication credentials: $@ ";
31 }
32}
33
34# accessor to server attribute
35sub server {
36 $_[0]->{server} = $_[1] if defined $_[1];
37 return shift->{server};
38}
39
40# accessor to port attribute
41sub port {
42 $_[0]->{port} = $_[1] if defined $_[1];
43 return shift->{port};
44}
45
46# accessor to username attribute
47sub username {
48 $_[0]->{username} = $_[1] if defined $_[1];
49 return shift->{username};
50}
51
52# accessor to password attribute
53sub password {
54 $_[0]->{password} = $_[1] if defined $_[1];
55 return shift->{password};
56}
57
58# accessor to from attribute
59sub from {
60 $_[0]->{from} = $_[1] if defined $_[1];
61 return shift->{from};
62}
63
64# accessor to "to" attribute
65sub to {
66 $_[0]->{to} = $_[1] if defined $_[1];
67 return shift->{to};
68}
69
70# accessor to body attribute
71sub body {
72 $_[0]->{body} = $_[1] if defined $_[1];
73 return shift->{body};
74}
75
76# send message method
77sub send {
78 my $self = shift;
79
80 eval {
81 use Net::SMTP::TLS;
82
83 # config mailer
84 my $mail = Net::SMTP::TLS->new(
85 $self->server,
86 Hello => $self->server,
87 Port => $self->port,
88 User => $self->username,
89 Password=> $self->password,
90 );
91
92 # config mail
93 $mail->mail( $self->from );
94 $mail->to( $self->to );
95 $mail->data;
96 $mail->datasend( $self->body );
97 $mail->dataend;
98 $mail->quit;
99 };
100 if ( $@ ) {
101 die "Error sending mail: $@ ";
102 }
103}
104
105
106package main;
107
108# getting new object instance
109my $app = Gmail::SendMail->new;
110
111# setting user authentication
112$app->configure(
113 username => 'dan.vinciguerra@gmail.com',
114 password => 'teste',
115);
116
117# setting message informations
118$app->from( 'dan.vicniguerra@gmail.com' );
119$app->to( 'allana.masters@gmail.com' );
120$app->body( qq{
121 Hi pretty,
122
123 Do you want to go to party with me today? :)
124 see ya,
125
126 Daniel Vinciguerra
127 This message was sent using Perl. ;)
128} );
129
130# lets rock baby ;)
131$app->send;
132
133__END__
134
135=head1 NAME
136
137Gmail::SendMail - Exemplo Gmail sender
138
139=head1 DESCRIPTION
140
141Esta é uma implementação simples de uma classe para
142trabalhar com envios de emails utilizando como base
143o servidor do gmail.com.
144
145=head1 SINOPSYS
146
147 use Gmail::SendMail;
148
149 my $app = Gmail::SendMail->new;
150
151 $app->configure(
152 username => 'usuario_gmail',
153 password => 'senha_gmail',
154 );
155
156 $app->from( 'foo@gmail.com' );
157 $app->to( 'bar@gmail.com' );
158 $app->body( 'Minha bicicleta amarela!' );
159
160 $app->send;
161
162=head1 SEE ALSO
163
164L<Net::SMTP::TLS>, L<perl>
165
166=head1 AUTHOR
167
168Daniel Vinciguerra < dan.vinciguerra at gmail.com >
169
170=cut