Search code examples
phpemailgmail

How to use non-ASCII strings in email Subject:, To: etc in PHP


Please tell me, I send using curl php email (gmail api). Content-Type is message/rfc822. How can I use Cyrillic in the Subject of an email when sending. In the body of the letter Cyrillic is displayed normally, in the subject of the letter it is displayed as symbols. ПривеÑ.

$access_token = "***";

$message = "To: ".addslashes($_POST['to'])."\r\nSubject: ".addslashes($_POST['subject'])."\r\n\r\n".addslashes($_POST['message']);

$ch = curl_init('https://www.googleapis.com/upload/gmail/v1/users/me/messages/send'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$access_token, 'Accept: application/json', 'Content-Type: message/rfc822; charset=utf-8'));    
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
$data = curl_exec($ch);

Solution

  • You need to use the standard php function:

    mb_encode_mimeheader($_POST['subject'], "UTF-8");