Latest web development tutorials

Perl Enviar correo

Si el programa se ejecuta en un sistema Linux / Unix, podrás enviar correo usando la herramienta desendmail en Perl.

El siguiente es un simple script de ejemplo para el envío de correo:

#!/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";

El programa anterior, la salida es:

邮件发送成功

En circunstancias normales, el mensaje anterior será QQ-mail intercepción, ¿qué puedo añadirlo a la lista blanca, el modo de funcionamiento puede hacer clic en: https: //kf.qq.com/faq/120322fu63YV130805rYRFzu.html

Después de la lista blanca puede recibir correo.

Enviar mensajes en formato HTML

Podemos añadir un encabezadode tipo de contenidoen elmensaje: text / html \ npara enviar mensajes en formato HTML, los ejemplos son los siguientes:

#!/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";

Después de hacerlo, puede comprobar el contenido del mensaje, de la siguiente manera:


Utilizar el módulo MIME :: Lite

Si está utilizando un sistema de ventanas, no hay ninguna herramienta Sendmail. A continuación, puede utilizar el Perl MIME: Módulo Lite como un cliente de correo para enviar correo.

MIME: Lite Descargar módulo como: el MIME-Lite-3.030.tar.gz .

Aquí directamente CPAN para instalar (requiere privilegios de root), no descargue:

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

Después de una instalación correcta, tenemos que demostrar un ejemplo:

#!/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";

Después de hacerlo, puede comprobar el contenido del mensaje, de la siguiente manera:

Enviar mensajes en formato HTML

Podemos añadir un encabezadode tipo de contenidoen elmensaje: text / html \ npara enviar mensajes en formato HTML, los ejemplos son los siguientes:

#!/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";

Después de hacerlo, puede comprobar el contenido del mensaje, de la siguiente manera:

Enviar correo con archivos adjuntos

Enviar e-mail con archivos adjuntos ejemplos son los siguientes:

#!/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";

Después de hacerlo, puede comprobar el contenido del mensaje, de la siguiente manera:

Se puede utilizar más de un $ msg-> Adjuntar para añadir más elementos adjuntos.