[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


perl script to send email with attachments

Posted on 2007-07-31
Medium Priority
Last Modified: 2012-08-13
I have a folder of .pdf files that are created everyday

I need a script that will send an email with these files as attachments.

C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\BidAskSpreads.pdf
C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\IntradayTrades.pdf
C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\TradesByTypeForPeriod.pdf

I want the script to check if files are new, ie modified today, and if so send the email.

The subject should be "TradesFor$YesterdayDate"
Question by:mcgilljd
LVL 24

Accepted Solution

mankowitz earned 1200 total points
ID: 19601599
Adapted from http://www.akadia.com/services/email_attachments_using_perl.html


# Akadia AG, Arvenweg 4, CH-3604 Thun                 send_attachment.pl
# ----------------------------------------------------------------------
# File:       send_attachment.pl
# Autor:      Martin Zahn / 05.01.2003
# Purpose:    Email attachments in Perl
# Location:   $ORACLE_HOME\Database
# Certified:  Perl 5.6.1, MIME-Lite-2.117 on Cygwin / Windows 2000
# ----------------------------------------------------------------------

use MIME::Lite;
use Net::SMTP;

### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'martin.zahn@akadia.com';
my $to_address = 'martin.zahn@akadia.com';
my $mail_host = 'mailhost.domain.com';

### Adjust subject and body message
my $subject = "TradesFor$YesterdayDate";
my $message_body = "....... whatever you want here.......";

### Adjust the filenames
my @attachments = ("C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\BidAskSpreads.pdf", "C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\IntradayTrades.pdf", "C:\Documents and Settings\jMcgill\Desktop\Reports\TradeReports\TradesByTypeForPeriod.pdf");

### Create the multipart container
$msg = MIME::Lite->new (
  From => $from_address,
  To => $to_address,
  Subject => $subject,
  Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text message part
$msg->attach (
  Type => 'TEXT',
  Data => $message_body
) or die "Error adding the text message part: $!\n";

### Add the PDFs
for each $fn (@attachments) {
$msg->attach (
   Type => 'application/pdf',
   Path => $fn,
   Filename => $fn,
   Disposition => 'attachment'
) or die "Error adding $fn: $!\n"; }

### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);

Author Comment

ID: 19602070
C:\>Perl  C:\PerlScripts\SendMail.pl
syntax error at C:\PerlScripts\SendMail.pl line 49, near "for each"
Execution of C:\PerlScripts\SendMail.pl aborted due to compilation errors.

Author Comment

ID: 19602317
I am getting access denied.
I use outlook on exchange server.
LVL 15

Assisted Solution

markpalinux earned 400 total points
ID: 19605795
If you are getting access denied from your exchange server be sure to check out this page, you can add the ip address of the machine running the perl script to allow exchange to accept the relay. also you may want to use real email addresses.

Controlling SMTP virtual server relaying

hope this helps,
LVL 17

Assisted Solution

mjcoyne earned 400 total points
ID: 19605981
for each $fn (@attachments) {

should be:

foreach $fn (@attachments) {

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question