File Coverage

blib/lib/Email/Send/Netease.pm
Criterion Covered Total %
statement 18 41 43.9
branch 0 8 0.0
condition 0 3 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 26 62 41.9


line stmt bran cond sub pod time code
1             package Email::Send::Netease;
2              
3 1     1   24000 use 5.006;
  1         4  
  1         39  
4 1     1   6 use strict;
  1         3  
  1         34  
5 1     1   4 use warnings;
  1         6  
  1         31  
6 1     1   1285 use MIME::Lite;
  1         56248  
  1         39  
7 1     1   1164 use MIME::Words qw(encode_mimewords);
  1         1595  
  1         65  
8 1     1   9 use Carp qw/croak/;
  1         2  
  1         409  
9              
10             our $VERSION = '0.02';
11              
12              
13             sub new {
14              
15 0     0 1   my $class = shift;
16 0           my $email = shift;
17 0           my $passwd = shift;
18 0           my $debug = shift;
19              
20 0 0 0       if ($email =~ /\@(126|163|188|)\.com$/ or $email =~ /\@yeah\.net$/) {
21             # fine
22             } else {
23 0           croak "must be Netease's email account at one of 163.com, 126.com, 188.com, yeah.net";
24             }
25              
26 0 0         $debug = 0 unless defined $debug;
27              
28 0 0         eval {
29 0           require MIME::Base64;
30 0           require Authen::SASL;
31             } or croak "Need MIME::Base64 and Authen::SASL for sendmail";
32              
33 0           bless {email=>$email,passwd=>$passwd,debug=>$debug}, $class;
34             }
35              
36             sub sendmail {
37              
38 0     0 1   my $self = shift;
39 0           my $title = shift;
40 0           my $html_body = shift;
41 0           my @recepients = @_;
42              
43 0           my $to_address = join ',',@recepients;
44 0           my $from_address = $self->{email};
45 0           my ($user,$domain) = split/\@/,$self->{email};
46 0           my $smtpsvr = 'smtp.'.$domain;
47              
48 0           my $subject = encode_mimewords($title,'Charset','UTF-8');
49              
50 0 0         my $msg = MIME::Lite->new (
51             From => $from_address,
52             To => $to_address,
53             Subject => $subject,
54             Type => 'text/html',
55             Data => $html_body,
56             Encoding => 'base64',
57             ) or croak "create container failed: $!";
58              
59 0           $msg->attr('content-type.charset' => 'UTF-8');
60 0           $msg->send( 'smtp',
61             $smtpsvr,
62             AuthUser=>$user,
63             AuthPass=>$self->{passwd},
64             Debug=>$self->{debug}
65             );
66             }
67              
68              
69             1;
70              
71             =head1 NAME
72              
73             Email::Send::Netease - Send email with Netease's SMTP servers
74              
75             =head1 VERSION
76              
77             Version 0.02
78              
79              
80             =head1 SYNOPSIS
81              
82             use Email::Send::Netease;
83             my $smtp = Email::Send::Netease->new('john@126.com','mypasswd');
84             $smtp->sendmail($subject,$html_body,'foo@163.com','bar@sina.com');
85              
86              
87             =head1 METHODS
88              
89             =head2 new($email, $password, [$debug])
90              
91             Create the object.
92              
93             The email and password are what you registered on Netease, whose domains include 126.com, 163.com, 188.com, yeah.net
94              
95             my $smtp = Email::Send::Netease->new('foo@126.com','password');
96             # or with debug
97             my $smtp = Email::Send::Netease->new('foo@126.com','password',1);
98              
99             =head2 sendmail($subject, $html_body, @recepients)
100              
101             Send the message.
102              
103             The subject and body can be Chinese (if so they must be UTF-8 string).
104             They will be encoded with UTF-8 for sending.
105              
106             The message body should be HTML syntax compatible, it will be sent with text/html format.
107              
108             my $subject = "Hello";
109             my $html_body =<
110            
111            
112            

Hello there

113            

It's nice to see you.

114            
115            
116             EOF
117              
118             $smtp->sendmail($subject,$html_body,'foo@163.com');
119             # send to more than one people
120             $smtp->sendmail($subject,$html_body,'foo@163.com','bar@sina.com', ...);
121            
122              
123             =head1 AUTHOR
124              
125             Ken Peng
126              
127              
128             =head1 BUGS/LIMITATIONS
129              
130             If you have found bugs, please send email to
131              
132              
133             =head1 SUPPORT
134              
135             You can find documentation for this module with the perldoc command.
136              
137             perldoc Email::Send::Netease
138              
139              
140             =head1 COPYRIGHT & LICENSE
141              
142             Copyright 2012 Ken Peng, all rights reserved.
143              
144             This program is free software; you can redistribute it and/or modify
145             it under the same terms as Perl itself.
146