Latest web development tutorials

Perl Invia Mail

Se il programma è in esecuzione su un sistema Linux / Unix, sarete in grado di inviare posta elettronica utilizzando lo strumentosendmail in Perl.

Quanto segue è uno script semplice esempio per l'invio di posta elettronica:

#!/usr/bin/perl

# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '这是一封使用 Perl 发送的邮件。';

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

# 邮件头部
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# 邮箱信息
print MAIL $message;

close(MAIL);
print "邮件发送成功\n";

Il programma precedente, l'uscita è:

邮件发送成功

In circostanze normali, il messaggio di cui sopra sarà QQ-mail intercettazione, cosa posso aggiungerlo alla lista bianca, la modalità di funzionamento può cliccare su: https: //kf.qq.com/faq/120322fu63YV130805rYRFzu.html

Dopo whitelist possono ricevere la posta.

Invia messaggi in formato HTML

Possiamo aggiungere un'intestazioneContent-type nel messaggio: text / html \ nper inviare i messaggi in formato HTML, esempi sono i seguenti:

#!/usr/bin/perl
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>你好,我来自本教程,地址是:http://www.w3big.com。</p>';
 
open(MAIL, "|/usr/sbin/sendmail -t");
 
# 邮件头部
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-type: text/html\n";
# 邮箱信息
print MAIL $message;

close(MAIL);
print "邮件发送成功\n";

Dopo aver fatto, è possibile controllare il contenuto del messaggio, come segue:


Utilizzare modulo MIME :: Lite

Se si utilizza un sistema a finestre, non esiste uno strumento sendmail. Quindi è possibile utilizzare il MIME perl: modulo Lite come un client di posta per inviare la posta.

MIME: Lite modulo Scarica come: il MIME-Lite-3.030.tar.gz .

Qui direttamente CPAN di installare (richiede i privilegi di root), non scaricare:

$ cpan -i MIME::Lite
……
  /usr/bin/make install  -- OK

Dopo una corretta installazione, dobbiamo dimostrare un esempio:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块。';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );
                 
$msg->send;
print "邮件发送成功\n";

Dopo aver fatto, è possibile controllare il contenuto del messaggio, come segue:

Invia messaggi in formato HTML

Possiamo aggiungere un'intestazioneContent-type nel messaggio: text / html \ nper inviare i messaggi in formato HTML, esempi sono i seguenti:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '<h1>这是一封使用 Perl 发送的邮件<h1><p>使用了 MIME::Lite 模块。</p><p>来自本教程,地址是:http://www.w3big.com。</p>';

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

# 添加头部信息
$msg->attr("content-type" => "text/html");                         
$msg->send;
print "邮件发送成功\n";

Dopo aver fatto, è possibile controllare il contenuto del messaggio, come segue:

Invia mail con allegati

Invia e-mail con allegati esempi sono i seguenti:

#!/usr/bin/perl
use MIME::Lite;
 
# 接收邮箱,这里我设置为我的 QQ 邮箱,你需要修改它为你自己的邮箱
$to = '[email protected]';
# 抄送者,多个使用逗号隔开
# $cc = '[email protected], [email protected]';

#发送者邮箱
$from = '[email protected]';
#标题
$subject = '本教程 Perl 发送邮件测试';
$message = '这是一封使用 Perl 发送的邮件,使用了 MIME::Lite 模块,包含了附件。';

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


$msg->attach (
              Type => 'TEXT',
              Data => $message
);# 指定附件信息
$msg->attach(Type        => 'TEXT',
             Path        => './w3big.txt',   # 当前目录下
             Filename    => 'w3big.txt',
             Disposition => 'attachment'
            );
$msg->send;
print "邮件发送成功\n";

Dopo aver fatto, è possibile controllare il contenuto del messaggio, come segue:

È possibile utilizzare più di un $ msg> Collega per aggiungere altri allegati.