PHPmailer Japanese Character - php

i'm trying to send an email with a japanese character with PHPmailer,
This is my function :
function sendMail()
{
mb_language('ja');
mb_internal_encoding('UTF-8');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = EMAIL_HOST;
$mail->Port = EMAIL_PORT;
$mail->Username = EMAIL_USERNAME;
$mail->Password = EMAIL_PASSWORD;
$mail->SMTPKeepAlive = true;
$mail->Mailer = 'smtp';
$mail->CharSet = 'ISO-2022-JP';
$mail->Encoding = "7bit";
$mail->SMTPDebug = 0;
$mail->From = EMAIL_SET_FROM_EMAIL;
$mail->FromName = mb_encode_mimeheader(EMAIL_SET_FROM_NAME, "ISO-2022-JP-MS");
$mail->addAddress($this->to);
if (!empty($this->replyTo)) {
$mail->addReplyTo($this->replyTo);
}
$mail->isHTML(true);
$mail->Subject = mb_encode_mimeheader($this->subject, "ISO-2022-JP-MS");
$mail->Body = mb_convert_encoding($this->body, "ISO-2022-JP-MS", "UTF-8");
$isSend = $mail->send();
if (!$isSend) {
throw new exception(__METHOD__ . '() ' . $mail->ErrorInfo);
}
}
In the recipient the email body which have Japanese character sometimes broken like this :
Case 1 : エ %j%" : A
Case 2 : My friends Japanese laptop showing several black diamond character with question mark in it. Its on Gmail,
Case 3 : A question mark appear in some Japanese character.
Can any body show me the correct setting for PHP mailer so it can send a Japanese-character mail without unknown character shown in the recipient ?

The black diamond with the question mark on viewing browser means that it has no glyph for that character. The character may be valid but it cannot be displayed.
In other words, it may be a limitation with the system doing the display and not your program.
You should check your program, though. Open the email in a hex editor and verify that the code is what you expect and matches the encoding you've specified.

You are encoding stuff yourself, and then PHPMailer will be doing it again. When you set the subject and body, just use raw text in the correct charset, don't encode it yourself. You're also setting 7bit encoding with a charset that will not fit into 7 bits. If your text is already in UTF-8, why not stick with that? UTF-8 processing is generally more reliable than 8-bit charsets.

Related

How to convert a certain special character using PHP

I have a word with a special character in it that does not display properly in the subject line of an email (via mail()).
The word is O’Conner and the special character is ’
"as is" the word appears as O’Conner in the subject line of the incoming email message.
When I use html_entities_decode()the word appears as O’Conner in the subject line of the incoming email message.
When I use mb_encode_mimeheader("O’Conner", "UTF-8", "Q") the word appears as O’Conner in the subject line of the incoming email message.
Is there a way to get the word to appear is it should?
Other similar or seemingly duplicate question/answers do not see to resolve this particular issue.
I even tried UTF-7 -- that did not work.
Here's the email function:
/**
* Send an html/smtp email
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function htmlEmailer( $email, $type, $parameter ) {
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require('/etc/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php');
mb_internal_encoding('UTF-8'); // 2021-06-18 added per https://stackoverflow.com/questions/7669668/how-to-use-special-characters-in-recipients-name-when-using-phps-mail-function/7670192#7670192
// Instantiation and passing "true" enables exceptions
$mail = new PHPMailer(true); // create a new object
//Server settings
$mail->SMTPDebug = 0; // see best_practices.txt file for settings
$mail->isSMTP(); // set mailer to use SMTP
$mail->Host = SMTP_SERVER; // specify main and backup SMTP servers separated by semi-colon (defined in config.php)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = SMTP_USER; // SMTP username (defined in config.php)
$mail->Password = SMTP_PASSWORD; // SMTP password (defined in config.php)
$mail->SMTPSecure = 'tls'; // enable encryption [tls or ssl]
$mail->Port = 587; // TCP port to connect to [465 or 587]
//Recipients
$mail->setFrom('info#example.com', mb_encode_mimeheader(SENDER_LAST_NAME, 'UTF-8', 'Q'));
$mail->addAddress($email, ''); // add a recipient [2nd parameter "recipient's name" is optional and is separated by a comma]
$mail->addReplyTo('no_reply#example.com', 'Do Not Reply');
//$mail->addCC('me#example.com');
$mail->addBCC('you#example.com');
// Attachments
//$mail->addAttachment('/home/cpanelusername/attachment.txt'); // Add attachments
//$mail->addAttachment('/home/cpanelusername/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'The message subject';
$mail->Body = 'HTML message body'; // html formatted body
$mail->AltBody = "Plain Text message body"; // plain text body
// Send the Message
$mail->send();
} // htmlEmailer()
Just replace it before sending:
$email_to = str_replace("’", "'", $email_to);

Mail Sending, Line break & and From header using gmail smtp

i send email with PHPMailer, my code is:
$mail = new PHPMailer(true);
$mail->Mailer = "smtp";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 0;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "nati323#gmail.com";
$mail->Password = "****";
$mail->addCustomHeader('Content-Type: text/plain; charset="utf-8"');
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->From = $from;
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->Body = $msg;
return $mail->Send();
the $msg variable is a function parameter i use it like this:
function sendMail ($to, $subject, $msg, $from, $add = null)
and i use it like this:
sendMail($_POST['mail'], $text['EMAIL_RECOVER_MSG_SUBJECT'], $text['EMAIL_RECOVER_MSG'], $from)
the variables contain:
$text['EMAIL_RECOVER_MSG_SUBJECT'] = 'Password Recover: From dmworld24.com';
$text['EMAIL_RECOVER_MSG'] = 'You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message';
$from = 'System#dmworld24.com';
now i try to add line break to message for example:
You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token=7054343021*****353214&mail=nati323#gmail.com \r\n If You Didnt Ask For Password Recovering Just Ignore This Message
and what i get in the mail is the same, the \r\n shows as chars and there is not line breaks, what i did wrong?
another problem that i have, that the form method dosent work, i send email for try to nati323#gmail.com , and i connect to smtp server with nati323#gmail.com acount, and i always get the message from my self, and the Form that i defined dosent show....
You're using single quotes in your message. That will take everything contained in it literally, which is why you're getting the literal string \r\n.
Replace the single quotes with double quotes to get the new line properly output.
I.e.
$text['EMAIL_RECOVER_MSG'] = "You Forget Your Password To Recover it Please Go Into The Next Link:\r\n http://dmworld24.com/login.php?act=rec&token={TOKEN}&mail={MAIL} \r\n If You Didnt Ask For Password Recovering Just Ignore This Message";`
Call the PHP function nl2br() to convert newlines to HTML line breaks. And then, setup PHPMailer to sent email as an html using $mail->IsHTML(TRUE);

Prevent Image blocking while sending emails using php

I tried this code. But image is not shown in the email. I need to display image without getting blocked.
require("class.phpmailer.php")
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->From="ex#gmail.com";
$mail->FromName="Email Image";
$mail->Sender="eg#gmail.com";
$mail->AddReplyTo("xe#gmail.com", "Reply");
$mail->AddAddress("exe#gmail.com");
$mail->Subject = "Test";
$mail->IsHTML(true);
$mail->AddEmbeddedImage('https://ci4.googleusercontent.com/proxy/D7vQqu3U7j2qWYtvzCLHodLRvMHt1Fq0F5s12lEp2YQc-RPwytgpqhRLhqzIZglky59F4-A-OtVXlmglO2CoS7CrkZk=s0-d-e1-ft#http://litebreeze.com/images/profile_small.jpg',profile_pic,'profile_small.jpg', "base64", "application/octet-stream");
$mail->Body = "This is a test picture: <img src=\"cid:profile_pic\" /></p>";
$mail->AltBody="This is text only alternative body.";
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo "Letter is sent";
}
You're doing several things wrong here.
addEmbeddedImage expects you to provide a local path to a file, not
a remote URL.
You are deliberately setting an incorrect MIME type
You have not quoted your cid.
I see no good reason to pull the image from google's proxy cache - just pull it directly from the source.
Fixes for all these:
$mail->AddStringEmbeddedImage(file_get_contents('http://litebreeze.com/images/profile_small.jpg'),'profile_pic','profile_small.jpg');
You're not using encryption - set $mail->SMTPSecure = 'tls'; and $mail->Port = 587;.
You're also using an old version of PHPMailer and have based your code on an obsolete example. Go get the latest.

PHPMailer - Certain SMTP messages don't send

I have a script that sends emails to users, which are sometimes divided into multiple messages depending on size. Here's the code:
for($i=0; $i<count($the_message); $i++){
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "my.smtp.server";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->IsHTML($is_html);
if($is_html === TRUE){
$mail->AltBody = "To view the message, please use an HTML compatible email client, or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
$mail->Body = $the_message[$i];
$mail->Subject = $the_sub;
$mail->Send();
}
This used to work great. Now, it seems that certain messages will never send. It seems to be a data issue (or perhaps the way PHPMailer sends lines to the server?), but I can't pinpoint why certain message segments are always rejected by the server.
The error message I get from PHPMailer is this:
SMTP Error: Data not accepted.
And from PHP itself:
Notice: fputs(): send of 31 bytes failed with errno=104 Connection
reset by peer in class.smtp.php on line 580
Here is a segment of HTML data that always fails to send:
http://pastebin.com/LGYkTTFA
Note that size isn't an issue, I can successfully send much larger HTML messages than this. However, when I send multiple segments, this one is never accepted by the server.
Here, I rewrote it for you in a somewhat saner way. You don't need to create everything from scratch every time around the loop, just change the body and send again.
<?php
require 'PHPMailerAutoload.php';
$the_message = array('Hello', 'there', 'Justin');
$to = array('tom#example.com', 'dick#example.net', 'harry#example.org');
$the_sub = 'Crazy subject';
$email_port = 25;
$from = 'justin#example.com';
$is_html = true;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->Host = "localhost";
$mail->Port = $email_port;
$mail->Username = "my#email.com";
$mail->Password = "mypassword";
$mail->SMTPAuth = true;
$mail->From = $from;
$mail->FromName = "My Name";
$mail->Subject = $the_sub;
$mail->IsHTML($is_html);
$mail->WordWrap = 200;
if($is_html){
$mail->AltBody = "To view the message, please use an HTML compatible email client, '.
'or change your export to a plain text format.";
}
foreach($to as $address){
$mail->AddAddress($address);
}
for($i=0; $i<count($the_message); $i++){
$mail->Body = $the_message[$i];
$mail->setWordWrap();
$mail->Send();
}
Update: added word wrapping to deal with content with very long lines.
In class.smtp.php, line 51, I changed the constant MAX_LINE_LENGTH from 998 to 500. This allowed all of my email segments to pass through.
I'm still not sure if the issue lies with my SMTP server or PHPMailer, I'll have to test it elsewhere.
EDIT: Tested using Gmail's SMTP server, and keeping 998 worked fine. This leads me to assume that my SMTP relay server is rejecting data sometimes depending on the length.
EDIT2: Still experienced failures with the above fix. I changed my port number and added SSL, and it is magically working. Wonky server issues...

PHPMailer character encoding issues

I try to use PHPMailer to send registration, activation. etc mail to users:
require("class.phpmailer.php");
$mail -> charSet = "UTF-8";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.mydomain.org";
$mail->From = "name#mydomain.org";
$mail->SMTPAuth = true;
$mail->Username ="username";
$mail->Password="passw";
//$mail->FromName = $header;
$mail->FromName = mb_convert_encoding($header, "UTF-8", "auto");
$mail->AddAddress($emladd);
$mail->AddAddress("mytest#gmail.com");
$mail->AddBCC('mytest2#mydomain.org', 'firstadd');
$mail->Subject = $sub;
$mail->Body = $message;
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
The $message contains latin characters. Unfortunately all webmail (gmail, webmail.mydomain.org, emailaddress.domain.xx) is using a different coding.
How can I force to use UTF-8 coding to show my mail exactly the same on all mailboxes?
I tried to convert the mail header width mb_convert_encoding(), but without luck.
If you are 100% sure $message contain ISO-8859-1 you can use utf8_encode as David says. Otherwise use mb_detect_encoding and mb_convert_encoding on $message.
Also take note that
$mail -> charSet = "UTF-8";
Should be replaced by:
$mail->CharSet = "UTF-8";
And placed after the instantiation of the class (after the new). The properties are case sensitive! See the PHPMailer doc fot the list & exact spelling.
Also the default encoding of PHPMailer is 8bit which can be problematic with UTF-8 data. To fix this you can do:
$mail->Encoding = 'base64';
Take note that 'quoted-printable' would probably work too in these cases (and maybe even 'binary'). For more details you can read RFC1341 - Content-Transfer-Encoding Header Field.
$mail -> CharSet = "UTF-8";
$mail = new PHPMailer();
line $mail -> CharSet = "UTF-8"; must be after $mail = new PHPMailer();
try this
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
I work myself this way
$mail->FromName = utf8_decode($_POST['name']);
http://php.net/manual/en/function.utf8-decode.php
When non of the above works, and still mails looks like ª הודפסה ×•× ×©×œ:
$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';;
Sorry for being late on the party. Depending on your server configuration, You may be required to specify character strictly with lowercase letters utf-8, otherwise it will be ignored. Try this if you end up here searching for solutions and none of answers above helps:
$mail->CharSet = "UTF-8";
should be replaced with:
$mail->CharSet = "utf-8";
I was getting ó in $mail->Subject /w PHPMailer.
So for me the complete solution is:
// Your Subject with tildes. Example.
$someSubjectWithTildes = 'Subscripción España';
$mailer->CharSet = 'UTF-8';
$mailer->Encoding = 'quoted-printable';
$mailer->Subject = html_entity_decode($someSubjectWithTildes);
Hope it helps.
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "16bit";
The simplest way and will help you with is set CharSet to UTF-8
$mail->CharSet = "UTF-8"
To avoid problems of character encoding in sending emails using the class PHPMailer we can configure it to send it with UTF-8 character encoding using the "CharSet" parameter, as we can see in the following Php code:
$mail = new PHPMailer();
$mail->From = 'midireccion#email.com';
$mail->FromName = 'Mi nombre';
$mail->AddAddress('emaildestino#email.com');
$mail->Subject = 'Prueba';
$mail->Body = '';
$mail->IsHTML(true);
// Active condition utf-8
$mail->CharSet = 'UTF-8';
// Send mail
$mail->Send();

Categories