#!/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;
如果你想使用 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
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
#!/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";