简体   繁体   中英

Email::Sender how to attach file in Perl?

System: Perl 5.26.1 on Ubuntu 18.04 LTS. There is a subroutine that does email in a file called util2.pl. The subroutine is called mailadming() and has a bunch of parameters like the SMTP server info, to and from addresses, etc. util2.pl is require d into the main program.

I've been searching for about 2 hours on various sites and Google, and reading the manual on Email::Sender and I cannot seem to find how to attach a file when using Email::Sender. I am upgrading a machine and was using Mail::Sender but Mail::Sender is deprecated for Email::Sender, and docs are sparse on Email::Sender.

I could not find any info at all on file attachments in Email::Sender docs.

Since Mail::Sender is not updated anymore it would be a bad idea to continue using that. We have a lot invested in Perl so we just can't change tools at this point.

  1. Can anyone help me out please?
  2. Should I use another email module for Perl? Email::Sender seems to be updated the most often and most recent.

EDIT: This is the code I have so far:

my $email = Email::Stuffer->new(
        text_body=>$body,
        subject=>$subjparam,
        from=>$from, # or use ->from($email1, $email2...)
        transport=>Email::Sender::Transport::SMTP->new({
            host => $smtpserver,
            port => $smtpport,
            username => $smtpuser,
            password => $smtppw,
            }),
        );

The error I get right now is: Can't use string ("text_body") as a HASH ref while "strict refs" in use at /usr/local/share/perl/5.26.1/Email/Stuffer.pm line 224.

I can't just create the email and send it in one shot, I have to create the email, look for other options to add to it (like CC addresses and if I need to attach a file), etc. And code I got from Perl Maven just doesn't work.

There are two components to the process, constructing the email and sending it. Email::Sender only handles the latter, and it shows how to use Email::Simple to construct simple emails. To construct more advanced emails like those with attachments, you want the more powerful Email::MIME , and there is a wrapper of Email::MIME and Email::Sender together called Email::Stuffer , which provides easy APIs for attaching files.

use strict;
use warnings;
use Email::Stuffer;
my $mail = Email::Stuffer->to($to)->from($from)->subject($subj)->text_body($text);

$mail->attach_file('/path/to/file.txt'); # guesses MIME type
# or if you have the file data in memory
$mail->attach($contents, name => 'foo.dat', filename => 'foo.dat',
           content_type => 'application/octet-stream');

# to change the transport
$mail->transport('SMTP', host => ...);
$mail->transport(Email::Sender::Transport::SMTP->new(...));

$mail->send_or_die;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM