2008/05/17

The Labs.Com Issue_01_Mail
Last update 1999/02/20

TPJ: Issue_01_Mail

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. mail
  2. reply
  3. smtp
  4. More Samples on Mail
Issue_01_Mail
1. mail
Download mail

  
  #!/usr/bin/perl  
  
  
  # Create a new Mail::Internet object, 
  
  # which we'll use to build our message.  
  
  
  $mail = Mail::Internet->new();  
  
 # Add three fields to the message: 
  
 # To:, Cc:, and Subject: 
  
  
 $mail->add(    To => 'user1@foobar.com',  
  
                Cc => 'xyz@not.here',  
  
           Subject => 'Please Read This ...'  
  
            );  
 # Store @text in the message body. 
 # The $mail object makes a copy, so later  
 # changes to @text won't affect the 
 # message.  
  
 $mail->body( \@text );  

Issue_01_Mail
2. reply

Download reply

 #!/usr/bin/perl  
  
 require Mail::Internet;  
  
 # We'll need Mail::Address to extract the  
 # return address from the incoming message  
  
 require Mail::Address;  
  
 # Since we're running as a filter, the 
 # incoming message will be available from 
 # STDIN:  
  
 $mail = Mail::Internet->new( \*STDIN );  
  
 # Create an empty Mail::Internet object 
 # for our reply  
  
 $reply = Mail::Internet->new();  
  
 # Locate address of sender. The get() 
 # method returns the header line from the 
 # message, or undef if it doesn't exist.  
  
 $sender = $mail->get('Reply-To') ||  
           $mail->get('From');  
  
 # Create a Mail::Address object  
  
 # Mail::Address->parse returns a list of 
  
 # objects. We probably want the first.  
  
  
 $sender = (Mail::Address->parse($sender))[0];  
  
  
 # Create a To header on our reply message  
  
 # which contains the senders address  
  
  
 $reply->add(To => $sender->format);  
  
 # Extract the Subject from the original  
 # message  
  
 $subject = $mail->get('Subject');  
  
 if ($subject) {  
  
 # Add Re: prefix  
     $subject = 'Re: ' . $subject  
               unless $subject =~ /^\s*Re:/i;  
  
 # Set the Subject line on our new message  
  
     $reply->add(Subject => $subject);  
 }  
  
 # Extract the References and Message-Id from  
 # the original message  
  
 $ref = $mail->get('References') || '';  
 $mid = $mail->get('Message-Id');  
  
 # Combine them to create our new  
 # References line  
  
 $ref .= ' ' . $mid if $mid;  
  
 # Add our new References line to our  
 # reply message  
  
 $reply->add(References => $ref)  
                               if length $ref;  
  
  
 # Create an array containing reply text  
  
 @reply = ("This is a recorded message",  
      "I am away from my machine at the moment",  
      "and will reply to your message", 
      "as soon as I return");  
  
 # Add the body to the reply message  
  
 $reply->body( \@reply );  
  
  
 # Open a pipe to /usr/lib/sendmail. -t tells 
  
 # sendmail to scan the input for To, Cc, and  
  
 # Bcc headers, and extract the mail addresses.  
  
  
 open(MAIL, "|/usr/lib/sendmail -t")  
  
                  || die "Can't send mail: $!";  
  
  
 # print the message down the pipe  
  
 # (i.e.: send the message to sendmail) 
  
  
 $reply->print( \*MAIL );  
  
  
 close(MAIL);  

Issue_01_Mail
3. smtp

Download smtp

 #!/usr/bin/perl 
 require Net::SMTP;  
  
 use Mail::Util qw(mailaddress);  
  
  
 # Create a Net::SMTP object, which opens  
  
 # an SMTP connection to the mailhost.  
  
 # This sends the SMTP 'HELO' command.  
  
  
 $smtp = Net::SMTP->new('mailhost');  
  
  
 # Tell the SMTP server we want to start to send  
  
 # a piece of mail, use mailaddress() to find  
  
 # our own e-mail address, which will be used 
  
 # as a return address. 
  
 $smtp->mail( mailaddress() )  
  
                        || die $smtp->message;  
  
  
 # Extract all the recipients from the reply 
  
 my @rcpt = ($reply->get('To', 'Cc', 'Bcc'));  
  
  
 # use Address::parse to parse these addresses 
  
 # and return a list of Mail::Address objects.  
 # Then map these, so that we end up with a list 
 # of mail addresses. 
 my @addr = map($_->address,  
                Mail::Address->parse(@rcpt));  
  
 # Tell the SMTP server the recipient  
 # addresses. 
  
 $smtp->to(@addr) || die $smtp->message;  
  
 # Create the message as a single piece of text,  
 # ensuring that there's a blank line between  
 # the header and the body of the message  
 my $text = join("", @{$reply->header}, 
               "\n", @{$reply->body});  
  
 # Send the message to the SMTP server  
  
 $smtp->data( $text ) || die $smtp->message;  
  
 # Tell the SMTP server that we've finished  
 # ...and close the connection  
  
 $smtp->quit;  

Issue_01_Mail
4. More Samples on Mail

  • Issue_01_Mail

                                                                                                                                   

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com