Contributed by Paav
on Jun 22, 2000 at 17:27 UTC
Q&A
> mail and news
Answer: How do I send an email with Perl (including an attachment)? contributed by lhoward There are several ways to send e-mails from perl.
My favorite for small jobs like this is Mail::Sendmail.
This module is very easy to use:
use Mail::Sendmail;
sendmail(
From => 'sender@somewhereelse.com',
To => 'recipient@somewhere.com',
Subject => 'some subject',
Message => "body of the message",
);
If you want to send a file as an attachment,
that can be easily done with MIME::Lite.
use MIME::Lite;
my $msg = MIME::Lite->new(
From => 'me@myhost.com',
To => 'you@yourhost.com',
Cc => 'some@other.com, some@more.com',
Subject => 'A message with 2 parts...',
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'TEXT',
Data => "Here's the GIF file you wanted",
);
$msg->attach(
Type => 'image/gif',
Path => 'aaa000123.gif',
Filename => 'logo.gif',
);
$msg->send;
| Answer: How do I send an email with Perl (including an attachment)? contributed by fongsaiyuk
I use Mail::Sender pretty extensively.
Here's a code snippet to get you started for sending an
email with an attachment.
(We let the user's
email client determine the helper application to execute
based on the file extension of the filename we attach to
the message.)
Mail::Sender lets you customize the MIME stuff,
but for simple single files you don't need to do that.
sub send_email
{
my $sender = new Mail::Sender({
from => $from_address,
});
$sender->OpenMultipart({
to => $to_email,
subject => $subject,
});
$sender->Body;
$sender->SendLine( $msg_body );
$sender->SendFile({
description => 'Raw Data File',
encoding => '7BIT',
file => $tempfile,
});
$sender->Close;
}
I really like this module. You should seriously consider
installing it if you find that you are doing a lot with emailing
attachments. It has saved me tons of time.
| Answer: How do I send an email with Perl (including an attachment)? contributed by electrosphere
My solution uses Mail::Send and sendmail only:
sub sendAttachment
{
my( $from, $to, $subject, $filename, $type, $data ) = @_;
my $boundary = '==' . time . '==';
# untaint stuff for mailing
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
my $msg = new Mail::Send;
$msg->to($to);
$msg->add('From',$from);
$msg->subject($subject);
$msg->add('Content-Type', qq(multipart/mixed; boundary="$boundary";
+) );
my $fh = $msg->open;
print $fh "--$boundary\n";
print $fh "Content-Disposition: attachment; filename=$filename;\n";
print $fh "Content-Type: $type; name=$filename; charset=iso-8859-1;
+\n";
print $fh "Content-Transfer-Encoding: 8bit;\n\n";
print $fh $data;
print $fh "--$boundary\n";
$fh->close;
}
Used like so:
sendAttachment(
'"Me, Here" <me@here.org>',
'user@foo.com',
'Quarterly Report - Q1 2007',
'report-q12007.csv',
'text/csv',
$data
);
| Answer: How do I send an email with Perl (including an attachment)? contributed by Anonymous Monk #!/usr/bin/perl -w
use Getopt::Long;
use IO::File;
use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail;
use strict;
use warnings;
my $cc;
my $bcc;
GetOptions( 'cc=s' => \$cc, 'bcc=s' => \$bcc, );
my( $from, $to, $subject, $msgbody_file, $attachment_file ) = @ARGV;
my $msgbody = read_file( $msgbody_file );
my $attachment_data = encode_base64( read_file( $attachment_file, 1 )
+);
my %mail = (
To => $to,
From => $from,
Subject => $subject
);
$mail{Cc} = $cc if $cc;
$mail{Bcc} = $bcc if $bcc;
my $boundary = "====" . time . "====";
$mail{'content-type'} = qq(multipart/mixed; boundary="$boundary");
$boundary = '--'.$boundary;
$mail{body} = <<END_OF_BODY;
$boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
$msgbody
$boundary
Content-Type: application/octet-stream; name="$attachment_file"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$attachment_file"
$attachment_data
$boundary--
END_OF_BODY
sendmail(%mail) or die $Mail::Sendmail::error;
print "Sendmail Log says:\n$Mail::Sendmail::log\n";
sub read_file
{
my( $filename, $binmode ) = @_;
my $fh = new IO::File;
$fh->open("< $filename")
or die "Error opening $filename for reading - $!\n";
$fh->binmode if $binmode;
local $/;
<$fh>
}
| Answer: How do I send an email with Perl (including an attachment)? contributed by Realbot
See the article
The Evolution of Perl Email Handling,
by Simon Cozens.
| Answer: How do I send an email with Perl (including an attachment)? contributed by Appy16 You can make use of MIME::Lite Module.
It can be used to send single as well as multiple attachments.
FOR MULTIPLE ATTACHMENTS :
#!/usr/bin/perl
use MIME::Lite;
$from = 'qwerty\@erweer.com';
$to = 'abcd\@efgh.com';
$Subject = 'Hello';
# Part using which the attachment is sent to an email #
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $Subject,
Type => 'multipart/Mixed',
);
$msg->attach(
Type => 'Text',
Data => "The attachment contains your file"
);
$msg->attach(
Type => 'Image/gif',
Path => "/abcd/M.gif"
);
print "Mail Sent\n";
$msg->send; # send via default
FOR SINGLE ATTACHMENTS :
#!/usr/bin/perl
use MIME::Lite;
$from = 'qwerty\@erweer.com';
$to = 'abcd\@efgh.com';
$Subject = 'Hello';
# Part using which the attachment is sent to an email #
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $Subject,
Type => 'Image/gif',
Path => "/abcd/M.gif"
);
print "Mail Sent\n";
$msg->send; # send via default
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|