Seitenanfang

Get server-side message-id or server response from Net::SMTP

Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.


Sending important mails leads sooner or later makes you search for your mails throughout the logs of your mailserver. Using the unique mail id assigned by the mail server makes things much easier.

Sending mails with Net::SMTP is pretty easy, but it won't show the server reply (containing the assigned transmission id).

Net::SMTP is able to print out the SMTP dialog to STDERR by enabling the debugging:

Net::SMTP>>> Net::SMTP(2.31)Net::SMTP>>>   Net::Cmd(2.29)Net::SMTP>>>     Exporter(5.62)Net::SMTP>>>   IO::Socket::INET(1.31)Net::SMTP>>>     IO::Socket(1.30_01)Net::SMTP>>>       IO::Handle(1.27)Net::SMTP=GLOB(0x67dc7b0)<<< 220 mail.xxxxx.xx ESMTP Tue, 11 Oct 2011 15:18:15 +0200Net::SMTP=GLOB(0x67dc7b0)>>> EHLO localhost.localdomainNet::SMTP=GLOB(0x67dc7b0)<<< 250-mail.xxxxx.xx Hello xxxxx.xxxxx.xx [192.168.xxx.xxx]Net::SMTP=GLOB(0x67dc7b0)<<< 250-SIZE 209715200Net::SMTP=GLOB(0x67dc7b0)<<< 250-PIPELININGNet::SMTP=GLOB(0x67dc7b0)<<< 250-STARTTLSNet::SMTP=GLOB(0x67dc7b0)<<< 250 HELPNet::SMTP=GLOB(0x67dc7b0)>>> MAIL FROM:<info@xxxxx.xxx>Net::SMTP=GLOB(0x67dc7b0)<<< 250 OKNet::SMTP=GLOB(0x67dc7b0)>>> RCPT TO:<catchall@xxxxx.xxx>Net::SMTP=GLOB(0x67dc7b0)<<< 250 AcceptedNet::SMTP=GLOB(0x67dc7b0)>>> DATANet::SMTP=GLOB(0x67dc7b0)<<< 354 Enter message, ending with "." on a line by itselfNet::SMTP=GLOB(0x67dc7b0)>>> From: info@xxxxx.xxNet::SMTP=GLOB(0x67dc7b0)>>> To: catchall@xxxxx.xxNet::SMTP=GLOB(0x67dc7b0)>>> Subject: =?ISO-8859-1?Q?test?=Net::SMTP=GLOB(0x67dc7b0)>>> MIME-Version: 1.0Net::SMTP=GLOB(0x67dc7b0)>>> Content-Disposition: inlineNet::SMTP=GLOB(0x67dc7b0)>>> Content-Transfer-Encoding: quoted-printableNet::SMTP=GLOB(0x67dc7b0)>>> Content-Type: text/plain;Net::SMTP=GLOB(0x67dc7b0)>>> Date: Tue, 11 Oct 2011 15:18:15 +0200Net::SMTP=GLOB(0x67dc7b0)>>>Net::SMTP=GLOB(0x67dc7b0)>>> test=Net::SMTP=GLOB(0x67dc7b0)>>> .Net::SMTP=GLOB(0x67dc7b0)<<< 250 OK id=1RDcDn-0002Hw-5VNet::SMTP=GLOB(0x67dc7b0)>>> QUITNet::SMTP=GLOB(0x67dc7b0)<<< 221 mail.xxxxx.xx closing connection
The third line from the end contains "id=1RDcDn-0002Hw-5V" which should be the id logged by your mailserver in each and every logfile line for this mail.

But there is no need to redirect STDERR trying to get this id. Your source has to issue a "->dataend" command somewhere and may get the id by calling the ->message method next:

$smtp->dataend()                 || die('SMTP error ' . $@);my $result = $smtp->message;$result =~ s/[\r\n]+$//;$result =~ s/^.+[\r\n]//;$id = $1 if $result =~ /id\=(.+)/;

But $result might be a better choice for logging into a database table or logfile.
 

Noch keine Kommentare. Schreib was dazu

Schreib was dazu

Die folgenden HTML-Tags sind erlaubt:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>