perlfaq

 view release on metacpan or  search on metacpan

lib/perlfaq9.pod  view on Meta::CPAN


  my $message = Email::MIME->new($rfc2822);
  my $subject = $message->header('Subject');
  my $from    = $message->header('From');

If you've already got some other kind of email object, consider passing
it to L<Email::Abstract> and then using its cast method to get an
L<Email::MIME> object:

  my $abstract = Email::Abstract->new($mail_message_object);
  my $email_mime_object = $abstract->cast('Email::MIME');

=head2 How do I check a valid mail address?

(partly contributed by Aaron Sherman)

This isn't as simple a question as it sounds. There are two parts:

a) How do I verify that an email address is correctly formatted?

b) How do I verify that an email address targets a valid recipient?

Without sending mail to the address and seeing whether there's a human
on the other end to answer you, you cannot fully answer part I<b>, but
the L<Email::Valid> module will do both part I<a> and part I<b> as far
as you can in real-time.

Our best advice for verifying a person's mail address is to have them
enter their address twice, just as you normally do to change a
password. This usually weeds out typos. If both versions match, send
mail to that address with a personal message. If you get the message
back and they've followed your directions, you can be reasonably
assured that it's real.

A related strategy that's less open to forgery is to give them a PIN
(personal ID number). Record the address and PIN (best that it be a
random one) for later processing. In the mail you send, include a link to
your site with the PIN included. If the mail bounces, you know it's not
valid. If they don't click on the link, either they forged the address or
(assuming they got the message) following through wasn't important so you
don't need to worry about it.

=head2 How do I decode a MIME/BASE64 string?

The L<MIME::Base64> package handles this as well as the MIME/QP encoding.
Decoding base 64 becomes as simple as:

    use MIME::Base64;
    my $decoded = decode_base64($encoded);

The L<Email::MIME> module can decode base 64-encoded email message parts
transparently so the developer doesn't need to worry about it.

=head2 How do I find the user's mail address?

Ask them for it. There are so many email providers available that it's
unlikely the local system has any idea how to determine a user's email address.

The exception is for organization-specific email (e.g. foo@yourcompany.com)
where policy can be codified in your program. In that case, you could look at
$ENV{USER}, $ENV{LOGNAME}, and getpwuid($<) in scalar context, like so:

  my $user_name = getpwuid($<)

But you still cannot make assumptions about whether this is correct, unless
your policy says it is. You really are best off asking the user.

=head2 How do I send email?

Use the L<Email::Stuffer> module, like so:

  # first, create your message
  my $message = Email::Stuffer->from('you@example.com')
                              ->to('friend@example.com')
                              ->subject('Happy birthday!')
                              ->text_body("Happy birthday to you!\n");

  $message->send_or_die;

By default, L<Email::Sender::Simple> (the C<send> and C<send_or_die> methods
use this under the hood) will try C<sendmail> first, if it exists
in your $PATH. This generally isn't the case. If there's a remote mail
server you use to send mail, consider investigating one of the Transport
classes. At the time of writing, the available transports include:

=over 4

=item L<Email::Sender::Transport::Sendmail>

This is the default. If you can use the L<mail(1)> or L<mailx(1)>
program to send mail from the machine where your code runs, you should
be able to use this.

=item L<Email::Sender::Transport::SMTP>

This transport contacts a remote SMTP server over TCP. It optionally
uses TLS or SSL and can authenticate to the server via SASL.

=back

Telling L<Email::Stuffer> to use your transport is straightforward.

  $message->transport($email_sender_transport_object)->send_or_die;

=head2 How do I use MIME to make an attachment to a mail message?

L<Email::MIME> directly supports multipart messages. L<Email::MIME>
objects themselves are parts and can be attached to other L<Email::MIME>
objects. Consult the L<Email::MIME> documentation for more information,
including all of the supported methods and examples of their use.

L<Email::Stuffer> uses L<Email::MIME> under the hood to construct
messages, and wraps the most common attachment tasks with the simple
C<attach> and C<attach_file> methods.

  Email::Stuffer->to('friend@example.com')
                ->subject('The file')
                ->attach_file('stuff.csv')
                ->send_or_die;

=head2 How do I read email?

Use the L<Email::Folder> module, like so:



( run in 0.440 second using v1.01-cache-2.11-cpan-97f6503c9c8 )