| 1 | ## Domain Registry Interface, SMTP Transport | ||
| 2 | ## | ||
| 3 | ## Copyright (c) 2006,2007,2009,2011,2013 Patrick Mevzek <netdri@dotandco.com>. All rights reserved. | ||
| 4 | ## | ||
| 5 | ## This file is part of Net::DRI | ||
| 6 | ## | ||
| 7 | ## Net::DRI is free software; you can redistribute it and/or modify | ||
| 8 | ## it under the terms of the GNU General Public License as published by | ||
| 9 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 10 | ## (at your option) any later version. | ||
| 11 | ## | ||
| 12 | ## See the LICENSE file that comes with this distribution for more details. | ||
| 13 | #################################################################################################### | ||
| 14 | |||
| 15 | package Net::DRI::Transport::SMTP; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | |||
| 20 | use base qw/Net::DRI::Transport/; | ||
| 21 | |||
| 22 | use Net::SMTP; | ||
| 23 | use Email::Valid; | ||
| 24 | |||
| 25 | use Net::DRI::Exception; | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | Net::DRI::Transport::SMTP - SMTP transport for Net::DRI | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | The following options are available at creation: | ||
| 36 | |||
| 37 | =head1 SUPPORT | ||
| 38 | |||
| 39 | For now, support questions should be sent to: | ||
| 40 | |||
| 41 | E<lt>netdri@dotandco.comE<gt> | ||
| 42 | |||
| 43 | Please also see the SUPPORT file in the distribution. | ||
| 44 | |||
| 45 | =head1 SEE ALSO | ||
| 46 | |||
| 47 | http://www.dotandco.com/services/software/Net-DRI/ | ||
| 48 | |||
| 49 | =head1 AUTHOR | ||
| 50 | |||
| 51 | Patrick Mevzek, E<lt>netdri@dotandco.comE<gt> | ||
| 52 | |||
| 53 | =head1 COPYRIGHT | ||
| 54 | |||
| 55 | Copyright (c) 2006,2007,2009,2011,2013 Patrick Mevzek <netdri@dotandco.com>. | ||
| 56 | All rights reserved. | ||
| 57 | |||
| 58 | This program is free software; you can redistribute it and/or modify | ||
| 59 | it under the terms of the GNU General Public License as published by | ||
| 60 | the Free Software Foundation; either version 2 of the License, or | ||
| 61 | (at your option) any later version. | ||
| 62 | |||
| 63 | See the LICENSE file that comes with this distribution for more details. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | #################################################################################################### | ||
| 68 | |||
| 69 | sub new | ||
| 70 | { | ||
| 71 | my ($class,$ctx,$rp)=@_; | ||
| 72 | my %opts=%$rp; | ||
| 73 | my $self=$class->SUPER::new($ctx,\%opts); ## We are now officially a Net::DRI::Transport instance | ||
| 74 | $self->has_state(0); ## We could be stateful by keeping a live connection to the SMTP host. But it would be useful only for high volumes | ||
| 75 | $self->is_sync(0); | ||
| 76 | $self->name('smtp'); | ||
| 77 | $self->version('0.1'); | ||
| 78 | $self->current_state(0); | ||
| 79 | |||
| 80 | ## Now deal with specifics for this transport | ||
| 81 | my %t; | ||
| 82 | $t{smtphost}=exists($opts{smtphost})? $opts{smtphost} : 'localhost'; | ||
| 83 | $t{cc}=$opts{cc} if (exists($opts{cc}) && Email::Valid->rfc822($opts{cc})); ## Will be added as Cc: to all messages | ||
| 84 | $t{bcc}=$opts{bcc} if (exists($opts{bcc}) && Email::Valid->rfc822($opts{bcc})); ## Ditto as Bcc: | ||
| 85 | |||
| 86 | $self->{transport}=\%t; | ||
| 87 | |||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | #################################################################################################### | ||
| 92 | |||
| 93 | sub end { } | ||
| 94 | |||
| 95 | sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||
| 96 | { | ||
| 97 | my ($self,$ctx,$tosend)=@_; | ||
| 98 | return $self->SUPER::send($ctx,$tosend,\&_send,undef); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _send | ||
| 102 | { | ||
| 103 | my ($self,$count,$tosend)=@_; | ||
| 104 | my $rt=$self->{transport}; | ||
| 105 | |||
| 106 | my $mime=$tosend->as_mime(); | ||
| 107 | my $head=$mime->head(); | ||
| 108 | $head->add('Cc',$rt->{cc}) if exists($rt->{cc}); | ||
| 109 | $head->add('Bcc',$rt->{bcc}) if exists($rt->{bcc}); | ||
| 110 | |||
| 111 | my %rcpts=map { $_ => 1 } map { $head->get_all($_) } ('To','Cc','Bcc'); | ||
| 112 | $head->delete('Bcc'); | ||
| 113 | |||
| 114 | my $smtp=Net::SMTP->new($rt->{smtphost}); | ||
| 115 | my $ok=$smtp->mail($head->get('From'),Bits=>8) && $smtp->to(keys(%rcpts)) && $smtp->data($mime->stringify()) && $smtp->quit(); | ||
| 116 | |||
| 117 | Net::DRI::Exception->die(0,'transport/socket',4,'Unable to send message') unless $ok; | ||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub receive | ||
| 122 | { | ||
| 123 | my ($self,$ctx,$count)=@_; | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | #################################################################################################### | ||
| 128 | 1; |