| 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
Condition0%0/3
defined $username and defined $password
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||