1 / 36

Chapter 7

Chapter 7. SMTP: Sending Mail. Net::SMTP and MIME-Tools. Net::SMTP allows you to send email to a mail server. SMTP stands for Simple Mail Transport Protocol You can not receive mail with SMTP, only send it.

matana
Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 SMTP: Sending Mail

  2. Net::SMTP and MIME-Tools • Net::SMTP allows you to send email to a mail server. • SMTP stands for Simple Mail Transport Protocol • You can not receive mail with SMTP, only send it. • MIME-Tools is a package for creating, decoding and manipulating Multipurpose Internet Mail Extensions (MIME); otherwise known as “attachments”.

  3. SMTP Protocol • Used for exchanging info between mail servers or for sending info from a mail client to a mail server. • Uses port number 25 • Client and server exchange info (client identifies itself) and then the client can send an email to be transmitted. • SMTP is a text protocol.

  4. SMTP interaction $ telnet prego smtp connected to prego.lsjs.org. Escape character is ‘^]’. 220 prego.lsjs.org ESMTP Sendmail 8.9.3/8.8.5; Tue, 28 Mar 2006 14:38:00 -0400 HELO joyous.cs.newpaltz.edu prego.lsjs.org Hello joyous.cs.newpaltz.edu [137.140.8.101], pleased to meet you MAIL From: <pletcha@newpaltz.edu> 250 <pletcha@newpaltz.edu>... Sender ok RCPT To: <lstein@cshl.org> 250 <lstein@cshl.org>... Recipient ok DATA 354 Enter mail, end with “.” on a line by itself From: Andrew Pletch pletcha@newpaltz.edu To: Lincoln D.Stein lstein@cshl.org Subject: A Message Here is my message . 250 0AA04310 Message accepted for delivery QUIT 221 prego.lsjs.org closed connection Connection closed to foreign host. could fail mail command From: To: Subject: are minimal; the date that you, the recipient, sees comes from the server RFC 821

  5. Net::SMTP API • Uses OO syntax • Comes from CPAN • Part of perl now • Inherits from Net::Cmd and IO::Socket::INET $smtp = Net::SMTP->new([$host],[$opt1=>$val1, $opt2=>$val2…]); HELO argument; there is a default options can be from IO::Socket or Timeout - seconds to wait - 120 Debug - verbose info - undef Port - mail server port - 25

  6. more API $banner = $smtp->banner(); $domain = $$smtp->domain; • Experiment with these to see what is their precise value. #!/usr/bin/perl use Net::SMTP; $smtp = Net::SMTP->new("npmail.newpaltz.edu"); $banner = $smtp->banner(); $domain = $smtp->domain(); printf "banner>>>$banner<<<\n"; printf "domain>>>$domain<<<\n"; banner>>>newpaltz.edu ESMTP CommuniGate Pro 4.3.9 <<< domain>>>newpaltz.edu<<<

  7. more API $success = $smtp->mail($address [,\%options]); • This issues the MAIL command • $address is the address of the sender • the hash reference points to a list of options that an Extended SMTP server can handle. Rarely needed. • Address formats: • Returns true if ok; undef otherwise. $smtp->message() returns the error message. doe@acme.org <doe@acme.org> John Doe <doe@acme.org> doe@acme.org (John Doe)

  8. more API anonymous hash; several options • This sends the RCPT command. All addresses must be valid or entire function fails. • The second version lets only valid addresses be used. • or $success = $smtp->recipient($address1,$address2,…); @ok_addr = $smtp->recipient($address1,$address2,…,{SkipBad=>1}); $success = $smtp->data($text); $success = $smtp->data(); $success = $smtp->datasend($line); $success = $smtp->dataend(); repeated, many times sends the data

  9. sample code $smtp = Net::SMTP->new($ServerName);$smtp->mail( $from );$smtp->recipient( $to ); $smtp->data();$smtp->datasend("To:  $to\n");$smtp->datasend("From:  $from\n");$smtp->datasend("Date: $mdate\n");$smtp->datasend("Subject: $subject\n");$smtp->datasend("MIME-Version: 1.0\n");$smtp->datasend("Content-type: text/html\n");$smtp->datasend("Content-Transfer-Encoding: 7bit\n");$smtp->datasend("\n");$smtp->datasend("$msg\n\n");$smtp->dataend();$smtp->quit();

  10. more API $smtp->reset(); $valid = $smtp->verify($address); @recipients = $smtp->expand($address); $smtp->quit(); • Sends RSET command. All commands cancelled. • Check if an address is valid • Expands valid addresses to any known aliases, including forwarding addresses or mailing list recipients. • This feature is often disabled by mail administrators. • Politely breaks a connection to a mail server.

  11. Another example #!/usr/bin/perl use Net::SMTP; $smtp = Net::SMTP->new("npmail.newpaltz.edu"); @recipients = $smtp->expand("pletcha@cs.newpaltz.edu"); printf @recipients. "\n"; 0 indicates we are not user-friendly

  12. send_email.pl #!/usr/bin/perl use Net::SMTP; $msg = <<'END'; From: Andrew Pletch <pletcha@newpaltz.edu> To: Andrew Pletch <pletcha@newpaltz.edu> Cc: pletcha@cs.newpaltz.edu Subject: hello there This is just a simple e-mail message me END mail($msg, 'npmail.newpaltz.edu') or die "Didn't make it"; exit 1; valid way to build a multi-line string in perl

  13. send_email.pl sub mail { my ($msg,$server) = @_; # parse the message to get sender and recipient my ($header,$body) = split (/\n\n/,$msg,2); return warn "no header" unless $header && $body; # fold continuation lines $header =~ s/\n\s+/ /gm; # parse fields my (%fields) = $header =~ /([\w-]+):\s+(.+)$/mg; my $from = $fields{From} or return warn "no From field"; my @to = split /\s*,\s*/,$fields{To} or return warn "no To field"; push @to,split /\s*,\s*/,$fields{Cc} if $fields{Cc}; # open server my $smtp = Net::SMTP->new($server) or return warn "couldn't open server"; $smtp->mail($from) or return warn $smtp->message; my @ok = $smtp->recipient(@to,{SkipBad=>1}) or return warn $smtp->message; warn $smtp->message unless @ok == @to; $smtp->data($msg) or return warn $smtp->message; $smtp->quit; } only split in 2 in case body contains \n\n continuation lines are “indented” multiple lines comparing lengths

  14. MailTools #!/usr/bin/perl # file: mailtools1.pl # Figure 7.3: Sending e-mail with Mail::Internet use Mail::Internet; my $head = Mail::Header->new; $head->add(From => 'John Doe <doe@acme.org>'); $head->add(To => 'L Stein <lstein@lsjs.org>'); $head->add(Cc => 'jac@acme.org'); $head->add(Cc => 'vvd@acme.org'); $head->add(Subject => 'hello there'); my $body = <<END; This is just a simple e-mail message. Nothing to get excited about. Regards, JD END $mail = Mail::Internet->new(Header => $head, Body => [$body], Modify => 1); print $mail->send('sendmail'); the advantage of MailTools anonymous array of lines try to use sendmail

  15. MailTools advantages: • Mail headers are complex: • fields occur multiple times • some fields have multiple values • line continuation is white space at start of line • header lines can be arbitrary length limitations • best to use Mail::Header to build headers • Mail::Header controls syntax, not contents. • Stick to approved header fields: RFCs 822 & 2045 Header Fields Bcc Date Received Sender Cc From References Subject Comments Keywords Reply-To To Content-Type Message-ID Resent-From X-* Content-Transfer-Encoding MIME-Version Resent-To Content-Dispostion Organization Return-Path

  16. Mail::Header methods: • Constructor. Called with no args, we get a new header with no fields. • $arg can be an open file handle or an array reference. Either can be a list of valid header fields w/ data • Modify is the most common option. • This is an alternative to passing the 1st argument to new. $head = Mail::Header->new([$arg][,@options]); open(HEADERS,”./mail.format”); $head = Mail::Headers->new(\*HEADERS, Modify=>1); $head->read(FILEHANDLE);

  17. Mail::Header methods: $head->add($name,$value[,$index]); $head->replace($name,$value[,$index]); $head->delete($name,$value[,$index]); • This allows you to arrange individual header fields to your liking. If $index is provided then the new value goes in this position if the appropriate value is a list. If no $index is provided then we add() at the end and replace() at the beginning. $head->replace(‘Subject: a subject line’); $head->replace(Subject => ‘a subject line’); acceptable alternatives

  18. Mail::Header methods: $line = $head->get($name [,$index]); @lines = $head->get($name); @fields = $head->tags() $count = $head->($tag); • Used to find out about an existing header. A “bug” is that stored \newlines are not removed; you must do it. • header_hashref() gets and/or sets a header. The (key,value) pairs are the header tags and values as lists. • Last is equivalent to how many times tag appears $string = $head->as_string(); $hashref = $head->header_hashref([\%headers]); $head->print(FILEHANDLE); print FILEHANDLE $head->as_string();

  19. Mail::Internet • Used to create, manipulate and send mail. • Just like Mail::Header->new() • @Options can contain Header and Body as tags. If present they override anything $arg provided. It is still a list of (tag, value) pairs . $mail = Mail::Internet->new([$arg][,@options]);

  20. Manipulating an email object: $arrayref = $mail->body(); $header = $mail->head(); $string = $mail->as_string(); $string = $mail->as_mbox_string(); $mail->print(FILEHANDLE); $mail->print_header(FILEHANDLE); $mail->print_body(FILEHANDLE); • Since body() returns a reference you can modify the body from that reference. Same for head(). special format

  21. Manipulating an email object: $mail->add_signature([$file]); $mail->remove_sig([$nLines]); $reply = $mail->reply(); • If no file is given to add_signature() then $ENV{HOME}/.signature is used. • an email signature is often offset from the body by a line with ’—’ on it. remove_sig() scans the last $nLines of the email for these characters and deletes everything from then on. If missing, the default is 10 lines. • reply() creates a new mail object suitable for replying to $mail. Since this a under program control, “auto-reply” is the most frequent usage of this method.

  22. send a copy to you your_local_email_address | /usr/local/bin/autoreply.pl pipe the email to the autoreply program Using Auto-reply: • Start by making a .forward file in your home directory

  23. Mail Autoreply program retrieves a list of stuff from /etc/passwd #!/usr/bin/perl # file: autoreply.pl # Figure 7.4: An autoreply program use strict; use Mail::Internet; use constant HOME => (getpwuid($<))[7]; use constant USER => (getpwuid($<))[0]; use constant MSGFILE => HOME . "/.vacation"; use constant SIGFILE => HOME . "/.signature"; exit 0 unless -e MSGFILE; exit 0 unless my $msg = Mail::Internet->new(\*STDIN); my $header = $msg->head; # no reply unless message is directed To: us my $myname = USER; exit 0 unless $header->get('To') =~ /$myname/; # no reply if message is marked as "Bulk" exit 0 if $header->get('Precedence') =~ /bulk/i; $< is your real userID remember, we are piping the email to here

  24. Mail Autoreply program (2): # no reply if the From line contains Daemon, Postmaster, Root or ourselves exit 0 if $header->get('From') =~ /subsystem|daemon|postmaster|root|$myname/i; # no reply if the Subject line is "returned mail" exit 0 if $header->get('Subject') =~ /(returned|bounced) mail/i; # OK, we can generate the reply now my $reply = $msg->reply; # Open the message file for the reply open (V,MSGFILE) or die "Can't open message file: $!"; # Prepend the reply message lines my $body = $reply->body; unshift (@$body,<V>,"\n"); # add the signature $reply->add_signature(SIGFILE); # send the mail out $reply->send or die; remember, shift and unshift act like pop and push but at the beginning of a list

  25. Mail::Mailer • used internally by Mail::Internet to send mail • an alternative to Mail::Internet; easier to use hash_ref or anonymous hash use Mail::Mailer; my $mailer = Mail::Mailer->new(); $mailer->open({To => ‘pletcha@newpaltz.edu’, From => ‘pletcha@newpaltz.edu’, CC => ‘pletcha@newpaltz.edu’, Subject => ‘hello there’} ); printf $mailer “This is a simple message.\n”; printf $mailer “Don’t get excited.\n”; printf $mailer “Regards, me\n”; $mailer->close();

  26. Method Description mail Use unix mail or mailx sendmail Use unix sendmail smtp Use Net::SMTP test prints rather than mails Mail::Mailer $mailer = Mail::Mailer->new([$method][,@args]; $fh = $mailer->open(\%header_hash); $mailer->close(); • new() creates a new object. $method indicates how to send the mail; @args is additional arguments. • When using mail, sendmail or test, the call to open() forks and opens a pipe to the specified mailer program. • When smtp is used, a filehandle is returned and this handle is identifiable with the $mailer object. • close() tidies up and sends the mail. Don’t use perl’s close() method.

  27. More Mail::Mailer • What if you want to send the mail to a list of addressees? $mailer->open({To => [‘pletcha@newpaltz.edu’,’pletcha@cs.newpaltz.edu’] From => ‘pletcha@newpaltz.edu’, CC => ‘pletcha@newpaltz.edu’, Subject => ‘hello there’} );

  28. MIME-Tools: • Multipurpose Internet Mail Extensions (MIME); • Used to describe mail contents. • Three basic extensions: • Every message body has a type • Every message body has an encoding • Any message may have multiple parts type/subtype nomenclature; message type is for emails application type is for otherwise undefined encoding used to be 7-bit ascii. But binary data must be encoded as text or SMTP will break. Transfer-encoding is a header field for messages with multiple parts; emails with same message in multiple formats (multipart/alternative) email with picture attachment (multipart/mixed)

  29. audio/* audio/basic audio/mpeg audio/midi audio/x-aiff audio/x-wav image/* image/gif image/jpeg image/png image/tiff message/* message/news message/rfc822 multipart/* multipart/alternative multipart/mixed A sound Sun Microsystem’s “au” format an MP3 file A MIDI file AIFF sound format Microsoft’s “wav” format An image Compuserv GIF format JPEG format Portable network graphics format TIFF format an e-mail message Usenet news message format Internet e-mail message format A message containing multiple parts The same info in alternative forms Unrelated pieces of info mixed together Common MIME Types:

  30. Common MIME Types: text/* texl/html text/plane text/richtext text/tab-separated-values text/xsl text/xml video/* video/mpeg video/quicktime video/msvideo application/* application/msword application/news-message-id application/octet-stream application/postscript application/rtf application/wordperfect5.1 application/gzip application/zip application/xml application/x-tar Human-readable text HTML plain text Enriched text in RFC 1532 format tables xml  xml transformation format xml document moving video or animation MPEG movie format Quicktime movie format Microsoft “avi” movie format None of the above Microsoft Word format News posting format a raw binary stream PostScript Microsoft rich text format Word Perfect 5.1 format Gzip file compression format PKZip file compression format alternative xml document tar file

  31. Common MIME Types: http://www.petri.co.il/mime_types.htm RFCs 2045, 2046, 2047, 2048, and 2077

  32. more • The Content-Disposition header tells the mail reader how to handle the message. One disposition type is called “attachment” and the reader is supposed to allow this to be saved to disk. Alternatively “inline” tells the reader to try to display the contents directly. • For example, an email message with a jpeg attachment is a multipart/mixed document with two parts.

  33. 7bit 8bit binary quoted-printable base64 body not encoded; body is 7-bit ascii and no line over 1000 characters body not encoded; body is 8-bit ascii and no line over 1000 characters not actually encoded; body is 8-bit characters and may have lines over 1000 characters encoded; text messages in other alphabets and so may contain 8-bit characters; 8-bit characters encoded in 7-bit escape sequences; long lines folded at 72 characters encoding used for arbitrary binary data. Every 8-bit character is encoded in a 7-bit character string using uuencode algorithm. Text folded at 72 characters MIME Encodings:

  34. MIME::Entity MIME::Head MIME::Body MIME::Parser This is a MIME message. It contains a MIME::Head and MIME::Body. In multipart messages the body can contain other MIME::Entities, recursively The header part of a MIME message The body part of a MIME message This recursively parses a MIME-encoded message from a file, file handle, memory and returns a MIME::entity. You can extract parts., display, re-mail, etc. MIME Module Organization:

  35. An example #!/usr/bin/perl # Figure 7.6: Sending an audio attachment with MIME tools # file: simple_mime.pl use strict; use MIME::Entity; # create top-level entity my $msg = MIME::Entity->build(From => 'lstein@lsjs.org', To => 'jdoe@acme.org', Subject => 'Greetings!', Type => 'multipart/mixed'); # attach a message my $greeting = <<END; Hi John, Here is a little something for you to listen to. Enjoy! L END

  36. An example (continued) $msg->attach(Type => 'text/plain', Encoding => '7bit', Data => $greeting); # attach the audio file $msg->attach(Type => 'audio/wav', Encoding => 'base64', Description => 'Picard saying "You will be assimilated"', Path => "$ENV{HOME}/News/sounds/assimilated.wav"); # attach signature $msg->sign(File=>"$ENV{HOME}/.signature"); # and send it off using "smtp" $msg->send('smtp');

More Related