使用phpmailer删除特殊字符

时间:2018-02-06 06:25:38

标签: php

如何使用此脚本避免使用特殊字符?

date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
include("class.smtp.php");
$mail    = new PHPMailer();
$name    = $_REQUEST['name'];
$email   = $_REQUEST['email'];
$message = $_REQUEST['comments'];
//$body             = 
file_get_contents('contents.html');
//$body             = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.office365.com"; // SMTP server
$mail->SMTPDebug  = 1;
$mail->SMTPSecure = "tls"; // 2 = messages only
$mail->SMTPAuth   = true;
$mail->Host       = "smtp.office365.com";
$mail->Port       = 587;
$mail->Username   = "***@ac.in";
$mail->Password   = "*******";
$mail->SetFrom('***@ac.in', 'First Last');
$mail->Subject = "Feedback";
$mail->AltBody = $comments;
if(!$mail->Send()) 
{ 
   echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
   echo "Message sent! Thank you for your feedback."; 
}

有时我会收到低于输出的数据,有人可以帮忙解决这个问题吗?

输出:

  

nation’的

     

民族是它的国家的

先谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用mb_convert_encoding()

代码:

$mail->AltBody    = mb_convert_encoding($comments, 'UTF-8', 'UTF-8');

这将删除所有非UTF-8字符。

有关详细信息,请参阅mb_convert_encoding()

更新代码:

<?php
date_default_timezone_set('America/Toronto');
require_once ('class.phpmailer.php');

include ("class.smtp.php");

 // optional, gets

calledfromwithinclass . phpmailer . php

if notalreadyloaded $mail = new PHPMailer();
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['comments'];

// $body             =

file_get_contents('contents.html');

// $body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.office365.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP
debuginformation(
for testing) // 1 = errors and messages
$mail->SMTPSecure = "tls"; // 2 = messages only
$mail->SMTPAuth = true; // enable SMTP
authentication $mail->Host = "smtp.office365.com"; // sets the
SMTPserver $mail->Port = 587; // set the SMTP port for the
GMAILserver $mail->Username = "***@ac.in"; // SMTP account
username $mail->Password = "*******"; // SMTP
accountltpassword $mail->SetFrom('***@ac.in', 'First Last');
$mail->Subject = "Feedback";
$mail->AltBody =  mb_convert_encoding($comments, 'UTF-8', 'UTF-8');
?>