GT::Mail - A simple interface to parsing, sending, and creating email.
use GT::Mail; # Create and Sending GT::Mail->send( smtp => 'gossamer-threads.com', smtp_port => 110, # optional; 110/465 (normal/SSL) will be used for the default smtp_ssl => 1, # establish an SSL connection. Requires Net::SSLeay 1.06 or newer. to => 'scott@gossamer-threads.com', from => 'scott@gossamer-threads.com', subject => 'Hello!!', msg => 'I am a text email' ) or die "Error: $GT::Mail::error"; # Parsing and sending my $mail = GT::Mail->new(debug => 1);
# Parse an email that is in a file called mail.test my $parser = $mail->parse('mail.test') or die "Error: $GT::Mail::error"; # Change who it is to $parser->set("to", 'scott@gossamer-threads.com');
# Add an attachment to it $mail->attach ( type => 'text/plain', encoding => '-guess', body_path => 'Mail.pm', filename => 'Mail.pm' ); # Send the email we just parsed and modified $mail->send(sendmail => '/usr/sbin/sendmail') or die "Error: $GT::Mail::error";
GT::Mail is a simple interface for parsing, creating, and sending email. It uses GT::Mail::Send to send email and GT::Mail::Parse to parse and store email data structurs. All the creation work is done from within GT::Mail.
The arguments to new()
in GT::Mail are mostly the same for all the class
methods in GT::Mail so I will be refering back to these further down. Mostly
these arguments are used to set parts of the header for creating an email. The
arguments can be passed in as either a hash or a hash ref. Any arguments aside
from these will be added to the content header as raw header fields. The
following is a list of the keys and a brief description.
my $parser = $mail->parser; $mail->parser($parser);
Set or get method for the parser object that is used when you call parse_head()
or parse(). This object must conform to the method parse and parse_head. If no
object is passed to this method a the GT::Mail::Parse manpage object is created when
needed.
Instance method that returns a parts object. Emails are stored recursivly in parts object. That is emails can have parts within parts within parts etc.. See the GT::Mail::Parts manpage for details on the methods supported by the parts object that is returned.
The parse()
method takes only one argument. It can be a GLOB ref to a file
handle, a FileHandle object, or the path to a file. In any case the IO must
contain a valid formated email.
Once an email is parsed, you can make changes to it as you need and call the send method to send it or call the write method to write it to file, etc.
This method will return false if an error occurs when parsing. The error message will be set in $GT::Mail::error.
This method does the exact same thing as the parse method but it will only parse the top level header of the email. Any IO's will be reset after the parsing.
Use this method if whether you want to parse and decode the body of the email depends on what is in the header of the email or if you only need access to the header. None of the parts will contain a body.
Class/Instance method for sending email. It sends the currently in memory email. This means, if you parse an email, that email is in memory, if you specify params for an email to new(), that is the email that gets sent. You can also specify the params for the email to this method.
Instance method to set or get the top level part. If you are setting this, the object must be from the GT::Mail::Parts manpage. You can use this to retrieve the part object after you specify params to create an email. This object will contain all the other parts for the email. e.g. attachments and emails that are attached. See the GT::Mail::Parts manpage for more details on this object.
Instance method to get a new part object. This method takes the same arguments
as the new()
constructor. Returns the new part object. The part object is
added to the current email only if arguments are given otherwize just returns
an empty part.
Instance method to attach to the in memory email. You can pass in a GT::Mail
object or you can pass the same arguments you would pass to new()
to specify
all the information about the attachment. In addition if you specify a file
path and do not specify a mime type, this will attempt to guess the mime type
from the file extention.
Returns the entire email as a string. Do not use this function if you have attachments and are worried about memory ussage.
Same as to_string.
Instance method that builds the currently in memory email. This method takes one argument, a code ref. It calles the code ref with one argument. The code ref is called for each section of the email that is created. A good example of how to use this is what the as_string method does:
my $ret = ''; $obj->build_email(sub { $ret .= $_[0] });
This puts the entire created email into the string $ret. You can use this, for
example to print the email to a filehandle (which is what the write()
method
does).
Instance mothod that writes the currently in memory email to a file or file handle. The only arguments this method takes is a file or a reference to a glob that is a filehandle or FileHandle object.
Instance method to specify a naming scheme for parsing emails. Calling this after the email is parsed has no effect. This method just wraps to the one in the GT::Mail::Parse manpage.
Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved. http://www.gossamer-threads.com/
Revision: $Id: Mail.pm,v 1.70 2004/11/04 20:23:09 brewt Exp $