perl script to send email with attachments

Posted on 2007-07-31
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

    Adapted from


    # Akadia AG, Arvenweg 4, CH-3604 Thun       
    # ----------------------------------------------------------------------
    # File:
    # 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 = '';
    my $to_address = '';
    my $mail_host = '';

    ### 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

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

    Author Comment

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

    Assisted Solution

    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

    for each $fn (@attachments) {

    should be:

    foreach $fn (@attachments) {

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now