How to send mail through gmail with perl?

Member

by samara , in category: Third Party Scripts , 17 days ago

How to send mail through gmail with perl?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 16 days ago

@samara 

You can send an email through Gmail using Perl by utilizing the Net::SMTP::TLS::ButMaintained module. Here is an example code snippet to achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use Net::SMTP::TLS;
use MIME::Base64;

# Set Gmail account credentials
my $smtp_server = 'smtp.gmail.com';
my $smtp_user = '[email protected]';
my $smtp_password = 'yourpassword';

# Create SMTP object
my $smtp = Net::SMTP::TLS->new(
    $smtp_server,
    Hello   => 'smtp.gmail.com',
    Port    => 587,
    User    => $smtp_user,
    Password => $smtp_password,
);

# Set email content
my $from = '[email protected]';
my $to = '[email protected]';
my $subject = 'Test Email';
my $message = 'This is a test email sent using Perl and Gmail.';

# Compose email headers
my $header = "To: $to
";
$header .= "From: $from
";
$header .= "Subject: $subject
";
$header .= "Content-Type: text/plain; charset=UTF-8
";
$header .= "
";
$header .= $message;

# Send email
$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend($header);
$smtp->dataend();
$smtp->quit;

print "Email sent successfully
";


Make sure to replace '[email protected]', 'yourpassword', and '[email protected]' with your actual Gmail account credentials and recipient email address. This code will send an email with the specified content using your Gmail account through the SMTP server.