Perl で、
open(SEND, '/usr/sbin/sendmail') || die;
などとせずにモジュールを使ってメールを送信するには、以下のようにする。
使用するモジュール
use Net::SMTP;
use MIME::Entity;
#!/usr/bin/perl
use Net::SMTP;
use MIME::Entity;
# Settings
our $SENDER = '送信元のメールアドレス';
our $RECIPIENT = '送信先のメールアドレス';
our $SUBJECT = 'タイトル';
our $ATTACHED_FILE_1 = '/home/youraccount/添付ファイル名その1.jpg';
our $ATTACHED_FILE_2 = '/home/youraccount/添付ファイル名その2.jpg';
# Create object
my $smtp=Net::SMTP->new('localhost',
HELLO=>'yourmailsever.yourdomain.com');
# Built headers
$smtp->mail($SENDER); # Sender
$smtp->to ($RECIPIENT); # Receiver
# Built Data (Create data by MIME::Entity)
$smtp->data();
my $mime = MIME::Entity->build(
From => $SENDER , # Sender (data)
To => $RECIPIENT, # Receiver (data)
Subject => $SUBJECT , # Subject
Data => ['']); # body
# Attached file
$mime->attach(
Path => $ATTACHED_FILE_1,
Type => 'image/jpeg',
Encoding => 'Base64'
);
# Attached file
$mime->attach(
Path => $ATTACHED_FILE_2,
Type => 'image/jpeg',
Encoding => 'Base64'
);
# Attached file (Text)
# $mime->attach(
# Path => $ATTACHED_FILE,
# Type => 'text/plain',
# Encoding => '-SUGGEST'
#);
$smtp->datasend($mime->stringify); # transfer strings
# Data termination and send mail
$smtp->dataend();
#Quit SMTP connection
$smtp->quit;
# for debug
print "Sender : $SENDER\n";
print "Recipient : $RECIPIENT\n";
print "Attached : $ATTACHED_FILE\n";
1;
トラックバック URL:
https://perltips.twinkle.cc/trackback/52
from Perl Tips on 2008/01/21(月) 15:31