Google Answers Logo
View Question
 
Q: Perl script question ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Perl script question
Category: Computers > Programming
Asked by: marcfest-ga
List Price: $25.00
Posted: 02 Aug 2005 16:57 PDT
Expires: 01 Sep 2005 16:57 PDT
Question ID: 551014
Please take a look at the perl script posted at www.2q.com/script.
It's supposed to take the content at
http://www.2q.com/serve.cgi?umlaut and email it to the specified
address. However, the umlauts in the text are not being transmitted
correctly so that "die Einführung der europäischen
Gemeinschaftswährung" in the source text becomes "die Einführung der
europäischen Gemeinschaftswährung" in the email. Please make the
changes to script so that umlauts will be preserved. Thank you.
Answer  
Subject: Re: Perl script question
Answered By: palitoy-ga on 03 Aug 2005 05:35 PDT
Rated:5 out of 5 stars
 
Hello marcfest-ga

I have added a few lines to your code which should work on all systems
(when I tried olebakk-ga's solution it did not work for me).

The problem you are having is due to the character set being used.  I
have changed this to UTF-8 in your mailing sub-routine.

Additionally, once the page has been scraped the scraped characters
are converted to UTF-8.

Let me know if this is still causing problems.

#!/usr/bin/perl

use LWP::Simple;
use Encode;
$content = get("http://www.2q.com/serve.cgi?umlaut");

my $encoding = find_encoding("utf-8");
my $content = $encoding->decode($content);

$email_from= "marcfest\@gmail.com";
$email_subject = "test";
$textonly_version = $content;

&smartsend($email_from, "concierge\@marcfest.com", "Concierge:
$email_subject", $textonly_version, $content);

print "done";


##############################


sub smartsend {
my ($recipient, $sender, $subject, $textonly, $richhtml)=@_;
my $sendmail = "/usr/sbin/sendmail -f noreply.com -t";
my $email = <
From: $sender
To: $recipient
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="_jkkdsffds32432dlkjifewks_"
Subject: $subject

--_jkkdsffds32432dlkjifewks_
Content-Type: text/plain; charset="utf-8"

$textonly

--_jkkdsffds32432dlkjifewks_
Content-Type: text/html; charset="utf-8"

$richhtml
--_jkkdsffds32432dlkjifewks_--
THE_EMAIL


open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $email;
close(SENDMAIL);
}

######################
marcfest-ga rated this answer:5 out of 5 stars
Solutions works very well. Thank you.

Comments  
Subject: Re: Perl script question
From: olebakk-ga on 03 Aug 2005 04:57 PDT
 
Try the following script. It works for me, but in my experience all
Unicode stuff depends on what perl version you are working with.

---

#!/usr/bin/perl

use Unicode::String qw(latin1 utf8);
use LWP::Simple;
$content = get("http://www.2q.com/serve.cgi?umlaut");

$email_from= "marcfest\@gmail.com";
$email_subject = "test";
$textonly_version = $content;

&smartsend($email_from, "concierge\@marcfest.com", "Concierge:
$email_subject", $textonly_version, $content);

print "done";


##############################


sub smartsend {
my ($recipient, $sender, $subject, $textonly, $richhtml)=@_;
my $sendmail = "/usr/sbin/sendmail -f noreply.com -t";
my $email = "From: " . $sender . "\n";

$email .= "To: " . $recipient . "\n";
$email .= "MIME-Version: 1.0\n";
$email .= "Content-Type: multipart/alternative;
boundary=\"_jkkdsffds32432dlkjifewks_\"\n";
$email .= "Subject: " . $subject . "\n\n";
$email .= "--_jkkdsffds32432dlkjifewks_\n";
$email .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n\n" .
utf8($textonly)->latin1 . "\n\n";
$email .= "--_jkkdsffds32432dlkjifewks_\n";
$email .= "Content-Type: text/html; charset=\"iso-8859-1\"\n\n" .
utf8($richhtml)->latin1 . "\n";
$email .= "--_jkkdsffds32432dlkjifewks_--\nTHE_EMAIL\n";

open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; 
print $email;
close(SENDMAIL); 
}

######################
Subject: Re: Perl script question
From: olebakk-ga on 03 Aug 2005 06:24 PDT
 
I had a slight typo in the script:

print $email;

should of course be:

print SENDMAIL $email;

That said - both the solution palitoy-ga ended up with and mine should work... :)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy