Sending email with special characters.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marcelo
Forum Newbie
Posts: 3
Joined: Thu Dec 03, 2009 8:11 pm

Sending email with special characters.

Post 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']}'");
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Sending email with special characters.

Post 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?
marcelo
Forum Newbie
Posts: 3
Joined: Thu Dec 03, 2009 8:11 pm

Re: Sending email with special characters.

Post by marcelo »

I guess that's my question;

How to encode the message? I'm not sure how to do it.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Sending email with special characters.

Post 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)
marcelo
Forum Newbie
Posts: 3
Joined: Thu Dec 03, 2009 8:11 pm

Re: Sending email with special characters.

Post 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.
hostingon
Forum Newbie
Posts: 6
Joined: Fri Dec 04, 2009 5:00 pm
Location: USA

Re: Sending email with special characters.

Post 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:
Post Reply