blib/lib/Net/SimpleMail.pm

filter: Statementstmt 50.0% 22 / 44 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 45.1% 28 / 62 CC 5 SCAR 27.4
CC
5
Cov
42%
simplemail
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 15.5
↑ Summary
1 package Net::SimpleMail;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.22';
7  
8 1 ▶use Net::SMTP::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Authen::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 require Exporter;
13 1 our @ISA = qw(Exporter);
14 1 our @EXPORT = qw(simplemail);
15  
16 1 my $DEFAULT_HOST = 'space.hostcache.com';
17 1 my $DEFAULT_PORT = 465;
18  
19  
20 0 ▶sub simplemail {
Subroutine simplemail: not called CC=5 SCAR=34.0
Pod documented
21 0 my ( $recipient, $subject, $message ) = @_;
22  
23 # 检查环境变量
24 0 my $username = $ENV{SM_USER};
25 0 my $password = $ENV{SM_PASS};
26  
27 0 ▶ unless ( defined $username && defined $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
28 0 die "Error: SM_USER and SM_PASS environment variables must be set.\n";
29 }
30  
31 # 连接到 SMTP 服务器
32 0 ▶ my $smtp = Net::SMTP::SSL->new(
Branch: 1 branch
BranchTrueFalseTotal
my $smtp = "Net::SMTP::SSL"->new($DEFAULT_HOST, "Port", $DEFAULT_PORT, "Timeout", 30) or die "Could not connect to SMTP server: $!\n" 0 0 0
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 ▶ $smtp->auth( $username, $password )
Branch: 1 branch
BranchTrueFalseTotal
$smtp->auth($username, $password) or die 'Authentication failed: ' . $smtp->message . "\n" 0 0 0
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 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<SM_USER> and C<SM_PASS> 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
↑ Summary