Bluehost.com Web Hosting $6.95

Sending Mail from Script: Setting Header

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 ...


Go Back   Usenet Forums > Linux Forums > Linux General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-20-2006
Ronald Fischer
 
Posts: n/a
Default Sending Mail from Script: Setting Header

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/

Reply With Quote
  #2 (permalink)  
Old 10-20-2006
Bit Twister
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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;
Reply With Quote
  #3 (permalink)  
Old 10-20-2006
zoot
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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/


Reply With Quote
  #4 (permalink)  
Old 10-20-2006
s. keeling
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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
Reply With Quote
  #5 (permalink)  
Old 10-20-2006
Mauricio Asenjo
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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

Reply With Quote
  #6 (permalink)  
Old 10-20-2006
John-Paul Stewart
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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.


Reply With Quote
  #7 (permalink)  
Old 10-21-2006
Alan Connor
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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
Reply With Quote
  #8 (permalink)  
Old 10-23-2006
Ronny
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header


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

Reply With Quote
  #9 (permalink)  
Old 10-23-2006
Ronny
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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

Reply With Quote
  #10 (permalink)  
Old 10-23-2006
Mauricio Asenjo
 
Posts: n/a
Default Re: Sending Mail from Script: Setting Header

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 01:33 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0