#!/usr/bin/perl use strict; use warnings; use MIME::Base64 qw(encode_base64); my $debug=0; my $mailprog ='/usr/sbin/sendmail'; my $sep_attachement='mail_attachement==============='; my $sep_html='plain_html==============='; my $sep_images='html_images==============='; my $tmp_dir='/tmp/'; my $body="body.html"; my @images=( ['image001.gif','image/gif'], ['image002.gif','image/gif'], ); my $to=$ARGV[0]; my $from=$ARGV[1]; my $subject="$ARGV[2] $ARGV[3] $ARGV[4]"; my $unknown=$ARGV[5]; my $attachement=$ARGV[6]; my $user_dir=$ARGV[7]; die(qq(NO USER DIR! "$user_dir")) unless(-d $user_dir); # mail erzeugen... my $mail=''; # Mail Header $mail.="To: $to <$to\@yxz.de>\n"; $mail.="From: $from \n"; $mail.="Reply-to: muster\@xyz\n"; $mail.="Subject: $subject\n"; $mail.="MIME-Version: 1.0\n"; # Trennerspezifikation für Attachements $mail.="Content-Type: multipart/mixed;\n"; $mail.="\tboundary=$sep_attachement\n\n"; # Hinweis, wenn kein MIME-Standard unterstützt wird. $mail.="This is a multi-part message in MIME format.\n"; $mail.="\n\n"; # Abgrenzung der Email vom Header $mail.="--$sep_attachement\n"; # Trennerspezifikation Plaint Text mit HTML als Alternative $mail.="Content-Type: multipart/alternative;\n"; $mail.="\tboundary=$sep_html\n"; # Abgrenzung der PLAIN von HTML #$mail.="--$sep_html\n"; #$mail.="Content-Type: text/plain; charset=us-ascii\n"; #$mail.="Content-Transfer-Encoding: quoted-printable\n\n"; #$mail.="Alternative als Plain Text\n"; # neuer Content-Type sobald sich ein Bild im HTMLTEIL befindet $mail.="--$sep_html\n"; $mail.="Content-Type: multipart/related;\n"; $mail.="\tboundary=$sep_images\n"; # Abgrenzung der HTMLQUELLCODE vom Bild $mail.="--$sep_images\n"; $mail.="Content-Type: text/html; charset=us-ascii\n\n"; $mail.=read_file($user_dir.$tmp_dir.$body); $mail.="\n"; # Einbinden der Bilder $mail.=read_image($user_dir.$tmp_dir,@$_,$sep_images) for(@images); $mail.="$unknown\n"; # Abgrenzung der HTMLQUELLCODE vom Bild schließen $mail.="--$sep_images--\n"; # Abgrenzung der PLAINTEXT vom HTMLTEIL schließen $mail.="--$sep_html--\n"; # Abgrenzung des TEXTTEIL vom ATTACHMENT $mail.="--$sep_attachement\n"; $mail.=qq(Content-Type: application/octet-stream; name="$attachement"\n); $mail.="Content-ID: <$attachement>\n"; $mail.="Content-Transfer-Encoding: 7bit\n"; $mail.=qq(Content-Disposition: attachement; filename="$attachement"\n); $mail.=read_file($user_dir.$tmp_dir.$attachement); # Abgrenzung des TEXTTEIL vom ATTACHMENT schließen $mail.="--$sep_attachement--\n"; if($debug) { print $mail; exit; } # mail versenden ... open(my $mail_out, "|$mailprog -t") || die "Can't open $mailprog!\n)"; print $mail_out $mail; close($mail_out); ######################################################################## sub read_file { my $file=shift; if(open(my $fh, '<', $file)) { binmode($fh); local $/=undef; return <$fh>; } die qq[ERROR open "$file" ($!)\n]; } sub read_image { my $base=shift; my $file=shift; my $type=shift; my $sep=shift; my $txt=''; $txt.="--$sep\n" if($sep); $txt.="Content-Type: $type\n"; $txt.="Content-ID: <$file>\n"; $txt.="Content-Transfer-Encoding: base64\n"; $txt.=qq(Content-Disposition: inline; filename="$file"\n); my $buf=read_file($base.$file); ### Kodierung auf base64 Basis $txt.=encode_base64($buf); $txt.="\n"; return $txt; }