Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

UTF-8 Email Form

by oswulf (Initiate)
on Mar 14, 2018 at 09:09 UTC ( [id://1210873]=perlquestion: print w/replies, xml ) Need Help??

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

Does anyone know of a ready-to-use script to back a contact form on a website that will send an Email via SMTP and which supports UTF-8?

NMA FormMail would appear to do this (it has a UTF-8 configuration setting and some associated code) but it simply doesn't work. Foreign language text arrives as gibberish. (And I'm not the only person to report this.)

I'd prefer not to have to write something myself given all the security considerations.

Thanks.

Replies are listed 'Best First'.
Re: UTF-8 Email Form
by hippo (Bishop) on Mar 14, 2018 at 09:37 UTC

    Assuming you mean NMS rather than NMA, then there is a page there with details on reporting problems with the scripts. Did you do this? What was the response? It would probably be best to help fix this in such a widely-used script if it is indeed a problem at that end.

    Foreign language text arrives as gibberish. (And I'm not the only person to report this.)

    Do any of the users report success? Are all the errors limited to those using MS Outlook? Did you check that the message headers regarding encoding are correctly set? All this would be good info.

Re: UTF-8 Email Form
by Anonymous Monk on Mar 14, 2018 at 13:11 UTC
    You'll need to look at the actual HTML content of the e-mails that it is sending – does the header specify UTF-8 encoding? There should be some kind of template somewhere into which the body of the message will be inserted for rendering, and that template should specify a complete HTML package including the "head" tag where the encoding spec should be. Next, you must also verify that your e-mail client understands this tag and can, in fact, render UTF-8 characters. Some of them are smart; some of them are stupid.
Re: UTF-8 Email Form
by Anonymous Monk on Mar 14, 2018 at 15:02 UTC
    The basic functionality that the FormMail package must provide is to send an e-mail that is packaged with both an HTML and a non-HTML version of the same content, which you appear to be saying that it is correctly doing. But perhaps the configuration setting isn't inserting the proper header into the HTML, to tell the recipient to expect UTF-8. (Just as web pages should do. Same thing.) And, pragmatically speaking, it just might be simplest to insert that yourself – e.g. using a Template. You need to be sending, in that portion of the message, a complete HTML package including both header and body portions ... after verifying that your mail client isn't the actual culprit by virtue of being clueless. If you can get it to hand you the body of the message ... or telnet into the mail server and get the message for yourself ... you should immediately be able to see if the encoding-spec is present or not in what the mail server is sending you.
Re: UTF-8 Email Form
by Polyglot (Chaplain) on Mar 18, 2018 at 16:10 UTC

    The ubiquitous "sendmail" handles UTF8 just fine. A very simple subroutine I have used to handle a specific form in Thai is presented below. (NOTE: It appears that perlmonks.org converts all UTF8 characters to HTML-entitites, even in <code></code> blocks, which is not the way I used it, so I have had to format my code in HTML style for proper readability.)

     


    sub emailFormResults {
     
    # Email the form results
     
    open ( MAIL, "| /usr/lib/sendmail -t" );
    #open ( MAIL, "| /usr/lib/sendmail", "-oi", "-t");
    print MAIL "From: $sending_email_address\n";
    print MAIL "To: $recipient_address\n";
    print MAIL "BCC: $bcc_address\n";
    print MAIL "Subject: $message_subject\n\n";
    print MAIL <<END_OF_MESSAGE;
    1. Personal Information / ข้อมูลส่วนตัว
    * National ID / หมายเลขประจำตัว(ตามบัตรประจำตัวประชาชน): $national_id
    * Title (นาย/นางสาว/นาง/ยศ): $title
    * Full Name / ชื่อ-สกุล: $fullname
    * Maiden Name / ชื่อ-สกุลเดิม(ถ้ามี): $maiden_name
    * Birthday / วดป.เกิด: $birthday
    * Current Address / ที่อยู่ปัจจุบัน บ้านเลขที่: $current_address
    * Home Phone / หมายเลขโทรศัพท์บ้าน: $home_phone
    * Cell Phone / มือถือ: $cell_phone
    * Email: $email_address
    * LINE Id: $line_id
    * Facebook: $facebook
     
    2. Occupational Information / ข้อมูลการประกอบอาชีพ ปัจจุบันประกอบอาชีพ: $occupation
    * Position / ตำแหน่ง: $position
    * Company / ชื่อ สถานที่ทำงาน: $company
    * Salary / รายได้ต่อเดือน: $salary บาท
     
    ฝากข้อความ: $comments
     
    END_OF_MESSAGE
     
     
    print MAIL "\n.\n";
    close ( MAIL );
    } # END SUB emailFormResults

     
    Note that the form itself should be set to accept only UTF-8, and the HTML for the page should be properly set as well. I have used the following pieces successfully.


    print <<HTML;
     
    <html lang="th">
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>ใบสมัครสมาชิก</title>
    . . .
    </head>
    <body>
    <form name="myform" id="myform" action="$0" method="post" accept-charset="UTF-8">
     
    HTML
     
     

    Blessings,

    ~Polyglot~

      I don't use sendmail directly like this but I'm pretty sure it will fail on the Header fields (To, Cc, Subject, etc) when they contain extra-ASCII. For that you can use Encode/Encode::MIME::Header.

      use Encode; my $subject = encode("MIME-Header", $some_utf8_string);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1210873]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-03-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found