【问题标题】:PHPMailer not working when trying to create using a PHP Function尝试使用 PHP 函数创建时 PHPMailer 不起作用
【发布时间】:2019-08-25 04:16:40
【问题描述】:

我正在使用 PHPMailer 发送电子邮件。但是我必须在每一页上编写发送电子邮件的长代码。所以我想尝试把所有的东西放到一个函数中,只要我想让事情变得干脆、简单和容易,就可以调用它。但是在尝试发送电子邮件时它不起作用。我尝试了以下方法:

functions.php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$settings = $pdo->prepare("SELECT * FROM settings");
$settings-> execute();
$set = $settings->fetch();

function newMail($name, $email, $sub, $msg, $set) {
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 0;
  $mail->Host = $set['set_smtp_host'];
  $mail->Port = $set['set_smtp_port'];
  $mail->SMTPSecure = $set['set_smtp_security'];
  $mail->IsHTML(true);
  $mail->SMTPAuth = true;
  $mail->Username = $set['set_smtp_uname'];
  $mail->Password = $set['set_smtp_pass'];
  $mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
  $mail->addAddress($email, $name);
  $mail->Subject = $sub;
  $mail->Body    = $msg;
  $mail->Send();
}

现在我尝试以这种方式在另一个页面(其中包含functions.php)上调用该函数:

$fname = (!empty($_POST['fname']))?$_POST['fname']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;

$sub = ''.$title.' - Account Verification Link';
$msg = 'SOME BODY MESSAGE';

if(newMail($fname, $email, $sub, $msg)){
    echo alert_success("Registration successful! Please check your email and click on the activation link to activate your account. If you did not receive any email within 5 minutes then <a href='resend.php'>click here</a> to resend it.");
}else{
    echo alert_success("Registration successful! But unfortunately, we could not send you a verification email. Please <a href='resend.php'>click here</a> to resend it.");
}

这里它总是返回 else 消息。我在这里写错代码了吗?

【问题讨论】:

  • 你的函数newMail()不返回'true'或'false',它什么也不返回。
  • 这段代码在把它放在一个单独的函数中之前工作了吗?
  • 是的,它在单独的函数之外起作用
  • 第一个问题是,正如@KIKOSoftware 所说,newMail() 函数什么也不返回。第二个问题是,您在 newMail() 函数之外包含了 PHPMailer 库。
  • @SuperGENScript 哦,对不起。执行 newMail() 函数时,您没有将 $set 变量作为参数发送。

标签: php email phpmailer


【解决方案1】:

最后修改你的函数newMail

return   $mail->Send();

如果邮件被发送,send 方法返回 true,所以你的函数应该返回这个值,如果不是:

if(newMail(...)){ }

为什么应用 else 情况总是错误的。

function newMail($name, $email, $sub, $msg, $set) {
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPDebug = 0;
  $mail->Host = $set['set_smtp_host'];
  $mail->Port = $set['set_smtp_port'];
  $mail->SMTPSecure = $set['set_smtp_security'];
  $mail->IsHTML(true);
  $mail->SMTPAuth = true;
  $mail->Username = $set['set_smtp_uname'];
  $mail->Password = $set['set_smtp_pass'];
  $mail->setFrom($set['set_noreply_email'], $set['set_site_name']);
  $mail->addAddress($email, $name);
  $mail->Subject = $sub;
  $mail->Body    = $msg;
  return $mail->Send();  // add return here 
}

【讨论】:

  • 我在你纠正前几秒钟就搞定了。但是谢谢:)
  • 我不能把use PHPMailer的东西放在函数里面吗?如果放置它返回 ` Parse error: syntax error, unexpected 'use' (T_USE) in`
  • 类定义在命名空间中是全局的,因此无需将use PHPMailer 放入函数中。在包含文件中可能需要它。
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2019-04-23
  • 1970-01-01
  • 2016-01-18
相关资源
最近更新 更多