Perl Sending Email
If your program runs on a Linux/Unix system, you can use the sendmail tool in Perl to send emails.
Below is a simple script example for sending an email:
Example
#!/usr/bin/perl
# Recipient email, set to my QQ email, you need to change it to your own email
$to = '[email protected]';
# Sender email
$from = '[email protected]';
# Subject
$subject = 'tutorialpro.org Perl Email Test';
$message = 'This is an email sent using Perl.';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email headers
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email message
print MAIL $message;
close(MAIL);
print "Email sent successfully\n";
Executing the above program, the output is:
Email sent successfully
Under normal circumstances, the above email would be intercepted by QQ Mail. We can add it to the whitelist. The operation method can be found here: https://kf.qq.com/faq/120322fu63YV130805rYRFzu.html
After adding to the whitelist, you can receive emails normally.
Sending HTML Format Email
We can add Content-type: text/html\n to the email header to send an HTML formatted email. Here is an example:
Example
#!/usr/bin/perl
# Recipient email, set to my QQ email, you need to change it to your own email
$to = '[email protected]';
# Sender email
$from = '[email protected]';
# Subject
$subject = 'tutorialpro.org Perl Email Test';
$message = '<h1>This is an email sent using Perl<h1><p>Hello, I am from tutorialpro.org, the address is: http://www.tutorialpro.org.</p>';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email headers
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-type: text/html\n";
# Email message
print MAIL $message;
close(MAIL);
print "Email sent successfully\n";
After successful execution, the email content looks like this:
Using MIME::Lite Module
If you are using a Windows system without the sendmail tool, you can use Perl's MIME::Lite module as an email client to send emails.
The MIME::Lite module download address is: MIME-Lite-3.030.tar.gz.
Here we directly install it using cpan (requires root privileges), no need to download:
$ 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, set to my QQ email, you need to change it to your own email
$to = '[email protected]';
# Carbon copy recipients, multiple addresses separated by commas
# $cc = '[email protected], [email protected]';
# Sender email
$from = '[email protected]';
# Subject
$subject = 'tutorialpro.org Perl Email Test';
$message = 'This is an email sent using Perl, with the MIME::Lite module.';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.
Chinese: Subject => $subject,
Data => $message
);
$msg->send;
print "邮件发送成功\n";
After successful execution, check the email content as shown below:
Sending HTML Format Email
We can add Content-type: text/html\n to the email header to send an email in HTML format, as shown in the following example:
Example
#!/usr/bin/perl
use MIME::Lite;
# Recipient email, set to my QQ email, you need to modify it to your own email
$to = '[email protected]';
# Carbon copy, multiple addresses separated by commas
# $cc = '[email protected], [email protected]';
# Sender email
$from = '[email protected]';
# Subject
$subject = 'tutorialpro.org Perl Email Test';
$message = '<h1>This is an email sent using Perl<h1><p>Using the MIME::Lite module.</p><p>From tutorialpro.org, address: http://www.tutorialpro.org.</p>';
$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;
print "Email sent successfully\n";
After successful execution, check the email content as shown below:
Sending Email with Attachments
An example of sending an email with attachments is as follows:
Example
#!/usr/bin/perl
use MIME::Lite;
# Recipient email, set to my QQ email, you need to modify it to your own email
$to = '[email protected]';
# Carbon copy, multiple addresses separated by commas
# $cc = '[email protected], [email protected]';
# Sender email
$from = '[email protected]';
# Subject
$subject = 'tutorialpro.org Perl 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 flag
);
$msg->attach (
Type => 'TEXT',
Data => $message
); # Specify attachment information
$msg->attach(Type => 'TEXT',
Path => './tutorialpro.txt', # In the current directory
Filename => 'tutorialpro.txt',
Disposition => 'attachment'
);
$msg->send;
print "Email sent successfully\n";
After successful execution, check the email content as shown below:
You can add multiple attachments by using multiple $msg->attach. ```