Good Day,
I'm sending an email with an attachement via SMTP with PHPMailer.
The charset in the mail is set to utf-8, but when I send an attachement with an special char like 'März' for example, the client gets an attachement with an broken charset like: M�rz.
My Code Example:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "host ip";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Username = "user";
$mail->Password = "pw";
$mail->setFrom('send#host.de', 'Host');
$mail->addAddress("client#mail.de");
$mail->Subject = 'Subject';
$mail->Body = "Email Content";
$mail->AltBody = 'Alternative Email Content';
$mail->AddAttachment("example/file_märz.pdf");
Am I doing something wrong or is there any fix for this problem?
Looking at the PHPMailer in the attachment process i found basename
was used. So to correctly output the specific encoding you need to setlocale depending on what charset compatible to your filename.
setlocale(LC_ALL, 'ja_JP.UTF-8');
the above code worked for me. Since I am using Japanese language/text for my filename.
If the problem is with the filename, and you're writing the file name inside the source code of your application (in the PHP file), then the problem can be one or both of the following:
The PHP file is saved with the wrong encoding. That is, you're telling PHPMailer to send UTF-8 emails, but your PHP file could be saved in ISO-8859-1 encoding. Thus, when PHP reads the "example/file_märz.pdf", it reads the byte sequence in ISO-8859-1, but then it tells PHPMailer to interpret it as UTF-8.
Solution: ensure your text editor is saving files in UTF-8
The operating system and/or client does not support Unicode file names for attachment.
Solution: there isn't really a solution, you need to avoid non-ASCII characters in file names (which couldn't be a bad idea anyways).
If your editor is already set to UTF-8 and you are sure your client/OS support Unicode in filenames, you need to check the raw email that PHPMailer sends and see what it contains. Many email clients let you see the raw email (the entire raw text).
Blockquote
I had the same problem.
It can be solve by adding:
utf8_encode() function to the $name of the attachment[] array.
On my version of phpmailer is located on class.phpmailer.php on line 1322.
Related
I am using SMPP Receiver API to get the messages from specific MSISDN. I am getting the English text correctly. But the Dari and Pashto text is not in same format. It shows output like below :
/ '2(� /'HFD/1
My Code is :
ob_start();
require_once "smpp.php";//SMPP protocol
//connect to the smpp server
$tx=new SMPP('IP',PORT);
//bind the receiver
$tx->system_type="NUll";
$tx->addr_npi=0;
$tx->bindReceiver("username","password");
do
{
//read incoming sms
if($sms=$tx->readSMS())
{
print_r($sms);
}
}while($sms);
And smpp.php is at :
http://121.100.50.58/apps/smpp/
How I can get the correct Dari and Pashto Language Message text?
Try this
$utf8 = $body;
$encodedMessage = mb_convert_encoding($utf8, "UCS-2", "utf8");
$from = new SmppAddress($sender, SMPP::TON_ALPHANUMERIC);
$to = new SmppAddress($number, SMPP::TON_INTERNATIONAL, SMPP::NPI_E164);
$smppClient->sendSMS($from, $to, $encodedMessage, [], SMPP::DATA_CODING_UCS2);
It works completly for polish characters and I'm pretty sure it works with all unicode.
Save the .php script itself in utf-8. This also has to line up with the charset in your browser.
If that still doesnt work, check out mb_convert_encoding http://de2.php.net/manual/en/function.mb-convert-encoding.php.
The text seems to be sent from the SMSC in UNICODE. It should be just a matter of using the correct encoding and charset ... try using charset UTF16-BE and encoding UCS2
I'm wondering how to get the charset via PHP IMAP when imap_fetchmime() isn't supported. I either have a lower version of PHP that doesn't support it or maybe it's deprecated, but it doesn't work for me.
What I basically need is to find out the charset of the email via IMAP, so the output would be:
ISO-8859-1
Or whatever it turns out to be. I don't want to read the whole email to find out, I want to find out what the charset is so I can use the correct encoding.
Here is the code I'm using simplified down for the purpose of this email:
$mailbox = "xxx#gmail.com";
$mailboxPassword = "xxx";
$mailbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",
mailbox, $mailboxPassword);
mb_internal_encoding("UTF-8");
$subject = mb_decode_mimeheader(str_replace('_', ' ', $subject));
$body = imap_fetchbody($mailbox, $val, 1);
$body = base64_decode($body);
echo $body;
I'm using the CodeIgniter email library to send emails using our Exchange server. The problem I get is that the content of the email gets messed up.
There are some words that get replaced with equal signs "=", I tried 2 different Exchange servers (they are in different locations and have no relation what so ever) and I still get the same issue. If I use any other server as an SMTP server to send emails everything works fine and the content stays intact and unchanged.
Content before sending:
Dear Customer
Please find attached a comprehensive explanation of how to get our brochure of Angola. This has been sent to you at the request of Alex.
The information has been taken from www.example.co.uk "Company name" is one of the leading tile and marble companies in the UK.
Content after sending it through the Microsoft Exchange:
Dear Customer
Please find attached a comprehensive explanation of how to get our brochure of A=gola. This has been sent to you at the request of Alex.
The information has been taken from www.example.co.uk "Company name" is one of the leadi=g tile and marble companies in the UK.
As you can see for some reason some of the "n" characters were replaced with equal signs "=" (Example: Angola > A=gola)
My email configuration:
$this->load->library('email');
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
// SMTP
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'exchange.example.com'; //ssl://
$config['smtp_user'] = 'email#example.com';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = 25;
$this->email->set_newline( "\r\n" );
$this->email->initialize( $config );
$this->email->clear();
......
$this->email->from( $frome, $fromn );
$this->email->to( $email );
$this->email->subject( $subject );
$this->email->message( $send_message );
$this->email->send();
Does anyone know why is the Microsoft exchange behaving this way? or is there some sort of setting I should use?
That's odd, specially since not all the ns are transliterated and not at a specific position.
Try calling $this->email->set_crlf( "\r\n" ); as well. Look up the message details in Exchange and inspect the Content-Type and Charset / Encoding - post the raw thing here so we can inspect it.
I found this in Microsoft Knowledgebase:
Microsoft Exchange uses an enhanced character set. The default MIME
character set for Microsoft Exchange is ISO 8859-1. Some gateways do
not support the way this character set issues a soft return for line
feeds. When this occurs, each line is terminated with an equal sign
showing the line break where the gateway's line-length support ends.
I solved this (kinda) by setting $charlim = '998' in the _prep_quoted_printable function.
When I set $crlf = "\r\n" the resulting email was completely garbled for some reason. But I noticed that the = signs were appearing at regular intervals which was caused by the line length being limited to 76 characters. So increasing the max characters per line (998 is the RFC2822 limit) solves the problem, as long as you don't have really long lines.
I've been trying out the PHPMailer sample script as given in its README file. Now, when I run that script:
<?php
require("phpmailer.inc.php");
$mail = new phpmailer;
//$mail->IsSMTP(); // set mailer to use SMTP
$mail->From = "from#email.com";
$mail->FromName = "Mailer";
//$mail->Host = "smtp1.site.com;smtp2.site.com"; // specify main and backup server
$mail->AddAddress("someone#someplace.com");
//$mail->AddAddress("ellen#site.com"); // name is optional
$mail->AddReplyTo("info#site.com", "Information");
$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("c:\\temp\\js-bak.sql"); // add attachments
//$mail->AddAttachment("c:/temp/11-10-00.zip");
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the message body";
$mail->Send(); // send message
?>
I get the following warning :
Warning: mb_send_mail() [function.mb-send-mail]: Unsupported charset
""iso-8859-1";" - will be regarded as ascii in
/export/opt/samplefiles/phpmailer.inc.php on line 162
and the message that is received contains the header as well:
Mime-Version: 1.0 This is the message body
Please help
That first warning hints at mb_send_mail() being used over the ordinary mail() function. Which is unlikely to be prepared for in the PHPMailer package.
It's the setting mbstring.func_overload you have to watch out for. It shouldn't have bit zero set. (For example change 5 to 4 -- or from seven to six,-- or for example from 3 to 2.)
It has to be set in the php.ini or .user.ini. Function overloading can't be fixed at runtime.
If that's not adaptable then a workaround might be mb_internal_encoding("UTF-8"); to eschew the error message, after applying mb_encode_mimeheader() on supplied values. Likely you have to adapt PHPMailer to omit the quotes around charaset parameters however.
I just realized that I've actually been using an older version of PHPMailer (version 0.89 or something). I've gotten myself a newer 5.2 and it appears resolved. Thanks anyway :P
I just got PHP's mail function to work properly in my test environment.
I have a PHP app that outputs a number of strings. It would be really nice to convert these strings to attachments (*.TXT -files) in an email, without first storing them on disk and having to read them back. Would this be possible in PHP?
Yes, this is possible. You just need to make your email message a multipart message with the following syntax:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=random-boundary
This is the optional preamble of a multipart/mixed message.
--random-boundary
Content-Type: text/plain
This is the main message body.
--random-boundary
Content-Type: text/plain
Content-Disposition: attachment; filename=file.txt
This is the content of the attached file.
--random-boundary--
This is the optional epilogue of a multipart/mixed message.
Each part can then be described like any other message. But you should probably use a library that does this for you.
Now if you’re using PHP’s mail function, the first two line would be the header and the rest would be the contents of that mail message. The boundary should be a random boundary so that the possibility of having that string with -- in front of it being in the contents of one part is very unlikely.
Yes, you can use e.g. PEAR's Mail_Mine class for it.
bool addAttachment ( string $file , string $c_type = 'application/octet-stream' , string $name = '' , boolean $isfile = true , string $encoding = 'base64' ) is the method you want to use, with $file containing your strings and $isfile being false.
And you can Use Zend_Mail Classes for much easier code
the file name would be "smapleFilename" and its the last parameter in createAttachment function
but don't foget to setup your transport before that
sample :
$mail = new Zend_Mail();
$mail->setBodyText("body")
->createAttachment("your wanted text " , Zend_Mime::TYPE_TEXT,
Zend_Mime::DISPOSITION_ATTACHMENT , Zend_Mime::ENCODING_BASE64, "smapleFilename.txt");
$mail->setFrom('test#222222.com', 'Server');
$mail->addTo('test#hotmail.com');
$mail->setSubject("subject");
$mail->send();
in Zend framework project you would do like this :
resources.mail.transport.type = smtp
resources.mail.transport.host = "mail.111111.com"
resources.mail.transport.auth = login
resources.mail.transport.username = test#111111.com
resources.mail.transport.password = test
;resources.mail.transport.ssl = tls
resources.mail.transport.port = 2525
resources.mail.transport.register = true ; True by default