0

When i tried to send email with attachments using MIME::Lite module in Centos.

The Email sent successfully but sent date is not displaying in rackspace

See my example code below:

#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;

$to = 'xxx@yyy.net';
$cc = 'test@gmail.com';
$from = 'DailyReports@test.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';

$msg = MIME::Lite->new(
    From     => $from,
    To       => $to,
    Cc       => $cc,
    Subject  => $subject,
    Type     => 'multipart/mixed'
    )or die "Error creating multipart container: $!\n";

# Add your text message.
$msg->attach(Type         => 'text',
    Data         => $message
);

# Specify your file as attachement.
$msg->attach(Type        => 'application/openoffice',
    Path        => '/tmp/Daily_Transactions_May_2014.xls',
    Filename    => 'Daily_Transactions_May_2014.xls',
    Disposition => 'attachment'
    );
#MIME::Lite->send('Mail', "/usr/sbin/sendmail);
open (SENDMAIL, "| /usr/lib/sendmail -t");
$msg->print(\*SENDMAIL);
close(SENDMAIL);

#$msg->send;
$str = $msg->as_string;
print "$str";
print "Email Sent Successfully\n";
secretformula
  • 6,414
  • 3
  • 33
  • 56
  • What do you mean by "not displaying in Rackspace"? The message gets sent but lacks a `Date:` header? – tripleee May 28 '14 at 20:53
  • I used to view emails in my rackspace email account. when i view my automated email using above script sent date is not displaying but i can receive email with'from','to','subject' and attachement – user3685157 May 28 '14 at 22:39
  • Note that MIME::Lite has been deprecated in favour of newer modules. https://metacpan.org/pod/MIME::Lite#WAIT – Kaoru Jun 02 '14 at 20:14

1 Answers1

1

To add a valid Date: header, add something like

use DateTime::Format::Mail;
my $date = DateTime::Format::Mail->format_datetime( DateTime->now() );

:

$msg = MIME::Lite->new(
    From     => $from,
    To       => $to,
    Cc       => $cc,
    Date     => $date,
    Subject  => $subject,
    Type     => 'multipart/mixed'
    ) or die "Error creating multipart container: $!\n";

See also How do I elegantly print the date in RFC822 format in Perl?

Granted, many MTAs will add this header if it is missing from a message in transport, but it should hardly come as a surprise that if you send a message to yourself without this header, the message you receive lacks it.

tripleee
  • 175,061
  • 34
  • 275
  • 318