How to send Mandrill message with special characters?

守給你的承諾、 提交于 2019-12-25 04:53:39

问题


I am using Mandrill to send mails to our members.
Sending a normal text message works perfect.
But a message with special characters (ä,ë,ï,ö,ü,€,...) will not be attached in Mandrill's 'html' => variable.

Can anyone tell me how to send a message with special characters through Mandrill?

I tried to edit the $message_content variable with str_replace and preg_replace, none worked so far.

When I use ï and € when typing the message content, it works perfect. But I can't seem to change this when the submit button is pressed.

this is my code:

<?
if(isset($_POST['submit']) {
    $subject = $_POST['subject'];
    $from_name = $_POST['from_name'];
    $from_email = $_POST['from_email'];
    $message_content = nl2br($_POST['message_content']);
    $mail_to_members = array();
    $mail_to_members[] = array('email' => 'name@domain.com');
    $mail_to_members[] = array('email' => 'name@domain.com');

    //Get Mandrill API
    require_once './include/src/Mandrill.php'; 
    $mandrill = new Mandrill('API-key');

    //Create mail
    $message = array(
        'subject' => $subject,
        'from_name' => $from_name,
        'from_email' => $from_email,
        'html' => $message_content,
        'to' => $mail_to_members,
    );

    $mandrill->messages->send($message);
}
?>

回答1:


We had this exact same problem and we solved it using the following:

mb_convert_encoding($message_content, 'UTF-8', 'ASCII');

It converts your content from ASCII to UTF-8.




回答2:


Try using htmlentities

It will convert all special characters to HTML entities.

For example:

<?php
$string = 'ä - ë - ï - ö - ü - €';
echo htmlentities($string);

Will outout:

&auml; - &euml; - &iuml; - &ouml; - &uuml; - &euro;

Any HTML parser that follow the standard will be able to display them.



来源:https://stackoverflow.com/questions/31206544/how-to-send-mandrill-message-with-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!