Page 1 of 1

Sending email with special characters.

Posted: Thu Dec 03, 2009 8:26 pm
by marcelo
Hello,
I'm trying to figure out why when my code sends the email with special characters ( such as ç é í ó...) They don't get sent that way. Those character get sent differently ( like this é fã)

Any help is appreciated.

Code: Select all

//notification about new message
                $followers = $_SESSION['user']->get_followers();
                if(is_array($followers)) foreach($followers as $f){
                    if($f->notify_direct == 0 && !$f->has_friend_nf($_SESSION['user']->id)){
                        if($f->notify_way == 'email'){
                            $msg = notification_mail;
                            $msg = str_replace("#recipient_name", $f->username, notification_mail);
                            $msg = str_replace("#author_name", $_SESSION['user']->username, $msg);
                            $msg = str_replace("#author_link", $base_href.$_SESSION['user']->username, $msg);
                            $msg = str_replace("#message", urldecode($_POST['message']), $msg);
                            $to = $f->email;
                            $subject = str_replace("#username", $_SESSION['user']->username, notification_subject);
                            $headers = "From: Admin<".CONTACT_MAIL.">";
                            mail($to, $subject, $msg, $headers);
                        } else if($f->notify_way == 'sms'){
                            if($f->phone && $f->sms_credits > 0){
                                $to = $f->phone;
                                $msg = notification_sms;
                                $msg = str_replace("#recipient_name", $f->username, $msg);
                                $msg = str_replace("#author_name", $_SESSION['user']->username, $msg);
                                $msg = str_replace("#message", urldecode($_POST['message']), $msg);
                                send_sms($to, $msg);
                            }
                        } else if($f->notify_way == 'im' && !$f->has_friend_nf($_SESSION['user']->id)){
                            $db->db_insert("nudges", "user, txt", "{$f->id}, '{$_SESSION['user']->username}:\n{$_POST['message']}'");

Re: Sending email with special characters.

Posted: Fri Dec 04, 2009 2:47 am
by Apollo
1. How do you encode the special characters (well, the entire message for that matter) in your email's body ?

2. Do you also correctly specify this encoding in your headers?

Re: Sending email with special characters.

Posted: Fri Dec 04, 2009 3:38 am
by marcelo
I guess that's my question;

How to encode the message? I'm not sure how to do it.

Re: Sending email with special characters.

Posted: Fri Dec 04, 2009 5:03 am
by Apollo
Well, those funky characters, where do they come from? Do you enter them in php source files? (if yes, then it depends on how your editor encodes files you save)
Or do you users enter them on your site? (in that case, it depends on what content charset your site uses)
Or do you get them from a database? (in that case, it depends on the collation)

Re: Sending email with special characters.

Posted: Fri Dec 04, 2009 12:46 pm
by marcelo
the site is a blog. The users enter the text on the site (witch is UTF8) then it goes to the database (UTF8) then the email notification is sent to other users following those blogs.
The information is shown correctly on the page but not on the email.

Re: Sending email with special characters.

Posted: Fri Dec 04, 2009 5:21 pm
by hostingon
Hi man - the problem is in $headers parameter in your mail() function.

Your $headers must look like this:

Code: Select all

 
 $headers = "From: Admin<".CONTACT_MAIL.">\r\nContent-type:text/plain;charset=utf-8";
 
:idea: