Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: using Email to Perl to append to a changelog.txt, html, or .xls?

by hsinclai (Deacon)
on Nov 24, 2004 at 06:03 UTC ( #410057=note: print w/replies, xml ) Need Help??

in reply to using Email to Perl to append to a changelog.txt, html, or .xls?

I believe I am reading your OP correctly and your intention, so, here is a mail responder code I wrote (which you might/could use as a template, altering it of course for your specific needs).

To use it, you must control the receiving email server. Invent an aliased user name to which any changes should be emailed, and map the script name in the /etc/mail/aliases file so that mail sent to this new alias goes to the script:
aliasname: "|/path/to/"

The script will receive the entire mail, then respond to the sender, with the short message of your choosing, bcc'ing you if so desired, but also save the entire email into a disk textfile, using a unique name (in your case you would change that filename to incorporate the sender's name - should be easy using a variable already there from the headers capture).

Now that you've preserved the email on disk (and also in an array in the script) you can do any number of things like separate the body out and send it onward to .txt, .html, or .XLS. IMO that is a separate task and should be treated as such unless you're doing a quick one off.

Sorry I would have altered the script more to your needs but really there are just too many variables not knowing your exact circumstances.

But hopefully this demonstrates that it is easy to capture the email and do what you want with it. The hardest part is going to be to get the users to send the email in the first place. Here's the code:
use strict; ##------------- Global my @email_message; my %headerz; my $new_recip; my $orig_recip; my $linelimit = 234; my $linecount = 0; ##------------- Edit these my $bcc = ''; my $replyto = ''; my $from_address = 'your AutoResponse <> +'; my $datestring = &datestring; my $saved_file_name = "/path/mailmsg-$datestring.txt"; ##------------- main &save_mail; &header_mgr; #-------------> Edit these too <------------- my $subject = "Your submission to $orig_recip"; my $bodytext = qq|\n your message body to send as autoreply\n + |; #-------------------------------------------- &dispatch or die "email dispatch failed: $!"; ##------------- end ##--------------- SUBS ### mail collector sub save_mail { while ( <STDIN> ) { chomp; push @email_message, $_; $linecount++; } if ( $linecount <= $linelimit) { open SAVEDMAIL, '>', $saved_file_name or warn("could not open +output file $!"); print SAVEDMAIL $_,"\n" for @email_message; close SAVEDMAIL; } return @email_message; } ### Timestamp for disk file sub datestring { my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time); my $timeformat = sprintf( "%02d%02d%04d-%02d_%02d_%02d", $mon+1,$mday,$year+1900,$hour,$min,$sec ); return $timeformat; } ### Collect original headers into a hash sub header_mgr { foreach ( @email_message ) { /(^From: )(.*$)/ ? $headerz{from_address} = "$2" : 1; /(To: )(.*$)/ ? $headerz{orig_recip} = "$2" : 1; /(^Reply-To: )(.*$)/ ? $headerz{replyto} = "$2" : 1; /(^cc: )(.*$)/i ? $headerz{cc} = "$2" : 1; /(^Date: )(.*$)/ ? $headerz{datestamp} = "$2" : 1; /(^Subject: )(.*$)/ ? $headerz{subject} = "$2" : 1; } ( defined $headerz{'reply_to'} ) ? $new_recip = $headerz{'replyto'} : $new_recip = $headerz{'from_addr +ess'}; ( defined $headerz{'orig_recip'} ) ? $orig_recip = $headerz{'orig_r +ecip'} : 1; } ##Mail sending routine sub dispatch { open(MAILPIPE,"|/usr/sbin/sendmail -t"); print MAILPIPE "To: $new_recip\n"; print MAILPIPE "From: $from_address\n"; print MAILPIPE "Bcc: $bcc\n"; print MAILPIPE "Subject: $subject\n"; print MAILPIPE "\n"; print MAILPIPE "$bodytext\n"; print MAILPIPE "\n-----------------------------------\n\n"; print MAILPIPE "Original message of $linecount lines, was sent +to $orig_recip.\n"; print MAILPIPE "\n-----------------------------------\n\n"; close(MAILPIPE); } __END__

Forgetting any Perl code momentarily, I'd like to mention that if you're trying to do any change control or tracking, this sure is a hard way to do it. Even if your team sends the requisite email, they surely won't ever be including the level of detail you're envisioning, or they might not even send the email until significantly later. One thing though, if you send an email, it is nice to get an automated response confirming receipt and processing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410057]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-06-23 07:47 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (117 votes). Check out past polls.