#!/bin/blog

March 13, 2004

Mail mit Attachment versenden

Filed under: Internet, Notizen, perl — Tags: — martin @ 7:58 am

Mime::Lite ist wohl die bequemste Möglichkeit, um aus Perl-Scripts heraus einigermaßen ordentlich MIME-codierte Mails mit Anhang zu versenden.

#!/usr/bin/perl -w
use strict;
use diagnostics;
use MIME::Lite;
use File::Basename;

my $absender    = 'Alice <alice@thisdomain.invalid>';
my $empfaenger  = 'Bob <bob@otherdomain.invalid>';
my $datei       = "/home/alice/info.pdf";
my $smtphost    = "127.0.0.1 25";
my $betreff     = "Demo Dateianhang";

my $anschreiben = <<EOF;
Sehr geehrte Damen und Herren,

anbei die gewünschten Informationen.
EOF

MIME::Lite->send('smtp', $smtphost, Timeout=>5);

my $msg = MIME::Lite->new(
        From            => $absender,
        To              => $empfaenger,
        Subject         => $betreff,
        Type            => 'multipart/mixed'
        );

$msg->attach(
        Type            => 'TEXT',
        Data            => $anschreiben
        );

$msg->attach(
        Type            => 'AUTO',
        Path            => $datei,
        Filename        => basename($datei),
        Disposition     => 'attachment'
        );

$msg->send();

Um eine Mail direkt über den lokalen MTA z.B. per sendmail oder qmail-inject einzuqueuen, würde man MIME::Lite wie folgt konfigurieren:

 
MIME::Lite->send('sendmail', "/usr/lib/sendmail -t -oi "); (Postfix)
MIME::Lite->send('sendmail', "/var/qmail/bin/qmail-inject -f $absender $emfpaenger"); (Qmail)

 
Fehler beim Versand kann man am Rückgabewert der send()-Methode erkennen.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: