Jump to content

I found that sending email with asian chars may occur garbage text on recipient


adrianmak
 Share

Recommended Posts

I used wireMail to send website email notification to users.

I found that email with asian chars, the email received by user may occur garbage text.

web mail client (gmail)

post-2272-0-74370500-1462920489_thumb.pn

In gmail, show original. the text on the email raw header look good.

post-2272-0-38709000-1462920521_thumb.pn

The code I use to send email

$mail = wireMail();
$mail->to($settings['paypal']['order_email_notify'])->from('noreply@mydomain.com', 'No-Reply');
$mail->subject($order->member->name.' has placed an order. Order# '.$ordernumber);
$email_template = wireRenderFile("partial/email-order-notification-tpl", array('order'=>$order, 'settings'=>$settings, 'user'=>$user));
$mail->bodyHTML($email_template);
$mail->send();

The partial/email-order-notification-tpl.php file

<?php
// This email order template is for administrator for order placement notification

$out = '';
$grand_total = 0;
$total = 0;

$out .= "<html>";
$out .= "<head>";
$out .= "<meta charset='utf-8'>";
$out .= "<meta http-equiv='x-ua-compatible' content='ie=edge'>";
$out .= "<title>Order</title>";
$out .= "<style>";
$out .= "table {width: 500px;}";
$out .= "thead {font-weight:bold;}";
$out .= "tfoot tr td:first-child {text-align: right;}";
$out .= "tr td:last-child {text-align: right;}";
$out .= "tbody tr:nth-child(even) {background: #eee;}";
$out .= "tbody tr:nth-child(odd) {background: #ddd;}";
$out .= "thead tr {background: #6d95e0;}";
$out .= "</style>";
$out .= "</head>";
$out .= "<body>";
$out .= "<p>Dear, Admin</p>";
$out .= "<p>Member <strong>{$order->member->name}</strong> is placed an order on ".date('d-M-Y H:i:s', $order->order_time)."</p>";
$out .= "<p><strong>Member number:</strong> {$user->member_number}</p>";
$out .= "<p><strong>Member name:</strong> <span style='text-transform:uppercase'>{$user->surname}</span> {$user->given_name}</p>";
$out .= "<p><strong>Order number:</strong> {$order->title}</p>";
$out .= "<p><strong>Order date:</strong>".date('d-M-Y H:i:s', $order->order_time)."</p>";
$out .= "<p><strong>PayPal Transaction Id: </strong>{$order->paypal_transaction_id}</p>";
$out .= "<p><strong>Payment method: </strong>{$order->payment_method}</p>";
$out .= "<h3>Order details</h3>";
$out .= "<table width='100%'>";
$out .= "<thead>";
$out .= "<tr>";
$out .= "<td>Item</td>";
$out .= "<td>Qty</td>";
$out .= "<td>Price</td>";
$out .= "<td>subtotal</td>";
$out .= "</tr>";
$out .= "</thead>";
$out .= "<tbody>";

foreach($order->order_items as $item) {
    $p = $pages->get($item->product_id);
    $out .= "<tr>";
    $out .= "<td>{$p->title}<br/>{$p->body}</td>";
    $out .= "<td>{$item->product_quantity}</td>";
    $subtotal = $item->product_price * $item->product_quantity;
    $total += $subtotal;
    $out .= "<td>{$item->product_price}</td>";
    $out .= "<td>".number_format($subtotal)."</td>";
    $out .= "</tr>";
    $grand_total += $total;
}
$out .= "</tbody>";
$out .= "<tfoot>";
$out .= "<tr>";
$out .= "<td colspan=3>";
$out .= "subtotal:";
$out .= "</td>";
$out .= "<td>".number_format($total)."</td>";
$out .= "</tr>";
$out .= "<tr>";
$out .= "<td colspan=3>";
$out .= "shipping:";
$out .= "</td>";
$out .= "<td>0</td>";
$out .= "</tr>";
$out .= "<tr>";
$out .= "<td colspan=3>";
$out .= "Grand total:";
$out .= "</td>";
$out .= "<td>".number_format($grand_total)."</td>";
$out .= "</tr>";
$out .= "</tfoot>";
$out .= "</table>";
$out .= "<p><strong>"."Terms and Conditions"."</strong><br/>";
$out .= $settings['terms_and_conditions']['eng']."</p>";
$out .= "</body>";
$out .= "</html>";

echo $out;
Link to comment
Share on other sites

The Content-Transfer-Encoding of 7bit isn't compatible with utf8 (or any other non-US-ASCII charset), but unfortunately it is hardcoded in WireMail.php. When mail clients encounter this header and find characters above ASCII code 127, it is anybody's guess how they interpret them. This should IMHO be fixed directly in the WireMail class' send method. A quick-n-easy approach would be to look for individual characters outside the 0-127 range and, if such is present, set Content-Transfer-Encoding to base64 and use encode_base64 on the part in question.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...