Send email

Using the Mail::Sendmail module to send email

use Mail::Sendmail;

my %mail = ( To => 'joe@example.com',
          From => 'foo@example.com',
          Message => 'This is a very cool message that I hope you get.'
          );

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK. Log says:\n", $Mail::Sendmail::log;

http://search.cpan.org/~mivkovic/Mail-Sendmail-0.79/Sendmail.pm »

Here's another example in it's entirety which also uses the Email::Valid module to validate email addresses.

#!/usr/local/bin/perl

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use Mail::Sendmail;
use Email::Valid;

### Usage: send_email("to\@address","subject line","message body","from\@address");
### Returns: True and sends an email, or it dies.
### Note: Must escape @ if using "quotes".
sub send_email
{
	my($to,$subject,$message,$from) = @_;

	if(Email::Valid->address($to))
	{
		my %mail = ( To => $to, Subject => $subject, Message => $message, From => $from );

		sendmail(%mail) or die('Email could not be sent');
	}
	else
	{
		die('Invalid email address');
	}
}

send_email("foo@example.com","This is a subject","This is a message body","frombar@example.com");

Simple script using sendmail

# Simple Email Function
# ($to, $from, $subject, $message)
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
} 

http://perl.about.com/od/email/a/perlemailsub.htm »

Another useful site about emailing mentions the Email::Valid module which can validate email addresses for you and strip out strings that could be sent to sendmail.

http://www.perlfect.com/articles/sendmail.shtml »

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.