2

I’m trying to use Perl to send an email message. Basically I have a Perl script that prints out a report in a nice format. I want that report to be sent via email. How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1443144
  • 97
  • 1
  • 2
  • 6
  • [What is the best Perl module for sending email?](http://stackoverflow.com/questions/338896/what-is-the-best-perl-module-for-sending-email), [Which package from CPAN should I use to send mail?](http://stackoverflow.com/questions/2919493/which-package-from-cpan-should-i-use-to-send-mail) – daxim Jun 07 '12 at 21:38
  • CPAN is your friend, new or old to Perl. Try [Email::Sender](https://metacpan.org/module/Email::Sender), for instance. Nice documentation in the [quickstart manual](https://metacpan.org/module/Email::Sender::Manual::QuickStart). – asjo Jun 07 '12 at 20:28
  • Use Perl SMTP http://perldoc.perl.org/Net/SMTP.html – Charles Ma Jun 07 '12 at 20:25

4 Answers4

4

If the machine does not have sendmail configured, I typically use Mail::Sendmail

use Mail::Sendmail;

%mail = (smtp    => 'my.isp.com:25',
         to      => 'foo@example.com',
         from    => 'bar@example.com',
         subject => 'Automatic greetings',
         message => 'Hello there');

sendmail(%mail) or die;
Pontus
  • 1,639
  • 2
  • 11
  • 6
3

MIME::Lite is a strong module used by many. It's easy to use, including if you want to attach documents.

use MIME::Lite;
my $msg = MIME::Lite->new(
    From    => $from,
    To      => $to,
    Subject => $subject,
    Type    => 'text/plain',
    Data    => $message,
);
$msg->send;

Since it uses sendmail by default (as opposed to SMTP), you don't even need to configure it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 6
    I know this answer is old but the MIME::Lite docs now say: `WAIT! MIME::Lite is not recommended by its current maintainer. There are a number of alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you should probably use instead. MIME::Lite continues to accrue weird bug reports, and it is not receiving a large amount of refactoring due to the availability of better alternatives. Please consider using something else.` – ThisSuitIsBlackNot Sep 30 '13 at 19:18
3

It's worth mentioning that if you happen to have Outlook on your machine and cpan the Outlook module:

 # create the object
 use Mail::Outlook;
 my $outlook = new Mail::Outlook();

  # start with a folder
  my $outlook = new Mail::Outlook('Inbox');

  # use the Win32::OLE::Const definitions
  use Mail::Outlook;
  use Win32::OLE::Const 'Microsoft Outlook';
  my $outlook = new Mail::Outlook(olInbox);

  # get/set the current folder
  my $folder = $outlook->folder();
  my $folder = $outlook->folder('Inbox');

  # get the first/last/next/previous message
  my $message = $folder->first();
  $message = $folder->next();
  $message = $folder->last();
  $message = $folder->previous();

 # read the attributes of the current message
 my $text = $message->From();
 $text = $message->To();
 $text = $message->Cc();
 $text = $message->Bcc();
 $text = $message->Subject();
 $text = $message->Body();
  my @list = $message->Attach();

  # use Outlook to display the current message
  $message->display;


  # Or use a hash
  my %hash = (
    To      => 'suanna@live.com.invalid',
    Subject => 'Blah Blah Blah',
     Body    => 'Yadda Yadda Yadda',
  );

  my $message = $outlook->create(%hash);
  $message->display(%hash);
  $message->send(%hash);

Note that the .invalid TLD is not real, so the address above will not deliver. In any case, I've put here a decent explanation of things in the module - this sends a message!

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
  • @Grep Bacon I'm tried this code, But the error is `Can't call method "From" on an undefined value at mail.pl line 24.` In this code. What can i do? – mkHun Nov 23 '14 at 16:06
  • @Hussain Don't run this code exactly as is - this is just a CPAN piece showing all the methods you can do. Previous to line 24 the code it runs first(), next(), last() AND previous(). You shouldn't run all of those back-to-back... might be it tries to access something with no From(). Also, before all that it selects the current folder twice... might as well clean it up to JUST grab 'Inbox'. – PinkElephantsOnParade Nov 23 '14 at 16:22
  • Hi, I encountered a problem with this, it seems that the `$outlook = new Mail::Outlook()` returns undef... why is that? i already installed the module and I have outlook 2007... how can I create the $outlook object? – catzilla Jun 25 '15 at 02:55
1

Simplest way without CPAN libraries:

#!/usr/bin/perl

$to = 'toAddress@xx.com';       # to address
$from = 'fromAddress@xx.com';   # from address
$subject = 'subject';           # email subject
$body = 'Email message content';# message

open(MAIL, "|/usr/sbin/sendmail -t");     
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL $body;    
close(MAIL);

print "Email Sent Successfully to $to\n";
Sandeep
  • 51
  • 3