Home > Article > Backend Development > PHP mail function sends mail and garbled code solution
When using the mail function in PHP code to send emails, sometimes there will be garbled characters. In this case, adding a specific encoding method can solve the problem.
Full code. <?php /** * mail函数发送邮件 乱码问题 * 整理 by http://bbs.it-home.org */ $to = 'sales@zui88.com'; $subject = "=?UTF-8?B?".base64_encode('程序员之家,欢迎大家的光临。')."?="; $message = " 用户姓名:{$_POST['name']}\n 用户邮箱:{$_POST['email']}\n 用户电话:{$_POST['contact']}\n 留言内容:{$_POST['content']} "; $headers = 'From: test@jbxue.com' . "\r\n" . 'Reply-To: admin@jbxue.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; mail($to, $subject, $message, $headers); ?> |