File Coverage

blib/lib/Net/SimpleMail.pm
Criterion Covered Total %
statement 15 37 40.5
branch 0 6 0.0
condition 0 3 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 21 53 39.6


line stmt bran cond sub pod time code
1             package Net::SimpleMail;
2              
3 1     1   147647 use strict;
  1         3  
  1         45  
4 1     1   5 use warnings;
  1         2  
  1         98  
5              
6             our $VERSION = '0.22';
7              
8 1     1   587 use Net::SMTP::SSL;
  1         197213  
  1         112  
9 1     1   825 use Authen::SASL;
  1         1872  
  1         8  
10 1     1   687 use Encode;
  1         22736  
  1         562  
11              
12             require Exporter;
13             our @ISA = qw(Exporter);
14             our @EXPORT = qw(simplemail);
15              
16             my $DEFAULT_HOST = 'space.hostcache.com';
17             my $DEFAULT_PORT = 465;
18              
19              
20             sub simplemail {
21 0     0 1   my ( $recipient, $subject, $message ) = @_;
22              
23             # 检查环境变量
24 0           my $username = $ENV{SM_USER};
25 0           my $password = $ENV{SM_PASS};
26              
27 0 0 0       unless ( defined $username && defined $password ) {
28 0           die "Error: SM_USER and SM_PASS environment variables must be set.\n";
29             }
30              
31             # 连接到 SMTP 服务器
32 0 0         my $smtp = Net::SMTP::SSL->new(
33             $DEFAULT_HOST,
34             Port => $DEFAULT_PORT,
35             Timeout => 30,
36             # Debug => 1,
37             ) or die "Could not connect to SMTP server: $!\n";
38              
39             # 认证
40 0 0         $smtp->auth( $username, $password )
41             or die "Authentication failed: " . $smtp->message() . "\n";
42              
43             # 构造邮件
44 0           my $sender = $username;
45 0           $smtp->mail($sender);
46 0           $smtp->to($recipient);
47              
48             # 准备邮件头
49 0           $smtp->data();
50 0           $smtp->datasend("To: $recipient\n");
51 0           $smtp->datasend("From: $sender\n");
52 0           $smtp->datasend("Subject: " . Encode::encode( 'MIME-Header', $subject ) . "\n");
53 0           $smtp->datasend("Content-Type: text/plain; charset=UTF-8\n");
54 0           $smtp->datasend("Content-Transfer-Encoding: 8bit\n");
55 0           $smtp->datasend("\n");
56              
57             # 发送邮件内容 (确保使用 UTF-8 编码)
58 0           $smtp->datasend( Encode::encode( 'UTF-8', $message ) );
59 0           $smtp->datasend("\n");
60 0           $smtp->dataend();
61              
62             # 关闭连接
63 0           $smtp->quit();
64              
65             # print "Email sent successfully to $recipient\n";
66 0           return 1;
67             }
68              
69             1;
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             Net::SimpleMail - A simple module to send emails via simplemail.co.in
76              
77             =head1 DESCRIPTION
78              
79             This module provides a simple way to send emails using the simplemail.co.in email service.
80             It requires the environment variables C and C to be set with your username
81             and password, respectively.
82              
83             =head1 FUNCTIONS
84              
85             =head2 simplemail( $recipient, $subject, $message )
86              
87             Sends an email to the specified recipient with the given subject and message.
88              
89             =head1 EXAMPLES
90              
91             use Net::SimpleMail;
92             use utf8;
93              
94             # Send a simple email
95             simplemail("test@example.com", "Test Email", "This is a test email message.");
96              
97             # Send an email with Chinese characters
98             simplemail("test@example.com", "中文邮件", "这是一封包含中文的邮件。");
99              
100             =head1 AUTHOR
101              
102             ypeng at t-online.de
103              
104             =cut