This is a discussion on Sending Mail from Script: Setting Header within the Linux General forums, part of the Linux Forums category; I have a zsh Script which dynacmically sends out a status mail. The mail sent out contains Cc, Bcc and ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have a zsh Script which dynacmically sends out a status mail.
The mail sent out contains Cc, Bcc and Reply-To headers and occasionally attachment. Right now I'm using mutt for this task, because it is very convenient to specify this information on the mutt command line, i.e. REPLYTO=rtaddr mutt -b bccaddr -c ccaddr -a attachment -s subject toaddr <body where body is the file containing a mailbody. This works very well so far. Now I would like to extend the application in that the email being sent also contains custom headers (such as X-Priority). There is no option where I can tell mutt to put in additional headers. This *can* be done, but both possibilities are a little bit complicated: One way to do it would be to prepare the Email so that it already contains *all* headers in addition to the mailbody, and then use mutt -H ... to send the mail. The other possibility is to generate dynamically a mutt resource control file, in which the custom headers are placed, and then use mutt's -F switch to load this file. So before I try one of these, I would like to ask what approach other users use to easily send mail using attachments from a script. A solution working on shell level would be preferable, but different approaches in Perl, Ruby, Tcl or Python would also be interesting. Ronald -- Ronald Fischer <ronaldf@eml.cc> Posted via http://www.newsoffice.de/ |
|
|||
|
On Fri, 20 Oct 2006 11:23:26 +0200, Ronald Fischer wrote:
> > So before I try one of these, I would like to ask what approach other > users use to easily send mail using attachments from a script. A > solution > working on shell level would be preferable, but different approaches > in Perl, Ruby, Tcl or Python would also be interesting. I used perl because of the different files I send to people in my humor list. perl snippet follows: (indention dinked up to fit usenet rules) if ($_lc_fn =~ /\.txt/i) { $_type = 'text/plain' ; $_encode = 0 ; last SWITCH ; } if ($_lc_fn =~ /\.swf/i) { $_type = 'application/octet-stream' ; $_encode = 1 ; last SWITCH ; } if ($_lc_fn =~ /\.eml/i) { $_type = 'multipart/related' ; $_en code = 0 ; last SWITCH ; } if ($_lc_fn =~ /\.wmv/i) { $_type = 'video/x-ms-asf' ; $_encod e = 1 ; last SWITCH ; } if ($_lc_fn =~ /\.zip/i) { $_type = 'application/x-zip-compres sed' ; $_encode = 1 ; last SWITCH ; } if ($_type eq "unknown" ) { printf "unknown sufix file type on $_fn_to_send\n" ; exit 1 ; } if ( $_encode == 0) { $msg = MIME::Lite->new( From =>"$USER\@$HOSTNAME", To =>$_to, Subject =>"$_subject $_type bytes=$size", Type =>$_type, Path =>$_fn_to_send ); } else { $msg = MIME::Lite->new( From =>"$USER\@$HOSTNAME", To =>$_to, Subject =>"$_subject $_type bytes=$size", Type =>$_type, Encoding =>'base64', Path =>$_fn_to_send ); } open MAIL, "|/usr/sbin/sendmail.postfix -t -oi -oem" or die "Error on mailopen is: $!\n"; $msg->print(\*MAIL); close MAIL; |
|
|||
|
I also use perl, but with the CPAN module Mailtool.
Make is very easy to send mail with attachements Zoot Ronald Fischer wrote: > I have a zsh Script which dynacmically sends out a status mail. > The mail sent out contains Cc, Bcc and Reply-To headers and > occasionally attachment. > > Right now I'm using mutt for this task, because it is very > convenient to specify this information on the mutt command line, > i.e. > > REPLYTO=rtaddr mutt -b bccaddr -c ccaddr -a attachment -s subject toaddr > <body > > where body is the file containing a mailbody. This works very well so > far. > > Now I would like to extend the application in that the email being sent > also contains custom headers (such as X-Priority). There is no option > where I can tell mutt to put in additional headers. > > This *can* be done, but both possibilities are a little bit complicated: > > One way to do it would be to prepare the Email so that it already > contains *all* headers in addition to the mailbody, and then use > > mutt -H ... > > to send the mail. The other possibility is to generate dynamically > a mutt resource control file, in which the custom headers are placed, > and then use mutt's -F switch to load this file. > > So before I try one of these, I would like to ask what approach other > users use to easily send mail using attachments from a script. A > solution > working on shell level would be preferable, but different approaches > in Perl, Ruby, Tcl or Python would also be interesting. > > Ronald > > > -- > Ronald Fischer <ronaldf@eml.cc> > Posted via http://www.newsoffice.de/ |
|
|||
|
Ronald Fischer <ronaldf@eml.cc>:
> [snip] > So before I try one of these, I would like to ask what approach other > users use to easily send mail using attachments from a script. A Two, both in perl: ---------------------- #!/usr/bin/perl -w # # report.pl # # from Spamcop reporter.pl by jim@$SOMEWHERE.net - 13Jan2002 # use strict; open(SENDMAIL, "| /usr/sbin/sendmail -oi -t") || die qq(Cannot open sendmail output: $!\n); print SENDMAIL <<EoF; From: keeling\@spots.ab.ca To: submit.$MYID\@spam.spamcop.net Subject: SPAM/UCE report MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="WowAreSpammersEverStupid" This is a multi-part message in MIME format. --WowAreSpammersEverStupid Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit --WowAreSpammersEverStupid Content-Type: message/rfc822 Content-Disposition: attachment EoF while (<STDIN>) { print SENDMAIL; } print SENDMAIL <<EoF; --WowAreSpammersEverStupid-- EoF close (SENDMAIL); ---------------------- ---------------------- #!/usr/bin/perl # # calls ~mx_urban/sh/err_log.sh to grep /var/log/httpd/error_log # for yesterday's log file events, then mails them out in a gzipped, # Mime encoded attachment. # use strict; use warnings; use diagnostics; use MIME::Lite; sub usage() { print qq($0: usage: \"$0 [user\@somewhere.com] [somewhere\@else.com]\"\n); } my ($from, $recip); # who to send it from. # if( defined( $ARGV[ 1 ] ) ) { if( $ARGV[ 1 ] =~ qq(\@) ) { $from = qq($ARGV[ 1 ]); } else { usage(); exit 0; } } else { $from = qq($ENV{USER}); } # who to send it to. # if( defined( $ARGV[ 2 ] ) ) { if( $ARGV[ 2 ] =~ qq(\@) ) { $recip = qq($ARGV[ 2 ]); } else { usage(); exit 0; } } else { $recip = qq(stephen.keeling\@blah.com); } # string to search for in error_log # my $day = qx(/bin/date '+%a %b %d' --date=yesterday); chomp( $day ); # date prefix for output filename. # my $fday = qx(/bin/date '+%Y%m%d' --date=yesterday); chomp( $fday ); # output filename (eg. 20050614_error_log.gz) # my $fname = $fday . qq(_error_log.gz); # see EOF for err_log.sh # my $msg = new MIME::Lite; $msg->build(Type => 'gzip', Path => "/home/mx_urban/sh/err_log.sh |", ReadNow => 1, Filename => "$fname"); $msg->add(From => $from); $msg->add(To => $recip); $msg->add(Subject => "Apache error log file snippet - $fname"); $msg->send(); __END__ -------------------------------------------------------------------- #!/bin/bash # # err_log.sh # # report yesterday's apache error_log entries. this is called # by a pipe in mmail.pl # DAY="$(/bin/date '+%a %b %d' --date=yesterday)" ALOG=/var/log/httpd/error_log if [ -f "${ALOG}" ]; then /bin/grep "${DAY}" ${ALOG} | gzip -9 fi -------------------------------------------------------------------- ---------------------- -- Any technology distinguishable from magic is insufficiently advanced. (*) http://www.spots.ab.ca/~keeling Linux Counter #80292 - - http://www.faqs.org/rfcs/rfc1855.html Please, don't Cc: me. Spammers! http://www.spots.ab.ca/~keeling/emails.html |
|
|||
|
Ronald Fischer <ronaldf@eml.cc> wrote:
> I have a zsh Script which dynacmically sends out a status mail. > The mail sent out contains Cc, Bcc and Reply-To headers and > occasionally attachment. > > Right now I'm using mutt for this task, because it is very > convenient to specify this information on the mutt command line, > i.e. > > REPLYTO=rtaddr mutt -b bccaddr -c ccaddr -a attachment -s subject toaddr > <body > > where body is the file containing a mailbody. This works very well so > far. > > Now I would like to extend the application in that the email being sent > also contains custom headers (such as X-Priority). There is no option > where I can tell mutt to put in additional headers. > > This *can* be done, but both possibilities are a little bit complicated: > > One way to do it would be to prepare the Email so that it already > contains *all* headers in addition to the mailbody, and then use > > mutt -H ... > > to send the mail. The other possibility is to generate dynamically > a mutt resource control file, in which the custom headers are placed, > and then use mutt's -F switch to load this file. > > So before I try one of these, I would like to ask what approach other > users use to easily send mail using attachments from a script. A > solution > working on shell level would be preferable, but different approaches > in Perl, Ruby, Tcl or Python would also be interesting. > > Ronald > > Try mailx. I use it on my scripts to send automated reports. I don't know if it can send custom header. For sending mail with custom headers, I modified a phpmailer class for php to do that , maybe it can be used in cgi mode on a shell script. I can post it if you think it could be useful. Mauricio Asenjo |
|
|||
|
Mauricio Asenjo wrote:
> > For sending mail with custom headers, I modified a phpmailer > class for php to do that , maybe it can be used in cgi mode > on a shell script. I don't think the OP would want to run a PHP script in CGI mode since PHP can be used directly for scripting. On Debian systems it's in the php4-cli and/or php5-cli packages (other distros may have similar packages). If building PHP from scratch, simply pass --enable-cli to the configure script. For more info on command-line PHP, see: http://www.php.net/manual/en/features.commandline.php Moreover, with PHP there's no need for a modified phpmailer class to send extra headers. The prototype for the PHP mail() function is: bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] ) Notice that the optional fourth parameter allows one to easily set extra headers. http://www.php.net/manual/en/function.mail.php Depending on the OP's needs, he could either write a simple PHP script to just do the mailing or redo the entire existing shell script in PHP. |
|
|||
|
On comp.os.linux.misc, in <1161336206.49@user.newsoffice.de>,
"Ronald Fischer" wrote: Hi Ron, > I have a zsh Script which dynacmically sends out a status mail. > The mail sent out contains Cc, Bcc and Reply-To headers and > occasionally attachment. > > Right now I'm using mutt for this task, because it is very > convenient to specify this information on the mutt command > line, i.e. > > REPLYTO=rtaddr mutt -b bccaddr -c ccaddr -a attachment -s >subject toaddr <body > > where body is the file containing a mailbody. This works very > well so far. > > Now I would like to extend the application in that the > email being sent also contains custom headers (such as > X-Priority). There is no option where I can tell mutt to put in > additional headers. > Oh yes there is! mutt foo@foo.net -s "foo" -e "my_hdr X-Priority: 23" <snip> Alan -- http://home.earthlink.net/~alanconnor/contact.html http://home.earthlink.net/~alanconno...val/index.html http://home.earthlink.net/~alanconno...nix/index.html |
|
|||
|
Mauricio Asenjo schrieb: > Ronald Fischer <ronaldf@eml.cc> wrote: > > The mail sent out contains Cc, Bcc and Reply-To headers and > > occasionally attachment. > > > > Right now I'm using mutt for this task, because it is very > > convenient to specify this information on the mutt command line, > Try mailx. I use it on my scripts to send automated reports. mailx doesn't support Attachments, so i would have to do this manually. Ronald |
|
|||
|
Alan Connor schrieb:
> Oh yes there is! > > mutt foo@foo.net -s "foo" -e "my_hdr X-Priority: 23" Excellent! That's it!!! Thanks a lot! (Although if I would redo the application, I would maybe do it entirely in Perl instead of zsh, as some of the other posters suggested). Ronald |
|
|||
|
John-Paul Stewart <jpstewart@binaryfoundry.ca> wrote:
> Mauricio Asenjo wrote: >> >> For sending mail with custom headers, I modified a phpmailer > > class for php to do that , maybe it can be used in cgi mode > > on a shell script. > > I don't think the OP would want to run a PHP script in CGI mode since > PHP can be used directly for scripting. On Debian systems it's in the > php4-cli and/or php5-cli packages (other distros may have similar > packages). If building PHP from scratch, simply pass --enable-cli to > the configure script. For more info on command-line PHP, see: > > http://www.php.net/manual/en/features.commandline.php > That's what I meant! I'm sorry, I've got the word wrong but I was thinking abut that :-) > Moreover, with PHP there's no need for a modified phpmailer class to > send extra headers. The prototype for the PHP mail() function is: > > bool mail ( string to, string subject, string message [, string > additional_headers [, string additional_parameters]] ) > > Notice that the optional fourth parameter allows one to easily set extra > headers. > > http://www.php.net/manual/en/function.mail.php > > Depending on the OP's needs, he could either write a simple PHP script > to just do the mailing or redo the entire existing shell script in PHP. > > That' right, but, Can this function be used with an external smtp server? I had to change some scripts once because I didn't figure out how to use mail() with a smtp server (the online manual mentions smtp server support only on windows platforms) and the php scripts we were using generated their own mail headers, so we decided to use phpmailer but we modified it a bit so it can accept custom headers . If it is posible tu use mail() with an external smtp server (with autentication), then I better get rid of phpmailer to keep things simple. |