Like Share Discussion Bookmark Smile

J.J. Huang   2021-06-10   Perl   瀏覽次數:次   DMCA.com Protection Status

Perl - 第二十章 | Perl 發送電子郵件

這邊提供十分鐘臨時信箱做以下示範:連結

如果你在 Linux/Unix 機器上工作,那麼你可以簡單地在 Perl 程序中使用sendmail實用程序來發送電子郵件。這是一個可以向給定電子郵件 ID 發送電子郵件的範例腳本。只需確保給定的 sendmail 實用程序路徑正確即可。對於你的 Linux/Unix 機器,這可能會有所不同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/perl

$to = 'fwrrotkjeremnbnqpf@twzhhq.com';
$from = 'morosedog@jj.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";

執行以上程序,輸出結果為:

1
Email Sent Successfully

實際上,上面的腳本是一個客戶端電子郵件腳本,它將起草電子郵件並提交到在你的 Linux/Unix 機器上本地運行的服務器。此腳本不負責將電子郵件發送到實際目的地。因此,你必須確保電子郵件服務器在你的計算機上正確配置並運行,以將電子郵件發送到給定的電子郵件 ID。

發送 HTML 消息

如果你想使用 sendmail 發送 HTML 格式的電子郵件,那麼你只需在電子郵件的標題部分添加Content-type: text/html\n,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl

$to = 'fwrrotkjeremnbnqpf@twzhhq.com';
$from = 'morosedog@jj.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Content-type: text/html\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";

執行以上程序,輸出結果為:

1
Email Sent Successfully

使用 MIME::Lite 模組

如果你在 Windows 機器上工作,那麼你將無法訪問 sendmail 實用程序。但是你可以使用 MIME:Lite perl 模塊編寫自己的電子郵件客戶端。你可以從MIME-Lite-3.01.tar.gz下載此模塊並將其安裝在 Windows 或 Linux/Unix 計算機上。

Windows 按照簡單的步驟安裝它 -

1
2
3
4
5
$ tar xvfz MIME-Lite-3.01.tar.gz
$ cd MIME-Lite-3.01
$ perl Makefile.PL
$ make
$ make install

MacOS 透過 cpan 安裝它 -

1
$ cpan MIME::Lite

安裝過程訊息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Loading internal logger. Log::Log4perl recommended for better logging
Reading '/Users/morose/.cpan/Metadata'
Database was generated on Wed, 02 Jun 2021 03:55:43 GMT
Fetching with LWP:
http://www.cpan.org/authors/01mailrc.txt.gz
Reading '/Users/morose/.cpan/sources/authors/01mailrc.txt.gz'
............................................................................DONE
Fetching with LWP:
http://www.cpan.org/modules/02packages.details.txt.gz
Reading '/Users/morose/.cpan/sources/modules/02packages.details.txt.gz'
Database was generated on Mon, 07 Jun 2021 00:55:37 GMT
............................................................................DONE
Fetching with LWP:
http://www.cpan.org/modules/03modlist.data.gz
Reading '/Users/morose/.cpan/sources/modules/03modlist.data.gz'
DONE
Writing /Users/morose/.cpan/Metadata
Running install for module 'MIME::Lite'
Fetching with LWP:
http://www.cpan.org/authors/id/R/RJ/RJBS/MIME-Lite-3.031.tar.gz
Checksum for /Users/morose/.cpan/sources/authors/id/R/RJ/RJBS/MIME-Lite-3.031.tar.gz ok
'YAML' not installed, will not store persistent state
Configuring R/RJ/RJBS/MIME-Lite-3.031.tar.gz with Makefile.PL
MIME::Lite is designed to take advantage of a variety of external modules
if they are not present then MIME::Lite will attempt to do its best
but its strongly recommend that you install them.

... 中間省略

Installing /usr/local/Cellar/perl/5.34.0/lib/perl5/site_perl/5.34.0/MIME/Lite.pm
Installing /usr/local/Cellar/perl/5.34.0/lib/perl5/site_perl/5.34.0/MIME/changes.pod
Installing /usr/local/Cellar/perl/5.34.0/share/man/man3/MIME::Lite.3
Installing /usr/local/Cellar/perl/5.34.0/share/man/man3/MIME::changes.3
Appending installation info to /usr/local/Cellar/perl/5.34.0/lib/perl5/5.34.0/darwin-thread-multi-2level/perllocal.pod
RJBS/MIME-Lite-3.031.tar.gz
/usr/bin/make install -- OK

就是這樣,你將在你的機器上安裝 MIME::Lite 模塊。現在你已準備好使用下面解釋的簡單腳本發送電子郵件。

註:CPAN搜尋網

發送普通消息

現在以下是一個腳本,它將負責將電子郵件發送到給定的電子郵件 ID:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl
use MIME::Lite;

$to = 'fwrrotkjeremnbnqpf@twzhhq.com';
$cc = 'efgh@mail.com';
$from = 'morosedog@jj.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);

$msg->send;
print "Email Sent Successfully\n";

執行以上程序,輸出結果為:

1
Email Sent Successfully

發送 HTML 消息

如果你想使用 sendmail 發送 HTML 格式的電子郵件,那麼你只需在電子郵件的標題部分添加Content-type: text/html\n 即可。以下是腳本,它將負責發送 HTML 格式的電子郵件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
use MIME::Lite;

$to = 'fwrrotkjeremnbnqpf@twzhhq.com';
$cc = 'efgh@mail.com';
$from = 'morosedog@jj.com';
$subject = 'Test Email';
$message = '<h1>This is test email sent by Perl Script</h1>';

$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Data => $message
);

$msg->attr("content-type" => "text/html");
$msg->send;
print "Email Sent Successfully\n";

執行以上程序,輸出結果為:

1
Email Sent Successfully

發送附件

如果你想發送附件,則以下腳本可達到目的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl
use MIME::Lite;

$to = 'fwrrotkjeremnbnqpf@twzhhq.com';
$cc = 'efgh@mail.com';
$from = 'morosedog@jj.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed'
);

# Add your text message.
$msg->attach(Type => 'text',
Data => $message
);

# Specify your file as attachement.
$msg->attach(Type => 'image/gif',
Path => '/tmp/logo.gif',
Filename => 'logo.gif',
Disposition => 'attachment'
);
$msg->send;
print "Email Sent Successfully\n";

你可以使用 attach() 方法在電子郵件中附加任意數量的文件。

使用 SMTP 服務器

如果你的機器沒有運行電子郵件服務器,那麼你可以使用遠程位置可用的任何其他電子郵件服務器。但是要使用任何其他電子郵件服務器,你需要有一個 id、密碼、URL 等。一旦你擁有所有必需的信息,你只需在send()方法中提供該信息,如下所示:

1
$msg->send('smtp', "smtp.myisp.net", AuthUser=>"id", AuthPass=>"password" );

你可以聯繫你的電子郵件服務器管理員以獲取上述使用信息,如果用戶 ID 和密碼尚不可用,那麼你的管理員可以在幾分鐘內創建它。


註:以上參考了
Tutorialspoint, Perl - Sending Email