File Coverage

blib/lib/Net/SMTP/Bulk.pm
Criterion Covered Total %
statement 8 21 38.1
branch 0 4 0.0
condition 0 2 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 12 32 37.5


line stmt bran cond sub pod time code
1             package Net::SMTP::Bulk;
2            
3 1     1   14621 use 5.006;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         16  
5 1     1   4 use warnings FATAL => 'all';
  1         4  
  1         188  
6            
7             #use Encode;
8             #use Coro;
9             #use Coro::Handle;
10             #use AnyEvent::Socket;
11            
12            
13             =head1 NAME
14            
15             Net::SMTP::Bulk - NonBlocking batch SMTP using Net::SMTP interface
16            
17             =head1 VERSION
18            
19             Version 0.22
20            
21             =cut
22            
23             our $VERSION = '0.22';
24            
25            
26            
27             =head1 SYNOPSIS
28            
29             This is a rewrite of Net::SMTP using AnyEvent and Coro as a backbone. It supports AUTH, SSL and STARTTLS as well. This module can be used as a drop in replacement for Net::SMTP. At this point this module is EXPIREMENTAL, so use at your own risk. Functionality can change at any time.
30            
31             =head1 IMPORTANT
32            
33             Before you start using this module, it is important to understand the fundementals behind it. Now I know it is tempting to skip this part but I assure you that reading this is crucial to using this module. I will try to keep it short.
34            
35             First of all, let me get this out of the way, this module contains 2 methods of implementation. Method 1: Coro+AnyEvent and Method 2: AnyEvent only. These methods somewhat accomplish the same thing but bahave completely different and were created for different purposes.
36            
37             =head2 METHOD 1: Coro+AnyEvent
38            
39             This method was created first, it was used on a server with little ram while trying to send millions of emails. How it works is you have a server+thread queue, once the queue fills up or quit command is called, it sends the emails.
40            
41             The email sending process is in order, which means it will call the MAIL FROM, RCPT TO and etc commands for all emails in the batch at the same time. If 1 email fails in a batch, it is thrown out of the batch and it is up to you to figure out how to handle it through the Callbacks. It is possible for a single command to mess up the entire batch though.
42            
43             Other differences: SSL/STARTTLS handshake is negotiated by a workaround
44            
45             use Net::SMTP::Bulk::Coro;
46            
47             my $smtp = Net::SMTP::Bulk::Coro->new($server, %options);
48            
49             =head2 METHOD 2: AnyEvent only
50            
51             After I got new servers with far more ram, I wanted to prioritize speed. So I created this module, it works quite different than the Coro one and is faster but is most likely more memory intensive.
52            
53             How it works is, you send all the emails to the module and once the quit command is called, all emails are processed on a first come first serve basis.
54            
55             This means if you have a million emails, all those million emails are going into ram.
56            
57             Other differences: SSL/STARTTLS handshake is negotiated AnyEvent::TLS
58            
59             use Net::SMTP::Bulk::AnyEvent;
60            
61             my $smtp = Net::SMTP::Bulk::AnyEvent->new($server, %options);
62            
63             =head2 Which Method should you use?
64            
65             The answer 9/10 times, METHOD 2: AnyEvent only. Why? Because I am using it. While I will go back and try to keep Method 1 up to date when I have time, Method 2 will get far more love since I am using it. If you really plan to use Method 1 and you need some function implemented to keep up with Method 2, please file it in the bug reports on CPAN. And I will prioritize it. But again, only when I have the time.
66            
67             If your worry about using Method 2 comes down to ram, there are workarounds such as having it send in batches of say 1000. And while I can make Method 2 work like Method 1 fully, at this point I have no plan to. So if you are constrained by ram, try dividing up the emails in 1000 at a time for Method 2. If that doesn't work, Method 1 is your way point.
68            
69             Both Methods should work for the most part. Just again, Method 2 will see more love at this point.
70            
71            
72             =head1 SYNTEX
73            
74             See Net::SMTP and methods below for syntax.
75            
76             =head1 SUBROUTINES/METHODS
77            
78             =head2 new($server,%options)
79            
80             =head2 new(%options)
81            
82             Options:
83             Host - Hostname or IP address
84            
85             Port - The port to which to connect to on the server (default: 25)
86            
87             Hello - The domain name you wish to connect to (default: [same as server])
88            
89             Debug - Debug information (0-10 depending on level. Higher level contains all lower levels) (default: 0, 0 - disabled, 5 - summary, 7 - hangs/fails, 8 - passes, 10 - full details) OPTIONAL
90            
91             DebugPath - Set to default Debug Path. use [HOST] and [THREAD] for deeper control of output. Dates can also be appended via [YYYY] = year, [MM] = month, [DD] = day, [WK] = week, [hh] = hour, [mm] = minute OPTIONAL
92            
93             Secure - If you wish to use a secure connection. ( default: 0, 0 - None, 1 - SSL [no verify], 2 - SSL [verify], 3 - STARTTLS [no verify], 4 - STARTTLS [verify]) OPTIONAL [Requires Net::SSLeay]
94            
95             Threads - How many concurrent connections per host (default: 2) OPTIONAL
96            
97             Encode - Encode socket( 1: utf8 )
98            
99             Timeout - Amount of seconds until it gives up on the session and attempts to reconnect ( defaukt: 60 ) OPTIONAL
100            
101             Hosts - An ARRAY containing a list of HASH reference of Hosts OPTIONAL
102            
103             GlobalTimeout - (Method 2 only) Amount of seconds for no activity on any thread. If no activity is seen on any thread, it will reconnect on all threads. Keep in mind the delays you plan to use for Sleep and Retry. ( default: 120 ) OPTIONAL
104            
105             Sleep - (Method 2 only) A HASH that sets a sleep timer in seconds for reconnect attempts. ( Default: Hang=>0, Fail => 0 ) CAVEAT: Since a timer is used the last second is not reliable, so if you set a timer of 30 seconds, it can be anywhere between 29-30 seconds. OPTIONAL
106            
107             Retry - (Method 2 only) Amount of retries until it gives up. ( default: Hang=>1, GlobalHang => 1, Fail => 5 ) Put 0 for unlimited. OPTIONAL
108            
109             Auth - (Method 2 only) An ARRAY containing AUTH details. ( eg. ['AUTO','user','pass'] or to force a mechanism such as LOGIN ['LOGIN','user','pass'] ) OPTIONAL
110            
111             Callbacks - You can supply callback functions on certain conditions, these conditions include: OPTIONAL
112            
113             Pipeline - (Method 2 only) Use pipelining for even quicker sending, makse sure server accepts pipelining. Pipelining should offer a good speed boost but may lead to more complex debugging, especially in mode 2 ( default: 0, 0 - Disabled with 4 round trips [normal], 1 - Pipelining with 2 roundtrips [faster], 2 - Pipelining with 1 roundtrip [fastest] ) OPTIONAL
114            
115             Method1:
116             connect_pass,connect_fail,auth_pass,auth_fail,reconnect_pass,reconnect_fail,pass,fail,hang
117            
118             The callback must return 1 it to follow proper proceedures. You can overwrite the defaults by supplying a different return.
119            
120             1 - Default
121            
122             101 - Remove Thread permanently
123            
124             102 - Remove thread temporarily and reconnect at end of batch
125            
126             103 - Remove thread temporarily and restart at end of batch (If your using an SMTP server with short timeout, it is suggested to use this over reconnect)
127            
128             104 - Remove Thread temporarily
129            
130             202 - Reconnect now
131            
132             203 - Restart now
133            
134             Method2:
135             connect_pass - connected to server
136             pass - Email was accepted by server
137             fail - This triggers if some sort of error happens, be it server did not recognize command or failed to connect. (I will most likely move fail to connect later on to connect_fail)
138             hang - timeout has been reached for sending to email to user, email not sent
139             global_hang - timeout has been reached for entire email sening operation
140             read - callback on every line read
141            
142             The callback must return 1 it to follow proper proceedures. Other callback responses will be implemented later.
143            
144             1 - Default
145            
146             =head2 new(%options, Hosts=>[\%options2,\%options3])
147            
148             You can supply multiple hosts in an array.
149            
150            
151             =head2 auth( [ MECHANISM,] USERNAME, PASSWORD )
152            
153             *Requires Authen::SASL
154            
155             =head2 mail( ADDRESS )
156            
157             =head2 to( ADDRESS )
158            
159             =head2 data()
160            
161             =head2 datasend( DATA )
162            
163             =head2 dataend( DATA )
164            
165             =head2 reconnect( )
166            
167             =head2 quit( [ID] )
168            
169             *ID is Method2 only and is an optional field. It helps you track the ID through debugging. It defaults to epoch time if not passed.
170            
171             =head1 CAVEATS
172            
173             Other then the Caveats described above, it should he noted that there are missing functions for full SMTP compatibility.
174            
175             Missing fuctions include: HELO(EHLO is used), TURN, ATURN, SIZE, ETRN, CHUNKING/BDAT, DSN, RSET, VRFY, HELP
176            
177             These functions will be added as time goes on. If you need a certain function to have priority, request it .
178             =cut
179            
180             sub new {
181 0     0 1   my $class=shift;
182 0           my %new=@_;
183 0           my $self={};
184            
185 0 0 0       if (($new{Mode}||'') eq 'AnyEvent') {
186 0           require Net::SMTP::Bulk::AnyEvent;
187 0           $self=Net::SMTP::Bulk::AnyEvent->new(@_);
188             } else {
189 0 0         if (eval { require Net::SMTP::Bulk::Coro; 1 }) {
  0            
  0            
190 0           $self=Net::SMTP::Bulk::Coro->new(@_);
191             } else {
192 0           require Net::SMTP::Bulk::AnyEvent;
193 0           $self=Net::SMTP::Bulk::AnyEvent->new(@_);
194             }
195            
196            
197             }
198            
199            
200            
201 0           return $self;
202             }
203            
204            
205             #########################################################################
206            
207            
208             =head1 AUTHOR
209            
210             KnowZero
211            
212             =head1 BUGS
213            
214             Please report any bugs or feature requests to C, or through
215             the web interface at L. I will be notified, and then you'll
216             automatically be notified of progress on your bug as I make changes.
217            
218            
219            
220            
221             =head1 SUPPORT
222            
223             You can find documentation for this module with the perldoc command.
224            
225             perldoc Net::SMTP::Bulk
226            
227            
228             You can also look for information at:
229            
230             =over 4
231            
232             =item * RT: CPAN's request tracker (report bugs here)
233            
234             L
235            
236             =item * AnnoCPAN: Annotated CPAN documentation
237            
238             L
239            
240             =item * CPAN Ratings
241            
242             L
243            
244             =item * Search CPAN
245            
246             L
247            
248             =back
249            
250            
251             =head1 ACKNOWLEDGEMENTS
252            
253            
254             =head1 LICENSE AND COPYRIGHT
255            
256             Copyright 2013 KnowZero.
257            
258             This program is free software; you can redistribute it and/or modify it
259             under the terms of either: the GNU General Public License as published
260             by the Free Software Foundation; or the Artistic License.
261            
262             See L for more information.
263            
264            
265             =cut
266            
267             1; # End of Net::SMTP::Bulk