0

I am using php mail function to send an email with a pdf attachment, but its name which is written in greek does not show correctly. I have tried many different things but nothing worked.

require_once "/usr/share/pear/Mail.php";
require_once "/usr/share/pear/Mail/mime.php";
$filename= "ΤΠΥ ΗΛ ΚΛΠ greek chars.pdf";

$mailBody = 'lorem ipsum blah blah';

$from = 'test@test.gr';
$to = 'test2@test2.gr';

//email settings from database (it is working correctly)
$smtp = Mail::factory('smtp',array ('host' => $emailSettings['host'],'port'=> $emailSettings['port'],'auth' => $auth,'username' => $emailSettings['username'],'password' => $emailSettings['password']));

$hdrs = array ('From' => $from,
                'To' => $to,
              'Subject' => $emailMessages['subject'],
             'Content-Type'  => 'text/html; charset=UTF-8');
$mime_params = array(
  'text_encoding' => '7bit',
  'text_charset'  => 'utf-8',
  'html_charset'  => 'utf-8',
  'head_charset'  => 'utf-8'
);        
$mime = new Mail_mime();          
$mime->setHTMLBody($mailBody);
$mime->addAttachment($filename, 'application/pdf');

$body = $mime->get($mime_params);
$hdrs = $mime->headers($hdrs);
$mail = $smtp->send($to, $hdrs, $body);

The result I get is ΤÎ%c2 greek chars.pdf. Could anyone figure out what is wrong

1 Answers1

0

As per Mail_Mime::addAttachment() documentation, you may specify the encoding of the attachment's name.

https://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php

So, you can modify your addAttachement function as:

$mime->addAttachment($file, 'application/pdf', $filename, true, 'base64', 'attachment', '', '', '', 'utf-8');
Pubudu Jayawardana
  • 2,250
  • 1
  • 13
  • 18