0

I've got problem with attachment name, if there are polish language diacritical signs, it won't display those signs.

I added just below php mailer class $mail->CharSet = 'UTF-8'; and it works for email body text( without it I had the same problem), but not for attachments. Also I had a similar problem with user name, but solved it with utf8_decode() function. Unfortunately that function is not working with attachment's name in my case.

<?php
    //Import PHPMailer classes into the global namespace
    //These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    //Load Composer's autoloader
    require 'autoload.php';
    
    //Create an instance; passing `true` enables exceptions
    $mail = new PHPMailer(true);
    //$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';
    $honeypot = $_POST['honey'];
    $user_name =  utf8_decode($_POST['name']);
    $user_email = $_POST['email'];
    $user_message = $_POST['message'];
    $user_phone = $_POST['phone'];
    $honeypot = trim($_POST["honey"]);
    $max_size = 2 * 1024 * 1204; //2mb
    $attachment = $_FILES['uploaded-file'];
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    
        if(!empty($honeypot)) {
            echo "NO SPAM!"; 
            exit;
          }    else {
    
            $mail = new PHPMailer; //From email address and name 
            $mail->isMail(); 
            
            //sender
            $mail->From = $user_email;
            $mail->FromName = $user_name;
    
            //recipient
            $mail->addAddress("jaroslaw.mor@gmail.com");
            
    
            //mail subject
            $mail->Subject = "Zapytanie ze strony www"; 
            
            
            $mail->isHTML(true);
            //body mail 
            $mail->Body = "Telefon:$user_phone<br><br>Treść wiadomośći:<br>$user_message";
            $mail->AltBody = "Telefon:$user_phone\n$content"; 
            
            //attachment
            if(isset($attachment)) {
    
                for ($i = 0; $i < count($_FILES['uploaded-file']['name']); $i++) {
                    if ($_FILES['uploaded-file']['error'][$i] !== UPLOAD_ERR_OK) continue;
                    $file_TmpName = $_FILES['uploaded-file']["tmp_name"][$i]; 
                    $file_name =   utf8_decode( $_FILES['uploaded-file']["name"][$i]);  
    
                    if ($_FILES['uploaded-file']['size'][$i] > $max_size) { 
                        echo "file is too big";
                        die();
                                 }
                     
                    else{
                     move_uploaded_file($file_TmpName,  "uploads/" . $file_name);
                     $mail-> AddAttachment("uploads/". $file_name);
                                                                    }      
                }//for
            }//isset
    
            if(!$mail->send()) {
                echo "Mailer Error: " . $mail->ErrorInfo;
               exit();
          } 
          
                  else {
                  header("Location: sent.html");
                                 
                     exit();
    
          }//if send else
    
    }//honey else end
    
    }//post end
Leonardo
  • 2,439
  • 33
  • 17
  • 31
Jaro
  • 35
  • 6
  • 1
    The function `utf8_decode()` notably _does not return UTF8 encoded data_. It returns ISO-8859-1 [Western European] encoded data, which is notably _incompatible with Polish_, which uses ISO-8859-2 or -16. If this is a "working solution" for the user name or filename, then you have additional problems. – Sammitch Apr 01 '22 at 21:39
  • In 2022 there's simply no reason to use anything else than UTF-8 in a web application. Verify your complete tech stack, esp. the database collation and your IDE encoding settings. Check our classic question [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through). – Álvaro González Apr 02 '22 at 12:27

3 Answers3

0

You can try to add this code before attachment upload:

setlocale( LC_ALL, "pl_PL.UTF-8" );
Dori Rina
  • 364
  • 2
  • 7
0

Try force encoding for attachment:

$mail->AddAttachment("uploads/", $file_name, $mail::ENCODING_BASE64, PHPMailer::filenameToType($file_name).'; charset=utf-8');
maguba
  • 11
  • 1
0

I tried all methods from here - without a positive result. By the accident I opened PHPMailer.php and noticed that there are some options connected with charset encoding, changed settings there and now it works.

Thx all for clues.

Jaro
  • 35
  • 6