我有一个脚本,它通过表格向报名参加比赛的人发送一封确认电子邮件。
电子邮件是对象的属性,在该对象的构造函数中以下列方式设置:
$this->email = mysqli_real_escape_string($link, $email); $this->email = iconv(mb_detect_encoding($this->email, mb_detect_order(), true), "UTF-8", $this->email);
请注意,转换是我放在那里尝试解决这个问题的东西,但它没有用。
然后通过同一对象上的公共函数发送它: 在下面查看类 emailMessage,它是 PHPMailer 的扩展
public function sendMail($subject, $message)
{
global $CFG;
$mail = new emailMessage();
$mail->Subject = $subject;
$mail->setContent($message);
$sentMails = 0;
$errors = "";
$mail->AddAddress($this->email);
if(!$mail->Send())
{
$errorCount++;
$errors .= 'Mailer error: ' . $mail->ErrorInfo . "<br/>";
}
else
{
$sentMails++;
}
$mail->ClearAddresses();
if($sentMails > 0)
return true;
if($errors != "")
{
echo $errors;
return false;
}
}
这一切都非常好用,但是,只要$this->email
包含特殊字符,在本例中为 Æ、Ø 或 Å,脚本就会给我以下错误:
Mailer error: You must provide at least one recipient email address.
我尝试了各种字符串编码,但似乎都不起作用。
我应该指出,丹麦域 (.dk) 允许包含这些特殊字符。
我真的希望有人能告诉我问题是什么!谢谢阅读。
最后但同样重要的是:正如所承诺的,PHPMailer 的扩展:
class emailMessage extends PHPMailer
{
public function __construct()
{
$this->CharSet="UTF-8";
$this->AddEmbeddedImage(calculateRelativePath() . "img/camp_carnival.png", "camp_carnival");
$this->IsSMTP(); // telling the class to use SMTP
//$this->Host = $CFG->smtpServer; // SMTP server
//$this->SMTPDebug = 2; // enables SMTP debug information (for testing)
$this->SMTPAuth = true; // enable SMTP authentication
$this->SMTPSecure = "tls"; // sets the prefix to the servier
$this->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$this->Port = 587; // set the SMTP port for the GMAIL server
$this->Username = "x@domain.com"; // GMAIL username
$this->Password = ""; // GMAIL password
$this->SetFrom('x@domain.com', 'Lasse Rørbæk');
$this->AddReplyTo("x@domain.com","Lasse Rørbæk");
$this->WordWrap = 50;
$this->IsHTML(true);
}
public function setContent($content)
{
$this->Body = '
<table width="100%" style="background-color:rgb(239,233,217);">
<tr>
<td height="15">
</td>
</tr>
<tr>
<td width="100%" style="margin:20px 0px 30px 0px;">
<center>
<img width="80%" alt="Camp*Carnival" src="cid:camp_carnival"/>
</center>
</td>
</tr>
<tr>
<td style="width:100%;padding:20px 70px;">
<div style="margin:auto;width:65%;border:3px solid rgba(0,0,0,0.5);border-radius: 7px;padding:10px 7px;background-color:rgba(255,255,255,0.7);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#7FFFFFFF,endColorstr=#7FFFFFFF);">
' . $content . '
</div>
</td>
</tr>
</table>';
}
}