Use emoji in dynamic content body of HTML email in PHP - php

I have been trying to send an HTML email with PHPMailer containing emojis:
$mail = new PHPMailer();
$mail->isHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = "😀 😃 😄 😁 😆 😅 😂 🤣 😊 😇 🙂 🙃 😉 😌";
$mail->Body = "😀 😃 😄 😁 😆 😅 😂 🤣 😊 😇 🙂 🙃 😉 😌";
However, the emojis are only displayed correctly in the subject; in the body, they are replaced by question marks (in Thunderbird and Outlook). The email does contain HTML, so just setting isHTML to false will not suffice. I have looked at Send unicode emoji with PHPMailer , but it does not seem to work for me, as the character set is already set to UTF-8. The content is user-generated (the user uses their own mobile keyboard to insert the emoji), so manually replacing every emoji with, for example, an image is not an option. Preferably the solution would be dynamic, so that new emojis also work whenever they come out.

you need to specify emoji's unicode value and try below method.
echo json_decode('"\uD83D\uDE00"');
Also you can use UTF-8 notation from this this table:
example:
😁 \xF0\x9F\x98\x81 GRINNING FACE WITH SMILING EYES

Related

phpmailer does not work for Persian letters

I have a problem that the subject is printed for Persian letters like this:
تست
I changed Cherst to utf-8 but the problem was not resolved
body has no problem with Persian letters, the only problem is the subject
$phpmailer->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
Don't encode your subject yourself; it will interfere with PHPMailer's processing. To get PHPMailer to handle it for you, just do this:
$mail->CharSet = 'UTF-8';
After that make sure that all your other text processing is in UTF-8 (e.g. your database), and it will all work.

Decoding Windows-1252 characters in imap subject line to UTF-8

I have a website that will allow people to post things to it using the subject line of an email in Outlook. Using PHP and imap, I get the subject line of the text and store it in a mysql db. But every once in a while, someone will copy text from a website into the subject line of that email and I will get garbled text. Similar to this:
=?Windows-1252?Q?_Every_day_in_our_offices_we_recycle_cardboard,aluminum?=
=?Windows-1252?Q?=96_won=92t_you_join_us=3F?=
What I've done is try to decode this text so it will appear normal on the page using the following code:
$subject = strip_tags($mailHeader->subject);
$header = imap_mime_header_decode($subject);
$subject = "";
for($i=0;$i<count($header);$i++)
{
$subject .= $header[$i]->text;
}
When finished I get rid of most of the garbled text, but am left behind with replacement characters for an em dash and a curly quote that was in the original subject line text. See the result below:
Every day in our offices we recycle cardboard, aluminum, � won�t you join us?
The charset for the website is set to UTF-8. When I set the website charset to ISO-8859-1, the replacement characters are replaced with the curly quote and em dash, which is great but I want to leave the website's charset at UTF-8.
Any help on how to get rid of the replacement characters without changing the charset to ISO-8859-1 would be great. Thanks.
Code above works except for one small change to the very end:
$subject .= mb_convert_encoding($header[$i]->text, "UTF-8", $header[$i]->charset);
Each of the objects returned by imap_mime_header_decode includes a charset property, which you are ignoring. You would need to convert each one to UTF-8 in your loop, using something like:
$subject .= mb_convert_encoding($header[$i]->text, "UTF-8", $header[$i]->charset);
As an alternative, consider using the mb_decode_mimeheader or iconv_mime_decode_headers functions. Both of these functions do the entire job of decoding a MIME header for you, returning a string in PHP's internal encoding (which is usually UTF-8).

phpmailer - undefined letters in the body

I use the PHPMailer as my mail tool for a project. I should send to a user a letter which contains Romanian alphabet (the main difference from English letters is the fact of issue such letters as â, ă, î, ș, ț).
I set charset UTF-8 for my letter:
$mail->CharSet = 'UTF-8';
but it didn't help me. I see in Google mail signs ? instead â, ă, î, ș or ț.
How can I fix it?

PHPmailer cyrillic charset trouble

I think im doing things correct:
Im specifying charset of PHPMailer object: $mail->CharSet = "UTF-8";
Charsets of mail body and php script are UTF-8 too
But email arrives with this kind of errors (words are broken by black quarter with "?" symbol:
опреÐ �елени
оÑ �ибка определения
click to see the screenshot of example email
Update 1
Found something kinda chinese glyphs (but not cyrillyc symbols) in the email's source
Try:
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
as staded by user2354947 in PHPMailer character encoding issues
the CharSet definition must be entered after the PHPMailer()
similar to my problem PHP ASCII to UTF-8 does not work
i found out that when you pull your data from sql, you need to transform it to UTF-8

PHP sent email subject - Hotmail/Outlook show £ as £

When I send an email containing £ using PHP mail it appears in outlook/hotmail as £. In Gmail/thunderbird it's fine.
Any idea how I can fix this?
The problem is, the client doesn't know what encoding is used to encode the subject. Whatever your application sets in Content-Type header only applies to the body of the email, not the headers.
Usually this affects the following headers:
Subject
From
To
In order to use different encodings your internationalized header lines should be MIME-encoded (as of RFC 2047), using one of the two methods: base64 (B) or modified quoted-printable (Q). The encoded subject usually looks like this:
Subject: =?ISO-8859-1?Q?Pr=FCfung_f=FCr?= Entwerfen von einer MIME kopfzeile
This may look difficult, but there is one very handy helper function in PHP which does all the magic:
iconv_mime_encode() - Composes a MIME header field
Alternatively you may look into discussion under:
quoted_printable_encode() - Convert a 8 bit string to a quoted-printable string
Before using quoted_printable_encode() directly you neet to take into account that long lines need to be split at certain length and spaces need to be replaced with underscore "_".
Just today I fixed a similar subject encoding issue by using phpmailer instead of php's builtin mail:
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet = "utf8";
$mail->Subject = $mail->EncodeHeader("You won £10000000!");
....
$retval = $mail->Send();
Usually I use the mb_convert_encoding() function
mb_convert_encoding($string, "UTF-8"); //AUTO DETECT AND CONVERT
mb_convert_encoding($string, "UTF-8", "latin1"); //MANUAL SET - CHANGE latin1 TO CURRENT ENCODING
Try to use UTF-8 encoding in your email.
this had results for my
<?php $subject = "=?UTF-8?B?" . base64_encode($subject) . "?="; ?>

Categories