Files
perl-examples/docs/21_sendmail.md
2025-02-10 05:29:29 +09:00

1.1 KiB

Mail

#!/usr/bin/perl

$to = 'abcd@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is a test email sent.';

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

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;

close(MAIL);

Mail::Mailer

#!/usr/bin/perl
use warnings;
use strict;

use Mail::Mailer;

my $mailer = Mail::Mailer->new('sendmail');
my %headers = (
    'To' => 'you@example.com',
    'From' => 'me@example.com',
    'Subject' => 'Sample Mail'
);
$mailer->open(\%headers);
print $mailer "This is the message.\n";
$mailer->close;
  • new

  • open

  • close

Mail::Send

#!/usr/bin/perl
use warnings;
use strict;

use Mail::Send;

my $mail = Mail::Send->new;
$mail->set('To', ('you@example.com', 'another@example.com'));
$mail->to('you@example.com');
$mail->subject('Sample Mail');
my $fh = $mail->open('sendmail');
print $fh "This is the message.\n";
$fh->close or die $!;
  • new

  • open

  • add

  • delete

  • set

  • to

  • cc

  • bcc

  • subject