Home » Perl Basics
13

Mail Integration

2 ways to send mails

Depending on the system you will have the script on 2 options are offered to you to send emails.
The first one is system dependant. On linux systems you can use the system command to redirect a file handle to sendmail.
The second will use a module.

1 option:

Our example makes use of sendmail. To use another mailer you will have to use the correct command line after the “|”

  #!/usr/sbin/perl
  #purpose : send emails with system mailer.

open (MAIL, “ | /usr/lib/sendmail –oi –t);# this will create a file handle MAIL and send the ouput to the prompt after the “/usr/lib/sendmail” command instead of writing it in a file. print MAIL << “EOF”; EOF mean End Of File. Again in order to make your script mode readable you should use standards. while it is true you can use anything to mark the end of the text bellow EOF will be recognized by anyone with coding experience.

  From: you
  To: me
  Subject: Testing 1st option to send mails
#that space is necessary in sendmail has it will expect a blank line after “Subject” to begin the body of the email. Body of “testing 1st option to send mails”
  EOF

IMPORTANT : Since we are using the print function to send that email, you can include variable inside the body of the email. Those variables will be interpreted in the email you will send.

2nd option :

The second option is to use a module. We will again use CPAN to install a module that will help us send emails.
As before you can use cpan.org or the cpan perl command interface.

Doing a search on either the command line of the website you will see that a lot of modules exist. Some will allow to use hotmail or yahoo to mail data. Other are specialized in parsing mail logs.etc. In our example we will use the generic mailer Mail::Mailer. It is updated regularly and allow you to use almost any mailer or to use a remote server to send emails.

First we will install the mailer

  Perl –MCPAN –e shell

Cpan>install Mail::Mailer
# remember that it is case sensitive either on windows or *nix systems

The install is fairly quick and do not require any input from you. Once the install is done your module is ready to be used.

Like most of the module you will be able to find a manual online or offline. for that particular module go to perl/html/site/lib/Mail to find and extensive documentation as well as a quick start manual that present you with the most common function go get you up and running in a few minutes.

We presented you with the 2 method to send emails both localy and not. We did not go into detail for the module section because each module will have its own syntax and arguments. They are usually easy to use and you should not have problems using them efficiently very quickly.