Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

SMTP send time

by glenn (Scribe)
on Jul 01, 2014 at 22:13 UTC ( #1091912=perlquestion: print w/replies, xml ) Need Help??

glenn has asked for the wisdom of the Perl Monks concerning the following question:

Perhaps one of you has seen this and can help. I'm using Net::SMTP to send emails and loving it; except for one thing which i'm getting grief from one user about. The send time is wrong. It's running on a windows client, and could run on just about any version of windows. The SMTP example code shows the timezone as UTC, while i'm in EST (which also leads to a DST bonus question). This could potentially also run in India and or Germany, therefore I need to check the host system for the timezone. So because it's available in most versions of windows i'm using wmic to get the time which gives "(UTC-05:00) Eastern Time (US & Canada)" from which i take -0500. But still the send time of the email is an hour after i received it (instant). Any suggestions? I've tried looking at the SMTP spec and it should accept -0500 time format. Thank you.

I posted some extra code in case anyone else is trying to use SMTP and attach files and do email with HTML. This is part of a sub.

Updated with suggestion in case anyone wants this.

my $subject = "Test Complete for station $station_num"; my @addresses = split(",",$emailconfig{"addresses$diagmode"}); for (my $a = 0; $a < @addresses; $a++) { if ($addresses[$a] !~ m/\@/) { $addresses[$a] .= "\@".$emailconfig{domain}; } } my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(); $year += 1900; my $mytimezone = qx"wmic timezone get Description"; #(UTC-05:00) E +astern Time (US & Canada) my $timezone = "EST"; #default foreach my $line (split("\n",$mytimezone)) { if ($line =~ m/\([UG][TM][CT]([+-]\d+:\d+)\)/) { $timezone = $1; $timezone =~ s/://; last; } } print "TIMEZONE: $timezone\n"; #Create an RFC compliant time stamp my $Date = sprintf("%s, %02d %3s %04d %02d:%02d:%02d %5s",$dayofwe +ek[$wday],$mday,$monthnames[$mon],$year,$hour - $isdst,$min,$sec,$tim +ezone); my $smtp = Net::SMTP->new(Host=>$emailconfig{server}, Hello=>$emai +lconfig{domain}, Timeout=>120, Debug=>0); if (not defined $smtp) { croak "Unable to connect to mailhost [$emailconfig{server}]"; } else { $smtp->mail("NOREPLY\"); $smtp->to(@addresses); #start data to server $smtp->data(); #HEADER $smtp->datasend("From: NOREPLY\@$emailconfig{domain}\n"); $smtp->datasend("To: ".join(",",@addresses)."\n"); $smtp->datasend("Reply-To: NOREPLY\@$emailconfig{domain}\n"); $smtp->datasend("Date: $Date\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-Type: multipart/mixed; boundary= \"*B +CKTR*\"\n"); $smtp->datasend("\n"); #end content block #MSG: $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: ".$MIMEtype{html}[0]."; charset +=UTF-8\n"); $smtp->datasend("\n"); #end content block $smtp->datasend("$email_msg"); $smtp->datasend("\n"); #ATTACHMENTS my $path = "$reportconfig{sharedriveletter}:\\$system->{serial +}->[0]\\"; for (my $f = 1; $f < @{$system->{filename}}; $f++) { if (-e "$path$system->{filename}->[$f]") { logLine("attaching file $system->{filename}->[$f]"); $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: ". $MIMEtype{substr($sy +stem->{filename}->[$f],index($system->{filename}->[$f],".") + 1)}[0] +."; name=\"$system->{filename}->[$f]\"\n");#; charset=binary $smtp->datasend("Content-Transfer-Encoding: ". $MIMEty +pe{substr($system->{filename}->[$f],index($system->{filename}->[$f]," +.") + 1)}[1] ."\n"); $smtp->datasend("Content-Disposition: attachment; file +name=\"$system->{filename}->[$f]\"\n"); $smtp->datasend("\n"); #end content block open (ATT, "< $path$system->{filename}->[$f]"); while (my $input = <ATT>) { if ($MIMEtype{substr($system->{filename}->[$f],ind +ex($system->{filename}->[$f],".") + 1)}[1] eq "base64") { $input = encode_base64($input); } $smtp->datasend($input); } close (ATT); $smtp->datasend("\n"); #separate each file with a new +line } } if ($system->{status}->[0] =~ m/fail/i) { $files{log}{file} =~ m/([a-zA-Z0-9\-\._]+)$/; my $logfile = $1; $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: ".$MIMEtype{log}[0]."; name +=\"$logfile\"\n"); $smtp->datasend("Content-Transfer-Encoding: ". $MIMEtype{l +og}[1] ."\n"); $smtp->datasend("Content-Disposition: attachment; filename +=\"$logfile\"\n"); $smtp->datasend("\n"); #end content block open (ATT, "< $files{log}{file}"); while (my $input = <ATT>) { $smtp->datasend($input); } close (ATT); $smtp->datasend("\n"); #separate each file with a new line } # Send the END section break $smtp->datasend("--*BCKTR*--\n\n"); #end data to server $smtp->dataend(); #close connection to server (SEND) $smtp->quit; }

Replies are listed 'Best First'.
Re: SMTP send time
by kennethk (Abbot) on Jul 01, 2014 at 23:25 UTC

    It looks like you've already identified your issue. Right now, it's EDT which is -04:00, not -05:00. It looks like your wmic system call is lying about time zone, and I'll bet that [localtime](8) returns 1, a.k.a. daylight savings. One way to correct your code would be to change your localtime call to

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday, $isdst) = localtime( +);
    and then modify your timestamp code to
    my $Date = sprintf("%s, %02d %3s %04d %02d:%02d:%02d %5s",$dayofweek[$ +wday],$mday,$monthnames[$mon],$year,$hour - $isdst,$min,$sec,$timezon +e);
    Or you can try to figure out how to get a correct answer from wmic

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      damn I wish I had thought of that. Thats good, and simple.
Re: SMTP send time
by wrog (Friar) on Jul 01, 2014 at 23:31 UTC
    apparently "wmic timezone" does not give you the daylight savings time adjustment.

    note that DateTime::TimeZone actually works on Windows (or at least, it does on my box); there may be other packages that do as well

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1091912]
Approved by shmem
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-01-24 12:19 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.