电子邮件地址中的Unicode字符-phpmailer-php



我有一个脚本,可以通过表格向报名参加比赛的人发送确认电子邮件。

电子邮件是一个对象的属性,在该对象的构造函数中以以下方式设置:

$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>';
}
}

该地址不通过phpmailer的ValidateAdress函数,其中regex甚至没有启用utf8支持。尽管即使启用了utf8模式,它仍然无法验证。

您应该启用异常以便看到它们。您可以通过将true传递给构造函数来启用异常:

class emailMessage extends PHPMailer
{
public function __construct()
{
parent::__construct(true);

有了这个,你就会看到无效地址的异常。

最新更新