-
Notifications
You must be signed in to change notification settings - Fork 130
Items vsEmail
See original
my $objMessage = new vsEmail(
SendmailPath => "/usr/sbin/sendmail",
HtmlMode => 0,
From => "email\@address.com",
To => "email\@address.com",
Subject => "Subject",
);
$objMessage->Message("Message Line 1");
$objMessage->AppendToMessage("Message Line 2");
$objMessage->Send;
# - OR -
my $objMessage = new vsEmail(
SmtpServer => "localhost",
HtmlMode => 1,
From => "email\@address.com",
To => "email\@address.com",
Subject => "Subject",
);
$objMessage->Message("<B>Message Line 1</B><br>");
$objMessage->AppendToMessage("Message Line 2<br>");
$objMessage->AppendToMessage("<hr>");
$objMessage->Send;
vsEmail.pm provides an object oriented interface for sending email messages in plain text or HTML format using either Sendmail or SMTP.
USAGE
Refer to http://www.verysimple.com/scripts/ Broken Link for more information.
Jason M. Hinkle
COPYRIGHT Copyright (c) 2000 Jason M. Hinkle. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module incorporates SendMail 0.77 by Milivoj Ivkovic for SMTP functionality. See below for details:
v. 0.77 - Simple platform independent mailer
use vsSmtpMail;
%mail = ( To => '[email protected]',
From => '[email protected]',
Message => "This is a very short message"
);
sendmail(%mail) or die $vsSmtpMail::error;
print "OK. Log says:\n", $vsSmtpMail::log;
Simple platform independent e-mail from your perl script. Only requires Perl 5 and a network connection.
After struggling for some time with various command-line mailing programs which never did exactly what I wanted, I put together this Perl only solution.
vsSmtpMail
contains mainly &sendmail
, which takes a hash with the message to send and sends it. It is intended to be very easy to setup and use.
Best
perl -MCPAN -e "install vsSmtpMail"
Traditional
perl Makefile.PL
make
make test
make install
Manual
Copy Sendmail.pm to Mail/ in your Perl lib directory.
(eg. c:\Perl\lib\Mail\, c:\Perl\site\lib\Mail\,
/usr/lib/perl5/site_perl/Mail/, ... or whatever it
is on your system)
ActivePerl's PPM
ppm install --location=http://alma.ch/perl/ppm Mail-Sendmail
But this way you don't get a chance to have a look at other files (Changes, Todo, test.pl, ...) and PPM doesn't run the test script (test.pl).
At the top of Sendmail.pm, set your default SMTP server, unless you specify it with each message, or want to use the default.
Install MIME::QuotedPrint. This is not required but strongly recommended.
- Automatic time zone detection, Date: header, MIME quoted-printable encoding (if MIME::QuotedPrint installed), all of which can be overridden.
- Internal Bcc: and Cc: support (even on broken servers)
- Allows real names in From: and To: fields
- Doesn't send unwanted headers, and allows you to send any header(s) you want
- Configurable retries and use of alternate servers if your mail server is down
- Good plain text error reporting
Headers are not encoded, even if they have accented characters.
Since the whole message is in memory (twice!), it's not suitable for sending very big attached files.
The SMTP server has to be set manually in Sendmail.pm or in your script, unless you can live with the default (localhost or Compuserve's smpt.site1.csi.com).
Default SMTP server(s) This is probably all you want to configure. It is usually done through $mailcfg{smtp}, which you can edit at the top of the Sendmail.pm file. This is a reference to a list of SMTP servers. You can also set it from your script:
unshift @{$vsSmtpMail::mailcfg{'smtp'}} , 'my.mail.server';
Alternatively, you can specify the server in the %mail hash you send from your script, which will do the same thing:
$mail{smtp} = 'my.mail.server';
A future version will try to set useful defaults for you during the Makefile.PL.
Other configuration settings
See %mailcfg
under "DETAILS" below for other configuration options.
sendmail is the only thing exported to your namespace by default
sendmail(%mail) || print "Error sending mail: $vsSmtpMail::error\n";
It takes a hash containing the full message, with keys for all headers, body, and optionally for another non-default SMTP server and/or port.
It returns 1 on success or 0 on error, and rewrites $vsSmtpMail::error and $vsSmtpMail::log.
Keys are NOT case-sensitive.
The colon after headers is not necessary.
The Body part key can be called 'Body', 'Message' or 'Text'. The SMTP server key can be called 'Smtp' or 'Server'.
The following headers are added unless you specify them yourself:
Mime-version: 1.0
Content-type: 'text/plain; charset="iso-8859-1"'
Content-transfer-encoding: quoted-printable
or (if MIME::QuotedPrint not installed)
Content-transfer-encoding: 8bit
Date: [string returned by time_to_date()]
The following are not exported by default, but you can still access them with their full name, or request their export on the use line like in: use `vsSmtpMail qw($address_rx time_to_date);``
convert time ( as from time() ) to an RFC 822 compliant string for the Date header. See also "%vsSmtpMail::mailcfg"
.
When you don't run with the -w flag, the module sends no errors to STDERR, but puts anything it has to complain about in here. You should probably always check if it says something.
A summary that you could write to a log file after each send
A handy regex to recognize e-mail addresses.
A correct regex for valid e-mail addresses was written by one of the judges in the obfuscated Perl contest... :-) It is quite big. This one is an attempt to a reasonable compromise, and should accept all real-world internet style addresses. The domain part is required and comments or characters that would need to be quoted are not supported.
Example:
$rx = $vsSmtpMail::address_rx;
if (/$rx/) {
$address=$1;
$user=$2;
$domain=$3;
}
This hash contains all configuration options. You normally edit it once (if ever) in Sendmail.pm and forget about it, but you could also access it from your scripts. For readability, I'll assume you have imported it.
The keys are not case-sensitive: they are all converted to lowercase before use. Writing $mailcfg{Port} = 2525; is OK: the default $mailcfg{port} (25) will be deleted and replaced with your new value of 2525.
This is a reference to a list of smtp servers, so if your main server is down, the module tries the next one. If one of your servers uses a special port, add it to the server name with a colon in front, to override the default port (like in my.special.server:2525).
Default: localhost and smtp.site1.csi.com (which seems to be an open relay)
$mailcfg{from}
= 'Mailing script [email protected]';
From address used if you don't supply one in your script. Should not be of type 'user@localhost' since that may not be valid on the recipient's host.
Default: undefined.
Set this to 0 if you don't want any automatic MIME encoding. You normally don't need this, the module should 'Do the right thing' anyway.
Default: 1;
How many times should the connection to the same SMTP server be retried in case of a failure.
Default: 1;
Number of seconds to wait between retries. This delay also happens before trying the next server in the list, if the retries for the current server have been exhausted. For CGI scripts, you want few retries and short delays to return with a results page before the http connection times out. For unattended scripts, you may want to use many retries and long delays to have a good chance of your mail being sent even with temporary failures on your network.
Default: 1 (second);
Normally, your time zone is set automatically, from the difference between time() and gmtime(). This allows you to override automatic detection in cases where your system is confused (such as some Win32 systems in zones which do not use daylight savings time: see Microsoft KB article Q148681)
Default: undefined (automatic detection at run-time).
Port used when none is specified in the server name.
Default: 25.
Prints stuff to STDERR. Not used much, and what is printed may change without notice. Don't count on it.
Default: 0;
The package version number (you can not import this one)
Configuration variables from previous versions The following global variables were used in version 0.74 for configuration. They should still work, but will not in a future version (unless you complain loudly). Please use %mailcfg if you need to access the configuration from your scripts.
$vsSmtpMail::default_smtp_server
$vsSmtpMail::default_smtp_port
$vsSmtpMail::default_sender
$vsSmtpMail::TZ
$vsSmtpMail::connect_retries
$vsSmtpMail::retry_delay
$vsSmtpMail::use_MIME
This one couldn't really be used in the previous version, so I just dropped it. It is replaced by $mailcfg{mime} which works.
ANOTHER EXAMPLE
use vsSmtpMail;
print "Testing vsSmtpMail version $vsSmtpMail::VERSION\n";
print "Default server: $vsSmtpMail::mailcfg{smtp}->[0]\n";
print "Default sender: $vsSmtpMail::mailcfg{from}\n";
%mail = (
#To => 'No to field this time, only Bcc and Cc',
#From => 'not needed, use default',
Bcc => 'Someone <[email protected]>, Someone else [email protected]',
# only addresses are extracted from Bcc, real names disregarded
Cc => 'Yet someone else <[email protected]>',
# Cc will appear in the header. (Bcc will not)
Subject => 'Test message',
'X-Mailer' => "vsSmtpMail version $vsSmtpMail::VERSION",
);
$mail{Smtp} = 'special_server.for-this-message-only.domain.com';
$mail{'X-custom'} = 'My custom additionnal header';
$mail{'mESSaGE : '} = "The message key looks terrible, but works.";
# cheat on the date:
$mail{Date} = vsSmtpMail::time_to_date( time() - 86400 ),
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $vsSmtpMail::error \n" }
print "\n\$vsSmtpMail::log says:\n", $vsSmtpMail::log;
CHANGES Many changes and bug-fixes since version 0.74. In short: less code, more functionality and docs. See the Changes file.
Milivoj Ivkovic [email protected] or [email protected]