<html>
<body>
At 2003/06/06 10:55 AM, Sean Carte wrote:<br>
<blockquote type=cite class=cite cite>Aha! I'm using $ARGV[0] where
you're using @ARGV:</blockquote>I did that too :-(<br><br>
<br>
I have written a object orientated perl module called Mailsend.pl.<br>
It give a very simple interface to sendmail and allows you to send HTML
formatted mail or plain or HTML and plain (multipart) and most
importantly attachments. <br>
The attachments bit is what makes it really usefully - attaching a file
in send mail is horrible and with this module you can write a simple perl
script to allow you to send attachments from the command line if you
want.<br>
It gives you control over things like the priority, from address, etc so
would also be a good start to a spam machine.<br><br>
It's not huge and is a very simple example of oop; both how to use it and
how to write one.<br><br>
The next step will be to replace sendmail with a built in SMTP engine
...<br><br>
You can perldoc it for instalation and usage stuff.<br><br>
<SNIP><br><br>
#----------------------------------------------------------------<br>
# Title :
Mailsend.pm<br>
# Author : Spike
spike@mweb.co.za<br>
# Date :
2003-03-04<br>
#<br>
# Description<br>
# -----------<br>
# This perl Module will provide a simple interface to Sendmail.<br>
# It must be copied to your library path.<br>
# To see a path list type: "perl -e 'print
join("\n", @INC)'"<br>
# Various methods allow a mail to be sent to multiple recipients<br>
# in plain text, html or both, with or without multiple
attachments.<br>
# The only obligatory methods Mailsend->new() and
setTo("address"). <br>
# If the style is set to html and only a setPlain message body is<br>
# specified the plain text message will be set to html with a
non-serif<br>
# font (more readable on screen) and any URLs will be expanded with<br>
# html tags to make them 'clickable'.<br>
# If the style is set to plain and only an html message body is set<br>
# an attempt to convert the html to plain text will be made. Note <br>
# that the results will probably be ugly.<br>
# If no style is set the setStyle will be defaulted to 'both' and <br>
# any necessary conversions made in accordance with the rules
above.<br>
# A message with an attachment set with setAttach will have it's
style<br>
# set to 'both'. Attachments are base64 encoded.<br>
# A message "priority can be set with setPriority. It can be set to
any<br>
# value but values fro 1-5 are supported by most clients. "1"
is highest, <br>
# "5" Lowest and "3" is Normal priority.<br>
# Code for subroutine 'old_encode_base64' taken from MIME::Base64<br>
# by Gisle Aas <gisle@ActiveState.com>. <br>
#<br>
# Examples <br>
# --------<br>
# $msg1 = Mailsend->new();<br>
#<br>
# $msg1->setTo('spike@mweb.com, recipient2@foo.com, ...');<br>
# $msg1->setSubject("$subject");<br>
# $msg1->setFrom('spike@mweb.co.za');<br>
# $msg1->setStyle('plain'); or 'html' or 'both'<br>
# $msg1->setPlain("$plain_message_text")<br>
# #msg1->sethtml("$html_message_code")<br>
# $msg1->setMailprog('/path/to/sendmail');<br>
# $msg1->setReplyto('sender@mweb.com');<br>
# $msg1->setCc('cc_recipient1@inode.co.za, cc_recipient2@inode.co.za,
...');<br>
# $msg1->setBcc('bcc_recipient1@inode.co.za,
bcc_recipient2@inode.co.za, ...');<br>
# $msg1->setAttach('/root/file1, /var/log/file2.txt, /home/me/file3,
...');<br>
# $msg1->setPriority('1') # 1 high to 5 low. 3 normal.<br>
# <br>
#<br>
# Change Control<br>
# ---------------<br>
# Date: 2003-03-o4<br>
# Author: Spike spike@mweb.co.za<br>
# Change: Initial version<br>
#<br><br>
package Mailsend;<br><br>
use <a href="file::Basename" eudora="autourl">File::Basename</a>;<br>
use strict;<br><br>
sub send <br>
{<br>
my (%self, @attachments, $file, $basefilename,
$encoded);<br><br>
my $self = shift;<br>
my ($die, $warn, $rv, $content_type,
$message);<br><br>
unless (defined $self->{Mailprog})<br>
{<br>
$warn .= "No mail program
defined\nI shall try and use a default\n";<br>
chomp($rv = `which
sendmail`);<br>
if ($rv !~ /no sendmail/)
<br>
{<br>
$self->{Mailprog} = $rv;<br>
$warn
.= "I have set the Mailprog to $rv\n";<br>
}<br>
else<br>
{<br>
$die
.= "I can't find a suitable Mailprog ... quiting\n";<br>
}<br>
}<br><br>
($warn) && (print $warn);<br>
($die) && (print $die) && (exit
0);<br><br>
($self->{Attach}) && ($self->{Style} =
"both");<br>
($self->{Style}) || ($self->{Style} =
"both");<br><br>
#-- plain only --#<br><br>
if ($self->{Style} =~ m/plain/i)<br>
{<br>
$content_type =
"text/plain; charset=\"us=ascii\"\n\n";<br><br>
#-- get some plain content<br><br>
if((defined $self->{Html})
&& (! defined $self{Plain}))<br>
{<br>
$self->{Plain} = makeplain("$self->{Html}");<br>
}<br><br>
#-- build and send a plain only<br><br>
$message =
$self->{Plain};<br>
}<br><br>
#-- html only --#<br><br>
if ($self->{Style} =~ m/html/i) <br>
{<br>
$content_type = 'text/html;
charset="us=ascii"';<br><br>
#-- get some html<br><br>
if ((defined
$self->{Plain}) && (! defined $self->{Html}))<br>
{<br>
$self->{Html} = makehtml("$self->{Plain}");<br>
}<br><br>
#-- build and send html only<br><br>
$message =
$self->{Html};<br>
}<br><br>
<br>
#-- both --#<br><br>
if ($self->{Style} =~ m/Both/i)<br>
{<br>
if ((defined
$self->{Plain}) && (! defined $self->{Html}))<br>
{<br>
$self->{Html} = makehtml("$self->{Plain}");<br>
}<br>
if((defined $self->{Html})
&& (! defined $self->{Plain}))<br>
{<br>
$self->{Plain} = makeplain("$self->{Html}");<br>
}<br><br>
$content_type =
'multipart/alternative;' .
qq{boundary="BoundaryPoodle"};<br><br>
$message =
"\n--BoundaryPoodle\nContent-Type: text/plain;
charset=\"us=ascii\"\n\n$self->{Plain}\n";<br>
$message .=
"\n--BoundaryPoodle\nContent-Type: text/html;
charset=\"us=ascii\"\n\n$self->{Html}\n";<br>
#$message =
"\n--BoundaryPoodle\nContent-Type: text/plain;
charset=\"us=ascii\"\nContent-Transfer-Encoding:
7bit\n\n$self->{Plain}\n";<br>
#$message .=
"\n--BoundaryPoodle\nContent-Type: text/html;
charset=\"us=ascii\"\nContent-Transfer-Encoding:
7bit\n\n$self->{Html}\n";<br>
$message .=
"\n--BoundaryPoodle--\n";<br>
}<br><br>
#-- attach--#<br><br>
if ($self->{Attach})<br>
{<br>
my $debug =
$self->{Attach};<br>
@attachments =
split(',',$self->{Attach});<br>
$content_type =
'multipart/mixed; ' . qq{boundary="mmBoundaryPoodle"};<br>
$content_type .=
"\n\n--mmBoundaryPoodle\n";<br>
$content_type .=
'Content-Type: multipart/alternative; ' .
qq{boundary="BoundaryPoodle"\n};<br><br>
foreach $file
(@attachments)<br>
{<br>
chomp($file);<br>
$file
=~ s/^\s+//g;<br>
$file
=~ s/\s+$//g;<br><br>
$basefilename = basename($file);<br><br>
(-e
"$file") || die(qq(I can't find file $file\n));<br><br>
local($/) = undef;<br>
open(FILE, "$file") or die "$!";<br>
$encoded = old_encode_base64(<FILE>);<br><br>
<br>
$message .= "--mmBoundaryPoodle";<br>
$message .= "\nContent-Type: application/octet-stream;
name=\"$basefilename\"";<br>
$message .= "\nContent-Transfer-Encoding: base64";<br>
$message .= "\nContent-Disposition: attachment;
filename=\"$basefilename\"";<br>
$message .= "\n\n$encoded";<br>
close
FILE;<br>
}<br>
$message .=
"--mmBoundaryPoodle--";<br>
}<br><br>
my $full_message = qq{From: $self->{From}\n};<br>
$full_message .= qq{To: $self->{To}\n};<br>
($self->{Replyto}) && ($full_message .=
qq{Reply-To: $self->{Replyto}\n});<br>
($self->{Cc})
&& ($full_message .= qq{Cc: $self->{Cc}\n});<br>
($self->{Bcc}) &&
($full_message .= qq{Bcc: $self->{Bcc}\n});<br>
($self->{Subject}) && ($full_message .=
qq{Subject: $self->{Subject}\n});<br>
$full_message .= "Mime-Version:
1.0\n";<br>
($self->{Priority}) && ($full_message .=
qq{X-Priority: $self->{Priority}\n});<br>
$full_message .= "Content-Type:
$content_type\n";<br>
$full_message .= $message;<br><br>
open(SENDMAIL, "|$self->{Mailprog} -oi
-t");<br>
print SENDMAIL "$full_message";<br>
close SENDMAIL;<br>
}<br><br>
# constructor #<br><br>
sub new<br>
{<br>
my $class = shift;<br>
my $self = {};<br><br>
return bless $self, $class;<br>
}<br><br>
# methods #<br><br>
sub setStyle{<br>
my $self = shift;<br>
my $style = shift;<br>
$self->{Style} = $style<br>
}<br><br>
sub setPriority{<br>
my $self = shift;<br>
my $priority = shift;<br>
$self->{Priority} = $priority;<br>
}<br><br>
<br>
sub setHtml{<br>
my $self = shift;<br>
my $html = shift;<br>
$self->{Html} = $html;<br>
}<br><br>
sub setPlain{<br>
my $self = shift;<br>
my $plain = shift;<br>
$self->{Plain} = $plain;<br>
}<br><br>
sub setAttach{<br>
my $self = shift;<br>
my $attach = shift;<br>
$self->{Attach} = $attach;<br>
}<br><br>
sub setSubject{<br>
my $self = shift;<br>
my $subject = shift;<br>
$self->{Subject} = $subject;<br>
}<br><br>
sub setTo{<br>
my $self = shift;<br>
my $to = shift;<br>
$self->{To} = $to;<br>
}<br><br>
sub setFrom{<br>
my $self = shift;<br>
my $from = shift;<br>
$self->{From} = $from;<br>
}<br><br>
sub setCc{<br>
my $self = shift;<br>
my $cc = shift;<br>
$self->{Cc} = $cc;<br>
}<br><br>
sub setBcc{<br>
my $self = shift;<br>
my $bcc = shift;<br>
$self->{Bcc} = $bcc;<br>
}<br><br>
sub setMailprog{<br>
my $self = shift;<br>
my $mailprog = shift;<br>
$self->{Mailprog} = $mailprog;<br>
}<br><br>
sub setReplyto{<br>
my $self = shift;<br>
my $replyto = shift;<br>
$self->{Replyto} = $replyto;<br>
}<br><br>
sub makehtml<br>
{<br>
my $body = shift;<br><br>
$body =~ s/ftp:\S+/<A
HREF="$&">$&<\/A>/gs;<br>
$body =~ s/http:\S+/<A
HREF="$&">$&<\/A>/gs;<br>
$body =~ s/https:\S+/<A
HREF="$&">$&<\/A>/gs;<br>
my $htmlbody = "<PRE><font
face=\"Arial, Helvetica, sans-serif\"> $body
<\/font><\/PRE>";<br><br>
return $htmlbody<br>
}<br><br>
sub makeplain<br>
{<br>
my ($line, $plaintext, @plain);<br><br>
my $html = shift;<br><br>
my @HTML = split(/(<[^>]*>)/,
$html);<br><br>
foreach $line (@HTML)<br>
{<br>
$line =~ s/\n//g;<br>
$line =~ s/^\s+//g;<br>
$line =~ s/\s+$//g;<br>
$line =~
s/<\s*HR*>/-----------------------------------------------------------\n/i;<br>
($line =~ m#<\s*/TD>#i)
&& push(@plain, "
");<br>
($line =~ m#<\s*/TR>#i)
&& push(@plain, "\n");<br>
($line =~
m#<\s*TABLE>#i) && push(@plain, "\n");<br>
($line =~ m#<\s*BR>#i)
&& push(@plain, "\n");<br>
($line =~ m#<\s*/DIV>#i)
&& push(@plain, "\n\n");<br>
($line =~ m#<\s*/P>#i)
&& push(@plain, "\n\n");<br>
($line =~ m#<*>#)
&& (next);<br>
push(@plain,
"$line");<br>
}<br><br>
$plaintext = join(' ', @plain);<br>
return $plaintext;<br><br>
}<br><br>
sub old_encode_base64 ($;$)<br>
{<br>
my $res = "";<br>
my $eol = $_[1];<br>
$eol = "\n" unless defined $eol;<br>
pos($_[0]) =
0;
# ensure start at the beginning<br><br>
$res = join '', map( pack('u',$_)=~ /^.(\S*)/,
($_[0]=~/(.{1,45})/gs));<br><br>
$res =~ tr|`
-_|AA-Za-z0-9+/|;
# `# help emacs<br>
# fix padding at the end<br>
my $padding = (3 - length($_[0]) % 3) % 3;<br>
$res =~ s/.{$padding}$/'=' x $padding/e if
$padding;<br>
# break encoded string into lines of no more than 76
characters each<br>
if (length $eol) {<br>
$res =~
s/(.{1,76})/$1$eol/g;<br>
}<br>
return $res;<br>
}<br><br>
<br>
1;<br><br>
__END__<br><br>
=head1 NAME<br><br>
Mailsend.pm<br><br>
=head1 SYNOPSIS<br><br>
<br>
$msg1 = Mailsend->new();<br><br>
<br>
$msg1->setTo('spike@mweb.com, recipient2@foo.com,
...');<br><br>
<br>
$msg1->setSubject("$subject");<br><br>
<br>
$msg1->setFrom('spike@mweb.co.za');<br><br>
<br>
$msg1->setStyle('plain'); or 'html' or 'both'<br><br>
<br>
$msg1->setPlain("$plain_message_text")<br><br>
<br>
#msg1->sethtml("$html_message_code")<br><br>
<br>
$msg1->setMailprog('/path/to/sendmail');<br><br>
<br>
$msg1->setReplyto('sender@mweb.com');<br><br>
<br>
$msg1->setCc('cc_recipient1@inode.co.za,<br>
cc_recipient2@inode.co.za,<br>
...');<br><br>
<br>
$msg1->setBcc('bcc_recipient1@inode.co.za,<br>
bcc_recipient2@inode.co.za,<br>
...');<br><br>
<br>
$msg1->setAttach('/root/file1, /var/log/file2.txt,<br>
/home/me/file3, ...');<br><br>
<br>
$msg1->setPriority('1') # 1 high to 5 low. 3 normal.<br><br>
<br>
$msg1->send();<br><br>
=head1 DESCRIPTION<br><br>
<br>
This perl Module will provide a simple interface to Sendmail.<br>
It must be copied to your library path.<br>
To see a path list type: "perl -e 'print join("\n",
@INC)'"<br><br>
Various methods allow a mail to be sent to multiple recipients<br>
in plain text, html or both, with or without multiple attachments.<br>
The only obligatory methods Mailsend->new() and
setTo("address")<br>
and send().<br><br>
If the style is set to html and only a setPlain message body is<br>
specified the plain text message will be set to html with a
non-serif<br>
font (more readable on screen) and any URLs will be expanded with<br>
html tags to make them 'clickable'.<br><br>
If the style is set to plain and only an html message body is set<br>
an attempt to convert the html to plain text will be made. Note<br>
that the results will probably be ugly.<br><br>
If no style is set the setStyle will be defaulted to 'both' and<br>
any necessary conversions made in accordance with the rules
above.<br><br>
A message with an attachment set with setAttach will have it's
style<br>
set to 'both'. Attachments are base64 encoded.<br><br>
A message "priority can be set with setPriority. It can be set to
any<br>
value but values fro 1-5 are supported by mosr clients. "1" is
highest,<br>
"5" Lowest and "3" is Normal priority.<br><br>
Code for subroutine 'old_encode_base64' taken from MIME::Base64<br>
by Gisle Aas <gisle@ActiveState.com>.<br><br>
<br>
=cut<br><br>
<br>
<SNIP><br><br>
<br><br>
<br><br>
<br><br>
<x-sigsep><p></x-sigsep>
<tt><font face="Courier New, Courier" size=2>Spike Hodge<br><br>
UNIX Programmer<br>
</font></tt><font face="Courier New, Courier" size=2>M-Web
Technology<br>
</font><tt>021 596 8496<br>
082 901 5265<br><br>
</tt>Click here and make M-Web your homepage<br>
<a href="http://homepage.mweb.co.za/" eudora="autourl">http://homepage.mweb.co.za</a></body>
</html>