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

时间:2012-12-09 16:39:13

标签: php unicode phpmailer

我有一个脚本,通过表单向注册参赛者的人发送确认电子邮件。

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

$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.包含特殊字符,在这种情况下Æ,Ø或Å,脚本会给我以下错误:

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

我尝试了各种字符串编码,而且似乎没有用。

我应该指出丹麦域名(.dk)可以包含这些特殊字符。

我真的希望有人能告诉我这是什么问题!谢谢你的阅读。

最后但并非最不重要:正如所承诺的,PHPMailer的扩展:

{{1}}

1 个答案:

答案 0 :(得分:0)

该地址未通过phpmailer的ValidateAdress函数,其中正则表达式甚至没有启用utf8支持。虽然即使启用了utf8模式,它仍然无法验证。

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

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

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