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

Hi Guru,

I am wondering is there any illegal characters that I need to worried about or do something when I'm using sendmail to delivery an email?

In particular when/IF there is any illegal characters inside variable like $to_email, $from_email, $subject.

my $mailprogram = "/usr/sbin/sendmail -oi -t"; open (MAIL, "|$mailprogram") || ($errmsg = "Error $!"); print MAIL "To: $to_email\n"; print MAIL "From: $from_email\n"; print MAIL "Subject: $subject\n";

The $from_email may contain value like "Someone Name <$form_email>". The name is obtain from Form, therefore should I do any filter?

Replies are listed 'Best First'.
Re: sendmail - worried illegal chars
by atcroft (Abbot) on Apr 05, 2016 at 21:02 UTC

    According to RFC 5321 - Simple Mail Transport Protocol and RFC 5322 - Internet Message Protocol, "unextended SMTP transport provides 7-bit transfer only". In fact,

    A message that is conformant with this specification is composed of characters with values in the range of 1 through 127 and interpreted as US-ASCII (ANSI.X3-4.1986) characters. For brevity, this document sometimes refers to this range of characters as simply "US-ASCII characters". Note: This document specifies that messages are made up of characters in the US-ASCII range of 1 through 127. There are other documents, specifically the MIME document series (RFC2045, RFC2046, RFC2047, RFC2049, RFC4288, RFC4289), that extend this specification to allow for values outside of that range.

    To be safe, I would keep that in mind.

    Hope that helps.

Re: sendmail - worried illegal chars
by haukex (Archbishop) on Apr 05, 2016 at 21:39 UTC

    Hi hankcoder,

    I'd recommend you leave the work of creating and sending an email to a module. Email::Sender (with Email::Simple) is just one of many. Something like this (based on Email::Sender::Manual::QuickStart):

    use warnings; use strict; use Email::Sender::Simple qw/sendmail/; use Email::Simple; my $email = Email::Simple->create( header => [ To => '"Xavier Q. Ample" <x.ample@example.com>', From => '"Bob Fishman" <orz@example.mil>', Subject => "don't forget to *enjoy the sauce*", ], body => "This message is short, but at least it's cheap.\n", ); print $email->as_string; # debug sendmail($email);

    Hope this helps,
    -- Hauke D

Re: sendmail - worried illegal chars
by stevieb (Canon) on Apr 05, 2016 at 21:09 UTC

    Use Email::Valid for this:

    use warnings; use strict; use Email::Valid; my $from_email = 'stevieb <steveb@cpan.org>'; if (! Email::Valid->address($from_email)){ die "that ain't a valid email address!\n"; } ...;
        [mod://Email::Valid]Email::Valid

        or

        [metamod://Email::Valid]Email::Valid

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: sendmail - worried illegal chars
by hankcoder (Scribe) on Apr 07, 2016 at 13:54 UTC

    Thank you all very much for the comments. Guess it is time for me to upgrade to Email::Valid instead of "sendmail"