【发布时间】:2019-07-25 19:12:31
【问题描述】:
我正在尝试将 PHPmailer 包含在 functions.php 中
我的代码:
add_action('wp_ajax_nopriv_test_mailer', 'test_mailer');
函数 test_mailer () {
try {
require_once(get_template_directory('/includes/mail/PHPMailer.php'));
require_once(get_template_directory('/includes/mail/Exception.php'));
$mail = new PHPMailer(true); // Passing `true` enables exceptions
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test@gmail.com'; // SMTP username
$mail->Password = 'dummypassword!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('test@gmail.com', 'Mailer Test');
$mail->addAddress('john.doe@gmail.com', 'John User'); // Add a recipient
$mail->addReplyTo('test@gmail.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject testing';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
wp_die();
}
我也尝试将 require_once 排除在 try catch 之外,仍然是同样的错误,这里是关于错误的 sn-p
“PHP 致命错误:未捕获的错误:找不到类 'PHPMailer'”
我使用 betheme 模板并将 PHPmailer 文件存储在 betheme/includes/mail 中。
【问题讨论】:
-
您是否有不想使用内置 wp_mail 功能的原因?他们已经使用 PHPMailer。 codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init
-
嘿 @BA_Webimax 很棒的信息欢呼我想做的是通过 ajax 发送用户数据,如果在 functions.php (my_function) 中成功,我会获取用户电子邮件并向用户发送确认电子邮件,然后将字段(用户输入)保存在我的表中,我不想在用户提交表单时重新加载整个页面。
标签: php wordpress email phpmailer