Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Perl MIME date format help

Posted on 2010-09-21
12
776 Views
Last Modified: 2012-05-10
Hi experts,

I'm writing a Perl script to run on a Linux box which needs to send emails. I'm using Net::SMTP to send the mail, with a subroutine adding the headers etc. being called from the body of the script.

I'm struggling to work out how to generate an RFC2183 date for use in the MIME headers - the top half of the SMTP subroutine is attached.

   
sub send_mail # send SMTP mail
	{
        my ($from, $to_addr, $subject, $body, $msg, $date);

        $from       = shift;
        $to_addr    = shift;
        $subject	= shift;
        $body       = shift;
        
        $msg = "MIME-Version: 1.0\n"
             . "From: $from\n"
             . "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
             . "Date: " . date_r() . "\n"
             . "Subject: $subject\n\n"      # Double \n
             . $body;

<snip!>

Open in new window


The format of the time string that I need for the MIME header is: Wed, 12 Feb 1997 16:29:51 -0500

I would like to know what commands will generate this format of date, and which modules I need to include to use them.

Thanks!

George
0
Comment
Question by:georgemason
  • 6
  • 4
12 Comments
 
LVL 8

Expert Comment

by:nognew
ID: 33726416
Hi there!
Please see the script below:
Cheers,
t.
#!/usr/bin/env perl

use POSIX;

print strftime("%a, %d %b %y %H:%M:%S %z \n",localtime);

Open in new window

0
 
LVL 1

Author Comment

by:georgemason
ID: 33726769
Hi, thanks for that - I get the following output:

Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at ./ftp_create.perl line 52, <FILE> line 35.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33727086
I have done some more testing, and in my script, strftime works if passed gmtime but not localtime - but obviously it then doesn't take account of daylight savings.

I've run it in a script on its own and it runs fine as shown above

I'm confused :-/
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 8

Expert Comment

by:nognew
ID: 33727095
May I see your ftp_create.perl around line 52 please?
I have a feeling something amiss there.
Cheers,
t.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33734924
I've made some changes to the script, so now the problem is at line 63. See below for that section of code.


sub send_mail # send SMTP mail
	{
        my ($from, $to_addr, $subject, $body, $msg);

        $from       = shift;
        $to_addr    = shift;
        $subject	= shift;
        $body       = shift;
		$date 		= strftime "%a, %d %b %y %H:%M:%S %z",localtime;
        
        $msg = "MIME-Version: 1.0\n"
             . "From: $from\n"
             . "To: " . ( ref($to_addr) ? join(';', @$to_addr) : $to_addr ) . "\n"
             . "Date: $date\n"
             . "Subject: $subject\n\n"      # Double \n
             . $body;

Open in new window

0
 
LVL 8

Expert Comment

by:nognew
ID: 33735595
Could you please pin point which line on the given snip produce the error and the error message?
Cheers,
t.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33736060
Sure, no problem. I get the following error when the end of line 9 is set to localtime rather than gmtime:

Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at ./ftp_create.perl line 62, <FILE> line 205.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33736066
(sorry, line 62 in the error relates to line 9 in the snippet)
0
 
LVL 8

Accepted Solution

by:
nognew earned 250 total points
ID: 33736090
Are you sure you have
use POSIX;
in the top of you file?
Which Perl version you are on?
Cheers,
t.
0
 
LVL 1

Author Comment

by:georgemason
ID: 33737391
I had use "POSIX qw(strftime);" but I added "use POSIX;" too,  and then the script still didn't work, but I also got this warning:

Prototype mismatch: sub main::ctime (;$) vs none at /usr/lib/perl5/5.8.8/Exporter.pm line 65.
 at /usr/lib/perl5/5.8.8/i386-linux-thread-multi/POSIX.pm line 19

I'm using Perl5.
0
 
LVL 9

Expert Comment

by:Suhas .
ID: 37313515
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

808 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