Perl Sending Email
If your program runs on a Linux/Unix system, you can use the sendmail tool in Perl to send email.
The following is a simple script example for sending email:
Example
#!/usr/bin/perl
Recipient email; here I set it to my QQ email, you need to change it to your own email
$to
=
'
429240967@qq.com
'
;
# Sender email
$from
=
'
test@runoob.com
'
;
# Subject
$subject
=
'
Runoob Tutorial Perl send email test
'
;
$message
=
'
This is an email sent using Perl.
'
;
open
(
,
"
|/usr/sbin/sendmail -t
"
)
;
Email header
"
To:
$to
\n
"
;
"
From:
$from
\n
"
;
"
Subject:
$subject
\n
\n
"
;
Email information
$message
;
close
(
)
;
"
Mail sent successfully
\n
"
;
Running the above program, the output is:
邮件发送成功
Normally, the above email will be blocked by QQ email; we can add it to the whitelist by clicking:https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html
After adding to the whitelist, you can receive the email normally.

Sending HTML-format Email
We can add the following to the email header Content-type: text/html\n to send an HTML-format email, as shown in the following example:
Example
#!/usr/bin/perl
Recipient email; here I set it to my QQ email, you need to change it to your own email
$to
=
'
429240967@qq.com
'
;
# Sender email
$from
=
'
test@runoob.com
'
;
# Subject
$subject
=
'
Runoob Tutorial Perl send email test
'
;
$message
=
'
This is an email sent using Perl
Hello, I am from Runoob Tutorial, address: http://www.runoob.com.
'
;
open
(
,
"
|/usr/sbin/sendmail -t
"
)
;
Email header
"
To:
$to
\n
"
;
"
From:
$from
\n
"
;
"
Subject:
$subject
\n
"
;
"
Content-type: text/html
\n
"
;
Email information
$message
;
close
(
)
;
"
Mail sent successfully
\n
"
;
After successful execution, view the email content as shown below:

Using the MIME::Lite Module
If you are using a Windows system without the sendmail tool, you can use Perl's MIME:Lite module as a mail client to send email.
The download address for the MIME:Lite module is:MIME-Lite-3.030.tar.gz。
Here we install it directly with cpan (requires root privileges), no download needed:
$ cpan -i MIME::Lite
……
/usr/bin/make install -- OK
After successful installation, let's demonstrate an example:
Example
#!/usr/bin/perl
use MIME::Lite
;
Recipient email; here I set it to my QQ email, you need to change it to your own email
$to
=
'
429240967@qq.com
'
;
CC recipients, multiple separated by commas
$cc = 'test1@runoob.com, test2@runoob.com';
# Sender email
$from
=
'
test@runoob.com
'
;
# Subject
$subject
=
'
Runoob Tutorial Perl send email test
'
;
$message
=
'
This is an email sent using Perl, using the MIME::Lite module.
'
;
$msg
=
MIME::Lite
->
new
(
From
=>
$from
,
To
=>
$to
,
Cc
=>
$cc
,
Subject
=>
$subject
,
Data
=>
$message
)
;
$msg
->
send
;
"
Mail sent successfully
\n
"
;
After successful execution, view the email content as shown below:

Sending HTML-format Email
We can add the following to the email header Content-type: text/html\n to send an HTML-format email, as shown in the following example:
Example
#!/usr/bin/perl
use MIME::Lite
;
Recipient email; here I set it to my QQ email, you need to change it to your own email
$to
=
'
429240967@qq.com
'
;
CC recipients, multiple separated by commas
$cc = 'test1@runoob.com, test2@runoob.com';
# Sender email
$from
=
'
test@runoob.com
'
;
# Subject
$subject
=
'
Runoob Tutorial Perl send email test
'
;
$message
=
'
This is an email sent using Perl
used the MIME::Lite module.
From Runoob Tutorial, address: http://www.runoob.com.
'
;
$msg
=
MIME::Lite
->
new
(
From
=>
$from
,
To
=>
$to
,
Cc
=>
$cc
,
Subject
=>
$subject
,
Data
=>
$message
)
;
Add Header Information
$msg
->
attr
(
"
content-type
"
=>
"
text/html
"
)
;
$msg
->
send
;
"
Mail sent successfully
\n
"
;
After successful execution, view the email content as shown below:

Sending Email with Attachments
An example of sending an email with an attachment is as follows:
Example
#!/usr/bin/perl
use MIME::Lite
;
Recipient email; here I set it to my QQ email, you need to change it to your own email
$to
=
'
429240967@qq.com
'
;
CC recipients, multiple separated by commas
$cc = 'test1@runoob.com, test2@runoob.com';
# Sender email
$from
=
'
test@runoob.com
'
;
# Subject
$subject
=
'
Runoob Tutorial Perl send email test
'
;
$message
=
'
This is an email sent using Perl, using the MIME::Lite module, and includes an attachment.
'
;
$msg
=
MIME::Lite
->
new
(
From
=>
$from
,
To
=>
$to
,
Cc
=>
$cc
,
Subject
=>
$subject
,
Type
=>
'
multipart/mixed
'
Attachment marker
)
;
$msg
->
attach
(
Type
=>
'
TEXT
'
,
Data
=>
$message
)
;
Specify attachment information
$msg
->
attach
(
Type
=>
'
TEXT
'
,
Path
=>
'
./runoob.txt
'
,
in the current directory
Filename
=>
'
runoob.txt
'
,
Disposition
=>
'
attachment
'
)
;
$msg
->
send
;
"
Mail sent successfully
\n
"
;
After successful execution, view the email content as shown below:

You can add multiple attachments by using multiple $msg->attach calls.