Archived Forum Post

Index of archived forum posts

Question:

Base64 encoded mail with SMTP/PHP

Aug 17 '12 at 02:02

Hello,

I have problem with e-mail encoding. I try to send a base64 encoded mail with this code:

$mailman = new CkMailMan();
$mailman->UnlockComponent($ck_mail_unlock);
$mailman->put_SmtpHost($smtp_host);
$mailman->put_SmtpUsername($smtp_user);
$mailman->put_SmtpPassword($smtp_pass);
$mailman->put_SmtpPort(587);
$mailman->put_StartTLS(true);

$email = new CkEmail();
$email->put_Utf8(true);
$email->put_Charset('utf-8');
$email->AddHeaderField("Content-Transfer-Encoding", "base64");
$email->AddHeaderField("MIME-Version", "1.0");
$email->AddHeaderField("Content-Type", "text/html; charset=utf-8");
$email->AddHeaderField("X-Priority", "1");
$email->AddHeaderField("X-MSMail-Priority", "High");
$email->AddHeaderField("Importance", "High");
$email->AddHeaderField("From", "=?UTF-8?B?".base64_encode("From Name")."?=\n"." <from_mail@aaa.bbb>");          
$email->put_Subject("=?UTF-8?B?".base64_encode("Subject")."?=");
$email->SetHtmlBody("An HTML body.");
$email->put_From("From Name <from_mail@aaa.bbb>");
$email->AddTo("To Name", "to_mail@aaa.bb");

The sending is successfull but the content is not encoded. What's wrong with my code?

Regards, Adam


Answer

Adam,

The Content-Transfer-Encoding header field applies to the MIME body, but not the headers. The Chilkat Mail API is designed to automatically handle things such as encoding/decoding, charset conversion, etc. For example, if Asian language chars are included in the Subject, or From/To names, then those header fields will automatically be "B" encoded, using the charset of the email, whatever that may be. On the other hand, if 8bit latin chars are present, then the header fields containing those chars might be "Q" encoded. Chilkat will automatically choose the most appropriate encoding. If the chars are 7bit us-ascii, then there is no need for "Q" or "B" encoding.

I'm sorry to say that it's not possible to explicitly do what you are requesting. You may find it odd, but on the other hand, if Chilkat didn't do so many things automatically, then the learning curve for sending and/or reading email would be orders of magnitudes higher.


Answer

Thank you for your reply!