LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2012, 04:07 AM   #1
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
PHP: Send email with attachment and UTF8 in filenames


Hi

I am trying to send email with a file attachment. Everything is UTF8. Here is some code:

PHP Code:
require_once 'Mail.php';
require_once 
'Mail/mime.php';
require_once 
'Mail/mail.php';

$to "recipient@example.org";
$from "sender@example.org";
$subject "זרו-subject";
$message "זרו-message";
$attachment_data "זרו-filecontent";
$attachment_filename utf8_decode("זרו-name.txt");
send_mail_utf8_with_attachment($to$from$subject$message ,$attachment_data$attachment_filename);

function 
send_mail_utf8_with_attachment($to$from$subject$message "",$attachment_data=""$attachment_filename="")
{
    
$params = array();
    
$params['head_charset'] = 'utf-8';
    
$params['text_charset'] = 'utf-8';
    
$params['html_charset'] = 'utf-8';
    
$params['eol'] = "\n";
    
$hdrs = array(
              
'From'    => $from,
              
'Subject' => $subject
    
);
    
$mime = new Mail_mime($params);
    
$mime->setTXTBody($message);
    if (
strlen($attachment_data) && strlen($attachment_filename)) {
        
$mimeType "application/octet-stream";
        
$mime->addAttachment($attachment_data$mimeType$attachment_filenamefalse);
    }
    
$body $mime->get();
    
$hdrs $mime->headers($hdrs);
    
$mail =& Mail::factory('mail');
    return 
$mail->send($to$hdrs$body);

The PHP file has UTF-8 encoding. Note the utf8_decode of $attachment_filename. Without it all email clients get it wrong. With it, some get it right (like GMail) but other still get it wrong, or replaces the filename with some default name. Also, I can't understand why the utf8_decode helps at all.

Everything else except the filename of the attachment is fine. If the filename contains just regular ascii, there is no problem.

I tried to read about it in the Pear documentation:

http://pear.php.net/manual/en/packag...attachment.php

But I don't understand it. Anyone have an idea what to do with $attachment_filename? Or what parameters to pass to tell that the filename is UTF8?
 
Old 02-17-2012, 07:56 AM   #2
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE
Posts: 102

Rep: Reputation: 24
Quote:
$subject = "זרו-subject";
$message = "זרו-message";
$attachment_data = "זרו-filecontent";
I think all headers in your mail-message are not allowed to have UTF-8 characters in them.
 
Old 02-17-2012, 08:34 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Original Poster
Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Well that's simply incorrect. As I wrote, everything except the filename of the attachment is fine. Also I can send and receive emails with UTF8 in filenames just fine. I just don't know how to do it with PHP.
 
Old 02-17-2012, 12:41 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Why not just try a test with all your utf8 strings decoded ?
PHP Code:
$from "sender@example.org";
$subject utf8_decode("זרו-subject");
$message utf8_decode("זרו-message");
$attachment_data utf8_decode("זרו-filecontent");
$attachment_filename utf8_decode("זרו-name.txt"); 
 
Old 02-17-2012, 01:02 PM   #5
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE
Posts: 102

Rep: Reputation: 24
It indeed works, the subject becomes this:
Quote:
Subject: =?utf-8?Q?=C3=A6=C3=B8=C3=A5-subject?=
And the message + attachment are looking ok (when received with Thunderbird, and then 'view source', from 'Date:'-down....)
Quote:
Date: Fri, 17 Feb 2012 19:54:25 +0100 (CET)

--=_92896147cb2a02b45588d261d65a21fd
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf-8

=C3=A6=C3=B8=C3=A5-message
--=_92896147cb2a02b45588d261d65a21fd
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
name*=US-ASCII''%E6%F8%E5-name.txt
Content-Disposition: attachment;
filename*=US-ASCII''%E6%F8%E5-name.txt

w6bDuMOlLWZpbGVjb250ZW50
--=_92896147cb2a02b45588d261d65a21fd--
But you also wrote:
Quote:
Without it all email clients get it wrong. With it, some get it right (like GMail) but other still get it wrong, or replaces the filename with some default name
Which client did get it wrong? (name, please ...)
 
Old 02-17-2012, 01:21 PM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Original Poster
Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Thanks. Good idea to check the source in the emails. The US_ASCII part seems incorrect, but I guess some email clients will understand that it's UTF-8 and some not?

I tried to use an email client called Firstclass that we use at work, and I sent an email to a guy who uses Outlook. Both got the filename in the attachment wrong. I'll try to investigate more on Monday when I'm at work. Did the strange letters look right with Thunderbird?
 
Old 02-17-2012, 04:27 PM   #7
JSkywalker
Member
 
Registered: Aug 2007
Distribution: openSUSE
Posts: 102

Rep: Reputation: 24
I looked ath this message in WindowsLiveMail, and the attachment had the wrong name....(ATT.00xxxx.dat)

When i opened the attachment, it did not look like an UTF-8 formatted textfile, but it looked like a non-utf-8-text.

I dont know Firstclass stuff, but looking at their site, it did not look like they support a 'normal' IMAP-server, so i cannot test this
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert email files with attachment to save as attachment file (metamail?) ted_chou12 Linux - Software 2 04-08-2011 08:01 PM
[SOLVED] how to process utf8 text of filenames to cp/ln thelordmule Linux - Software 1 11-07-2010 02:23 PM
Get/open email attachment using PHP benjalien Programming 2 09-07-2006 04:21 AM
How to send email with attachment using "mail"? jonathanztaub Linux - Software 4 05-10-2004 11:53 AM
Send Email + Txt Attachment Using Cron Job beringer Linux - Newbie 3 01-31-2003 10:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration