attach ical .ics in a cgi-Skript

Alles rund um Perl / Cgi
Antworten
marek
Beiträge: 5
Registriert: 27.05.2012 13:12

attach ical .ics in a cgi-Skript

Beitrag von marek »

Hallo an alle!


Dies ist meine erste Frage in diesem Forum. Ich habe eine Bestellseite für einen Taxi-Freund programmiert. Ich möchte an die Email eine Datei mit einer .ics Datei anhängen, sodass mein Kollege nur noch in seinem iPhone darauf klicken muss, um den neuen Termin in seinem iCalendar einzufügen.

Die Server-Konifguration lässt sich nur mit großer Überredung ändern (ich glaube Module wie MIME::Lite oder Mail::Sendmail sind nicht installiert???). Daher habe ich versucht, ein Shell-Befehl einzubinden:

my $command = $mailprog; # $mailprog = /usr/sbin/sendmail -t -i
$command = "uuencode $ical_data | $command";
# uuencode $ical_data | /usr/sbin/sendmail -t -i
$command .= qq{ -f "$postmaster"} if $postmaster;
# uuencode $ical_data | /usr/sbin/sendmail -t -i -f "postmaster@mydomain.org"
my $result;
eval {
local $SIG{__DIE__};
$result = open SENDMAIL, "| $command";
};

Die iCal Daten sind nicht in einer Datei gesichert. Ich habe auch versucht den Dateinamen für die Daten in $ical_data anzugeben, ohne dass die Datei existiert:

$command = "uuencode $ical_data $ical_file | $command"; # $ical_file soll der Datei-Name sein

Muss ich erst die Datei in /tmp sichern. Oder muss der Befehl in Backticks stehen?

Das Skript meldet:

close sendmail pipe failed, mailprog=[/usr/sbin/sendmail -t -i] : at order_de.cgi line 1390.

Danke für Eure Hilfe


marek
marek
Beiträge: 5
Registriert: 27.05.2012 13:12

Re: attach ical .ics in a cgi-Skript

Beitrag von marek »

Hallo an Alle!


Schade, dass niemand eine Lösung weiß. Darf ich hier ein 191 Zeilen Skript posten? Das ist das kleinste Test-Format, dass ich schaffe.

Ich bekomme leider immer noch die Fehlermeldung:

sendmail: fatal: mstep@podiuminternational.org(501): No recipient addresses found in message header
close sendmail pipe failed, mailprog=[/usr/sbin/sendmail -t -oi] : at ./mail_test.pl line 163.

Und ich kann den Fehler nicht finden.

Meine ursprüngliche Idee, den sendmail-Befehl so zu ändern, dass uuencode die Datei anhängt habe ich inzwischen aufgegeben. Im Kern füge ich die Datei per Hand ein:

foreach my $to (@recipients) {
email_start( $postmaster, $to );
email_data(<<EOMAIL);
X-HTTP-Client: [$addr]
MIME-Version: 1.0
Content-Type = "multipart/mixed; boundary=\"$boundary\"";
Content-Transfer-Encoding: 8bit
To: "Mare" <$to>
Reply-to: $reply
From: $from
Subject: $subject
--$boundary
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
$body
--$boundary

Content-Type: text/calendar
Content-Disposition: attachment; filename="$ical_file_name"
Content-Transfer-Encoding: base64
$ical_data
--$boundary--


EOMAIL
email_end();
}

Wenn mir jemand grünes Licht geben könnte, dass ich das komplette Testskript hier posten darf ... Ich brauche dringend Hilfe.

Danke! marek
marek
Beiträge: 5
Registriert: 27.05.2012 13:12

Re: attach ical .ics in a cgi-Skript

Beitrag von marek »

Uff! Endlich bin ich auf der richtigen Spur! Nach eingehenden Tests, habe ich festgestellt, dass sendmail empfindlich reagiert, wenn vor dem "To:" - Header andere Header stehen. Am besten ist, diesen Empfänger-Header an die erste Stelle zu setzen.

Und noch ein Tipp: die anderen Header müssen sehr sorgfältig eingefügt werden, weil sonst sendmail durcheinander kommt. Am besten an sich selbst eine Mail schicken, mit einem Mail-Client (also nicht über die Shell und sendmail) und genau dem Typ der angehängten Datei, die auch in dem Skript verwendet werden soll. In meinem Fall war das ein Termin für den iCal (Apple Calendar). Von dem Quellcode kann man dann genau die Header übertragen, die für die Mail gebraucht werden:

Mail-Headers --Boundary Body-Headers Body --Boundary Attached-File-Headers --Boundary--

Ich werde hier noch einmal zurückkommen und die Headers meiner überarbeiteten Version hier posten.


Schade dass mir niemand helfen konnte! Ich habe viel Zeit investiert, aber auch viel gelernt.


marek
marek
Beiträge: 5
Registriert: 27.05.2012 13:12

Re: attach ical .ics in a cgi-Skript

Beitrag von marek »

Hallo alle!

Da steh ich nun, ich armer Tor! Und bin so klug als wie zuvor.
Leider habe ich mich geirrt. Und ich brauch trotzdem eure Hilfe. Warum funktioniert Skript 1 ? Und Skript 2 sendet nichts, macht aber auch keine Fehlermeldungen. Kann mir jemand verraten wie ich im Debugger den Inhalt von einem FILEHANDLE auslesen kann? Das wäre sehr hilfreich. Ich habe es so versucht: > x <SENDMAIL> Aber Antwort ist immer "empty Array" ...

Hier die Skripte. Das zweite brauche ich :-)

***Skript 1***

#!/usr/bin/perl

# Aus: http://wiki.perl-community.de/Wissensba ... ttachments
# funzt!


use strict;
use warnings;
use MIME::Base64;

my $file_path = "/Users/you/Documents/webpages/cgi-bin/tmp";
my $file_name = "ical_summary.ics";

open(M, "|/usr/sbin/sendmail -t -oi") or die "Can't open mailprogram: '/usr/sbin/sendmail'!\n$!";
print M "MIME-Version: 1.0\n";
print M qq(To: Your Name <you\@yourdomain.com>\n);
print M qq(From: Your Name <you\@yourdomain.com>\n);
print M qq(Reply-to: Your Name <you\@yourdomain.com>\n);
print M "Subject: E-Mail - the corrected version!\n";
my $boundary = "==========".substr(pack('u', ('E-Mail'.'Your Name')), 0, 24);
print M qq(Content-type: multipart/mixed; boundary="$boundary"\n);
print M qq(--$boundary\nContent-Type: text/plain; charset="UTF-8"\nContent-Transfer-Encoding: 7bit\n\n);

print M qq(\nUnd hier kommt ein Test jeflogen!\n);

print M "\n--$boundary\n";
print M qq(Content-type: application/octet-stream; name="$file_name"\n);
print M "Content-Transfer-Encoding: base64\n";
print M qq(Content-Disposition: attachment;\nContent-Type: text/calendar filename="$file_name"\nContent-Transfer-Encoding: base64\n);
open(F, "$file_path/$file_name") or die "Can't open data: '$file_path/$file_name'!\n$!";
my $data;

{
binmode F;
local $/;
$data = <F>;
}
close(F) or die "Can't close: '$file_path/$file_name'! $!\n";
my $codiert = MIME::Base64::encode($data);
print M "\n$codiert\n";
print M qq(\n--$boundary--\n);
close(M) or die "Can't close the filehandle <M>: $!";

__END__


***Skript 2*****

#! /usr/bin/perl

use strict;
use warnings;

use MIME::Base64;

my $mailprog = '/usr/sbin/sendmail -t -oi';
my $postmaster = 'you@yourdomain.com';
my @recipients = ( 'info@anothedomain.de', 'marek@yourdomain.com' );

my $ical_data_start = "20120601T210000";
my $ical_data_end = "20120601T220000";
my $ical_summary = "Dinner";
my $ical_location = "Hörwartstr";
my $ical_file_name = $ical_summary . ".ics";

my $ical_data = <<"EOICAL";
BEGIN:VCALENDAR
BEGIN:VEVENT
DTEND;TZID=Europe/Berlin:$ical_data_end
SUMMARY:$ical_summary
DTSTART;TZID=Europe/Berlin:$ical_data_start
DTSTAMP:20120521T190638Z
LOCATION:$ical_location
SEQUENCE:0
BEGIN:VALARM
TRIGGER:-PT1H
DESCRIPTION:Event reminder
ACTION:DISPLAY
END:VALARM
END:VEVENT
END:VCALENDAR
EOICAL

$ical_data = encode_base64($ical_data);

my $reply = '"Your Name" <you@yourdomain.com>';
my $from = $reply;
my $subject = "Abholung vom 1.6.2012, 21:00";
my $body = "This is a test :-) Corrected Version! You really have to pay attention to the line endings between the different headers!\n\tda maaki";
my $boundary = "=====" . time() . "=====";

foreach my $to (@recipients) {
my $result;
eval {
local $SIG{__DIE__};
$result = open SENDMAIL, "| $mailprog";
};
if ($@) {
die $@ unless $@ =~ /Insecure directory/;
delete $ENV{PATH};
$result = open SENDMAIL, "| $mailprog";
}

die "Can't open mailprog [$mailprog]\n" unless $result;

my $data = <<"EOMAIL";
MIME-Version: 1.0
From: $from
To: $to
Reply-to: $reply
Subject: $subject
Content-Type = multipart/mixed; boundary="$boundary"

--$boundary
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

$body

--$boundary
Content-Disposition: attachment;
Content-Type: text/calendar filename="$ical_file_name"
Content-Transfer-Encoding: base64

$ical_data

--$boundary--
EOMAIL

print SENDMAIL $data or die "write to sendmail pipe: $!";
close SENDMAIL or die "Closing of SENDMAIL failed: $!";
}



__END__
marek
Beiträge: 5
Registriert: 27.05.2012 13:12

Re: attach ical .ics in a cgi-Skript

Beitrag von marek »

Ich habe meine Frage in perl.beginners.cgi gestellt. Trotzdem Danke an alle, die wenigstens meine Frage gelesen haben (50 wenn ich mich recht erinnere).

Grüße aus München


marek
Antworten