Perl script for sending Gmail messages with file attachments

I’m working on a Perl script that needs to send emails through Gmail’s SMTP server and include file attachments. I’ve been trying different approaches but can’t get it working properly.

Here’s my current attempt:

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

my $smtp_client = Net::SMTP::SSL->new(
    'smtp.gmail.com',
    Port => 465,
    Debug => 1
);

$smtp_client->auth('[email protected]', 'mypassword');

my $message = MIME::Lite->new(
    From => '[email protected]',
    To => $recipient_email,
    Subject => 'Log report for channel ' . $channel_id,
    Type => 'multipart/mixed'
);

my @log_data = slurp_file($log_path);
$message->attach(
    Type => 'TEXT',
    Data => join('', @log_data)
);

The connection seems to work but I’m having trouble with the attachment part. Has anyone successfully sent Gmail messages with attachments using Perl? What modules and configuration work best for this?

This happens all the time with that approach. Net::SMTP::SSL and MIME::Lite don’t play nice together - the attachment encoding gets messed up during transmission. I wasted weeks debugging this exact issue before I figured out that MIME::Lite->send(‘smtp’) completely ignores your custom SMTP connection. What fixed it for me: let MIME::Lite handle everything. Build your message object, then call send with the SMTP parameters passed right to it. The module handles both the connection and MIME encoding properly. One more thing - check your Gmail settings. You need two-factor auth turned on and an app password generated. Regular passwords don’t work with SMTP anymore. Turn on Debug and it’ll show you if authentication is failing.

The Problem:

You’re having trouble sending email attachments with your Perl script using Net::SMTP::SSL and MIME::Lite. The connection to Gmail’s SMTP server appears to be working, but the attachment isn’t being sent correctly.

:gear: Step-by-Step Guide:

  1. Switch to Email::Sender::Simple: The most effective solution is to replace your current approach using Net::SMTP::SSL and MIME::Lite with the simpler Email::Sender::Simple module. This module simplifies the process of sending emails with attachments and handles MIME encoding more reliably, resolving common compatibility issues between Net::SMTP::SSL and MIME::Lite.

    Install the module if you haven’t already:

    cpan Email::Sender::Simple
    

    or:

    sudo apt-get install libmail-sender-simple-perl
    
  2. Rewrite Your Script: Here’s how to rewrite your script using Email::Sender::Simple and Email::MIME:

    use Email::Sender::Simple;
    use Email::MIME;
    
    my $sender = Email::Sender::Simple->new(
        smtp => {
            host     => 'smtp.gmail.com',
            port     => 587, # Use port 587 for TLS
            auth     => {
                user     => '[email protected]',
                password => 'myAppPassword', # Use an App Password, NOT your regular Gmail password.
            },
            tls      => 1, # Enable TLS
        }
    );
    
    my $msg = Email::MIME->create(
        from    => '[email protected]',
        to      => $recipient_email,
        subject => 'Log report for channel ' . $channel_id,
        body    => 'Attached are your logs.', #Optional body text.
    );
    
    my @log_data = slurp_file($log_path);
    $msg->attach(
        type    => 'text/plain', #Specify text/plain for plain text files, or another MIME type as appropriate for other files
        data    => join('', @log_data),
        filename => 'log_file.txt', #Adding filename here helps the recipient know what they're opening
    );
    
    
    $sender->send($msg);
    
  3. Generate an App Password: Gmail requires you to use an App Password instead of your regular password for SMTP connections. Go to your Google account security settings, find the “App Passwords” section, and generate a new password specifically for your Perl script. Use this App Password in the script’s password setting.

:mag: Common Pitfalls & What to Check Next:

  • App Password: Double-check you are using the App Password generated in your Google account, not your regular Gmail password. Gmail blocks regular passwords for security reasons.
  • MIME Type: Ensure the type in the attach method correctly reflects the content type of your attachment. For example, use 'application/pdf' for PDFs, 'text/csv' for CSV files, and so on. Incorrect MIME types can lead to problems opening or viewing attachments.
  • File Path: Make absolutely sure that the $log_path in your slurp_file function points to the correct location of your log file. A wrong path will result in an empty attachment or an error.
  • Debug Mode: Add $sender->debug(1); before the $sender->send($msg); line in the script to enable debugging output. This output can be very helpful in troubleshooting email delivery issues. It will tell you exactly where the process is failing.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

I used to fight with Perl email scripts constantly. MIME::Lite gets messy fast, especially with different file types or multiple recipients.

Your real problem is mixing Net::SMTP::SSL with MIME::Lite - attachment handling becomes a pain. And debugging email delivery in Perl? Total nightmare.

I ditched custom scripts and automated the whole workflow instead. Now I have a system that watches for files, processes them, and sends through Gmail automatically.

It handles SMTP auth, file type detection, and attachment formatting without any Perl code. Logs everything too, so you know exactly why emails fail.

Just trigger it from your existing Perl script with an HTTP call or file drop. Way cleaner than maintaining email code.

Check out Latenode for this: https://latenode.com

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.