I referred to serhio for the utf-8 encoding problem and hv been trying for the whole day different methods searched from net :( I want to show the chinese characters in subject lines but when received in gmail it shows rubbish characters. I had tried to put
header('Content-Type: text/html; charset=utf-8');
on top of page but not working
i tried to add "\r\n" also not working
My code as below
$mail->charset = 'utf-8';
$mail->body('',$strInv);
$mail->subject('"=?UTF-8?B?".base64_encode(我的问题)."?=" #'.$inquiry_no);
when I received in gmail subject looks like this :
"=?UTF-8?B?".base64_encode(è®¢å •ç¡®è®¤)."?=" #00016
I really appreciate anyone can help me with this. Thank you.
when you fix it your subject string should look like this:
=?UTF-8?B?RUSSIANNNN?=
use the echo function to debug your subject string before you call
$mail->subject
or just do
$ssubject = '=?UTF-8?B?' . base64_encode('RUSSIAN') . '?=';
$ssubject = $ssubject . $inquiry_no;
$mail->subject($ssubject);
good luck newbie
Related
I am using the Swiftmailer in my Symfony2 webapp.
// Subject and body dynamically come from database
$subject = "This is my subject with an apostroph ' in it.";
$bodytext = "Test text, also with an ' apostrophe in it.";
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom('me#mail.com')
->setTo('you#mail.com');
$message->setBody($bodytext);
$this->get('mailer')->send($message);
Now when there is a special char, e.g. the apostrophe (') in my subject, the email has a strange subject line in my email client:
This is my subject with an apostroph ' in it
Funny thing: The body text is displayed correctly, it's only the subject that's wrongly formatted.
Now how can I handle special chars like this - and even better, is there a function I can call before passing the subject that handles special chars in general?
Sorry, I know this is late but it might help anyone still having these similar issues.
I was having issues with apostrophe ' and ampersand & on SwiftMailer 6.2 so combining both htmlspecialchars_decode and html_entity_decode to convert the subject resolved it for me:
->setSubject(htmlspecialchars_decode(html_entity_decode($subject), ENT_QUOTES))
See https://www.php.net/manual/en/function.html-entity-decode.php and https://www.php.net/manual/en/function.htmlspecialchars-decode.php for further explanations.
Try to escape the subject with the htmlentities PHP function:
$subject = htmlentities("This is my subject with an apostroph ' in it.");
I'm using use Mail::IMAPClient to retrieve mail headers from an imap server. It works great. But when the header contains any character other that [a-z|A-Z|0-9] I'm served with strings that look like this :
Subject : Un message en =?UTF-8?B?ZnJhbsOnYWlzIMOgIGxhIGNvbg==?= (original string : "Un message en français à la con")
Body :
=C3=A9aeio=C3=B9=C3=A8=C3=A8 (original string : éaeioùèè)
What is this strange format ? Is that the famous "perl string
internal" format ?
what is the safest way of handling human idioms
coming from IMAP servers ?
The body encoding is Quoted-Printable; the header (subject) encoding is MIME "encoded-word" encoding ("B" type for base64). The best way to deal with both of them is to pass the email into a module that's capable of dealing with MIME, such as Email::MIME or the older and buggier MIME::Lite.
For example:
# $message was retrieved from IMAP
my $mime = Email::MIME->new($message);
my $subject = $mime->header('Subject'); # automatically decoded
my $body = $mime->body_str; # also automatically decoded
However if you need to deal with them outside of the context of an entire message, there are also modules like Encode::MIME::Header and MIME::QuotedPrint.
It is quoted-printable coded. It is a standard encoding used in email. It has nothing to do with Perl's internal string format.
Set out to write a simple procmail recipie that would forward the mail to me if it found the text "Unprovisioned" in the subject.
:0:
* ^Subject:.*Unprovisioned.*
! me#test.com
Unfortunately the subject field in the mail message coming from the mail server was in MIME encoded-word syntax.
The form is: "=?charset?encoding?encoded text?=".
Subject: =?UTF-8?B?QURWSVNPUlk6IEJNRFMgMTg0NSwgTkVXIFlPUksgLSBVbnByb3Zpc2lvbmVkIENvbm4gQQ==?=
=?UTF-8?B?bGVydA==?=
The above subject is utf-8 charset, base64 encoding with text folded to two lines. So was wondering if there are any mechanisms/scripts/utilities to parse this and convert to string format so that I could apply my procmail filter. Ofcourse I can write a perl script to parse this an perform the required validations, but looking to avoid it if possible.
Encode::MIME::Header, which ships with Perl, accessed directly through Encode:
use Encode qw(encode decode);
my $header_text = decode('MIME-Header', $header);
I use MIME::Entity module in Perl to create a MIME message. Some of the headers seem to be encoded OK, while other seem to have issues with folding.
Code:
use strict;
use Encode;
use MIME::Entity;
my %build_params = (
'Charset' => 'UTF-8',
'From' => encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>'),
'Subject' => encode('MIME-Header', "A very long subject that will span on multiple lines in the headers, with a leading sp\
ace at the beginning of each new line."),
'Type' => 'multipart/alternative',
);
my $top = MIME::Entity->build(%build_params);
$top->print_header();
Output:
Content-Type: multipart/alternative;
boundary="----------=_1312196104-11708-0";
charset="UTF-8"
Content-Transfer-Encoding: binary
MIME-Version: 1.0
X-Mailer: MIME-tools 5.427 (Entity 5.427)
Subject: A very long subject that will span on multiple lines in the
headers, with a leading space at the beginning of each new line.
From: Fantasy Email
<vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail .com>
The Subject seems to be correctly split into multiple lines. The From doesn't, leaving a space before the com, but the newline is gone.
Is this standard behavior or have I found a bug in MIME::Entity?
Encode::MIME::Header (called as encode('MIME-Header', ...)) does some line splitting (called folding in the RFC 822).
Unfortunately, MIME::Entity does some line splitting too, probably in a different way. It also gets rid of the newline generated by Encode::MIME::Header. It leaves the spaces though.
I would be happy to leave MIME::Entity deal with the encoding of my headers, but it looks like it just does the line splitting part. So I guess I still have to encode them myself.
As a workaround, I removed the line splitting markers from my encoded headers with
my $encoded_from = encode('MIME-Header', 'Fantasy Email <vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>');
$encoded_from =~ s/\r?\n\s//g;
(And same thing for the subject.)
Now the output looks like this:
Subject: A very long subject that will span on multiple lines in the
headers, with a leading space at the beginning of each new line.
From: Fantasy Email
<vujerldujhgurtelhwgutrwhgunwlhvulhgvnuwlhvuwlnhvgnulwh#gmail.com>
I'm wondering if there's a more elegant solution, like Encode::MIME::Header featuring a MIME::Entity compatibility mode or something like that.
I am getting Arabic characters as ????? in return from JSON.
Can anyone tel me how to get Arabic characters right in JSON format?
EDIT:
I am using English language. I also have tried encoding it to UTF8.
Many Thanks,
Naveed
I have searched the solution by myself.
Solution is just call the set names to utf8 after connecting to DB like this:
$host_link = mysql_connect(DBASE_HOST, DBASE_USER, DBASE_PWD);
if (!$host_link) {
die('Could not connect: ' . mysql_error());
exit;
}
mysql_query("SET NAMES utf8; ");
Hope this will help some others.
Naveed