Thread Probleme mit dem Mailversand nach Umstieg auf Perl 5.10 (26 answers)
Opened by Michael at 2010-12-16 12:07

topeg
 2010-12-16 16:15
#143639 #143639
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ein paar Sachen sind mir aufgefallen:

Das HTML wird in "$ARGV[7]/tmp/body.html" gesucht, die Bilder aber in "$ARGV[7]/image001.gif". Ist das so richtig? Da alles andere in "$ARGV[7]/tmp/" liegt halte ich es für wahrscheinlich, dass die Bilder auch dort liegen.

Was steht in "$ARGV[5]"? das kann unter Umständen eines der Bilder kaputt machen.

Das ganze etwas anders geschrieben, so sollte auch für andere verständlicher sein:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/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 <noreplay\@xyz.de>\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;
}

View full thread Probleme mit dem Mailversand nach Umstieg auf Perl 5.10