【问题标题】:dynamically setting email adress in phpMailer在 phpMailer 中动态设置电子邮件地址
【发布时间】:2020-01-15 16:18:24
【问题描述】:

我在尝试从 GET 变量动态设置电子邮件时遇到问题。 我已经尝试转换为字符串,但我不断收到:“邮件程序错误:您必须提供至少一个收件人电子邮件地址。”

$destinationEmail = $_GET["email"];

  //$destinationEmail = (string)$_GET["email"]; ----> already tried this.

function sendEmail($pdf,$data){

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxxx";
$mail->Password = "xxxx";
$mail->SetFrom("Noreply@email.com");
$mail->Subject = "Subject";
$mail->Body = "Hello";
$mail->AddAddress($destinationEmail);
$mail->addStringAttachment($pdf,"pdf.pdf");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }
}

if ( $_GET["email"] ){
    sendEmail($pdf,$data);
}


我们将不胜感激任何帮助。提前致谢!

【问题讨论】:

  • 如果您像$mail->AddAddress("hello@world.com");这样对电子邮件进行硬编码会发生什么

标签: php email phpmailer


【解决方案1】:

sendEmail() 函数不知道电子邮件地址,因为该变量超出范围。您需要将 $destinationEmail 变量传递给您的 sendEmail() 函数:

function sendEmail($pdf,$data,$destinationEmail){
 ...
}

然后将电子邮件地址添加到函数调用中:

if ( $_GET["email"] ){
    sendEmail($pdf,$data,$destinationEmail);
}

【讨论】:

  • 还要检查来自addAddress() 的返回值——如果您提供的地址无效(或为空),它会返回 false,您可以使用它来中止发送或向用户显示错误。
  • 天哪...我没看到。非常感谢
猜你喜欢
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
相关资源
最近更新 更多