Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
################
# Use SendMail #
################
use SendMail;
# create mailer object
my $mail = SendMail->new(
# remember to pass arrays as a reference to the array with a backslash \@myarray
SEND_TYPE => 'TLS',
SERVER => 'my.mailserver.com',
PORT => 25,
TIMEOUT => 30,
UID => 'myUserID',
PWD => 'myPassword',
TO => \@recip,
FROM => 'webmaster@mydomain.com',
SUBJECT => 'This is a test email subject!',
BODY => $body,
FILE => \@attach_file,
FILE_NAME => \@attach_name,
FILE_TYPE => \@attach_type
);
# send the mail
$mail->sendmail;
SEND_TYPE => 'SMTP'
#############################################################################################################################
# Name : SendMail.pm
# Version : v0.02
# Author : Craig Chant (DJ - C.D.C.) - http://dance-music.org - dmo@dance-music.org
# Copyright : Craig Chant (c) 29/06/2011
# Acknowledgement : Tatsuhiko Miyagawa - http://plagger.org/trac/browser/trunk/plagger/lib/Plagger/Plugin/Publish/Gmail.pm
# Special thanks to : Dr. ishnid - http://ishnid.ucd.ie/ - A gentleman and a scholar!
##############################################################################################################################
# OO Perl module to send email securely over TLS encryption / authentication or normal SMTP. Requires following Perl modules
# to be installed on server...
#
# MIME::Lite
# Net::SMTP::TLS
# Net::SSLeay
# IO::Socket::INET
#
###############################################################################################################################
######################
# Set Error Trapping #
######################
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use warnings;
use strict;
##########################
# Set Package Name Space #
##########################
package SendMail;
########################
# Public Class Version #
########################
our $VERSION = '0.02';
###########################
# Class Constructor New() #
###########################
sub new {
# class variable
my $class = shift;
# class oject
my $self;
# class attributes
$self = {
@_,
ERRORS => [],
WARNINGS => []
};
# Default attribute values
# Server
if(! $self->{SERVER}){
$self->{SERVER} = '127.0.0.1';
}
# Send type
if(! $self->{SEND_TYPE}){
$self->{SEND_TYPE} = 'TLS';
}
# Port
if(! $self->{PORT}){
$self->{PORT} = 25;
}
# Timeout
if(! $self->{TIMEOUT}){
$self->{TIMEOUT} = 30;
}
# Bless the data structure, making it a true object
bless ($self,$class);
# Return instantiated object
return $self;
}
###################################################
################## CLASS METHODS ##################
###################################################
###################
# sendmail method #
###################
# Preconditions : requires valid values for :- recipient array (TO), subject scaler (SUBJECT), sender scalar (FROM) & email content scalar (BODY)
# : if attachment array (FILE) is provided, matching arrays for (FILE_NAME & FILE_TYPE) must be provided.
#
# Postconditions : an attempt to send email via TLS using login credentials (UID & PWD) if provided, on port (PORT) is made or by standard SMTP,
# : selectable via scaler (SEND_TYPE) to mail server (SERVER).
###################
sub sendmail {
# Assign object
my $self = shift;
# Check for valid email details
# Recipients' email
if (! $self->{TO}){
$self->error('Missing recipients email address');
}
# Subject
if (! $self->{SUBJECT}){
$self->error('Missing email subject');
}
# Sender email
if (! $self->{FROM}){
$self->error('Missing sender email address');
}
# Email content body HTML
if (! $self->{BODY}){
$self->error('Missing email body content');
}
# Check for attachment
if (defined @{$self->{FILE}}){
for (my $i=0; $i<@{$self->{FILE}}; $i++){
if(! -e $self->{FILE}[$i]){
$self->error("Missing attachment file : $self->{FILE}[$i]");
}
if(! $self->{FILE_NAME}[$i]){
$self->error("Missing attachment file name : ( $self->{FILE}[$i] )");
}
if(! $self->{FILE_TYPE}[$i]){
$self->error("Missing attachment file type : ( $self->{FILE}[$i] )");
}
}
}
# if no errors
if (! @{$self->{ERRORS}}){
# Send email
$self->sendit;
# check send errors
return ! @{$self->{ERRORS}} ? 1 : 0;
}
else{return 0;}
}
########################
# sendit helper method #
########################
sub sendit {
# Assign object
my $self = shift;
##################
# Use MIME::Lite #
##################
use MIME::Lite;
# Add MIME::Lite TLS hack
&mimehack;
# Loop recipients and send
foreach my $to (@{$self->{TO}}){
# create new msg object
my $msg = MIME::Lite->new(
From => $self->{FROM},
To => $to,
Subject => $self->{SUBJECT},
Type => 'multipart/mixed'
);
# check for attachments
if (defined @{$self->{FILE}}){
for (my $i=0; $i<@{$self->{FILE}}; $i++){
$msg->attach(
Type => $self->{FILE_TYPE}[$i],
Path => $self->{FILE}[$i],
Filename => $self->{FILE_NAME}[$i]
);
}
}
# add email
$msg->attach(
Type => 'TEXT/HTML',
Data => $self->{BODY}
);
# check send type
if ($self->{SEND_TYPE} eq 'TLS'){
# TLS arguments
my @tls_args = (
$self->{SERVER},
Port => $self->{PORT},
Timeout => $self->{TIMEOUT}
);
# check for user id
if (defined $self->{UID}){
push (@tls_args,User => $self->{UID});
}
# check for password
if (defined $self->{PWD}){
push (@tls_args,Password => $self->{PWD});
}
# send via TLS or SMTP if TLS fails
if (! $msg->send_by_smtp_tls($self, @tls_args)){
eval {$msg->send('smtp', $self->{SERVER}, Timeout => $self->{TIMEOUT})};
$self->error($@) if $@;
}
}
else{
# send via SMTP
eval {$msg->send('smtp', $self->{SERVER}, Timeout => $self->{TIMEOUT})};
$self->error($@) if $@;
}
}
}
#######################
# MIME::Lite TLS Hack #
#######################
sub mimehack {
# Add MIME::Lite hack to support TLS authentication / encryption
*MIME::Lite::send_by_smtp_tls = sub {
# Set objects & arguments
my($self, $sendmail, @args) = @_;
# Create SMTP TLS client:
eval{
# Use Net::SMTP::TLS
require Net::SMTP::TLS;
my $smtp = MIME::Lite::SMTP::TLS->new(@args);
$smtp->mail($self->get('From'));
$smtp->to($self->get('To'));
$smtp->data();
# MIME::Lite can print() to anything with a print() method:
$self->print_for_smtp($smtp);
$smtp->dataend();
};
# check for TLS errors and set warnings
$sendmail->warning($@) if $@;
return $@ ? 0 : 1;
};
@MIME::Lite::SMTP::TLS::ISA = qw( Net::SMTP::TLS );
sub MIME::Lite::SMTP::TLS::print { shift->datasend(@_) }
}
########################
# errors helper method #
########################
sub error {
# get object
my $self = shift;
# add error
push @{$self->{ERRORS}}, @_;
}
##########################
# warnings helper method #
##########################
sub warning {
# get object
my $self = shift;
# add warning
push @{$self->{WARNINGS}}, @_;
}
#############################################
######### End of Module - Return 1; #########
#############################################
1;