【问题标题】:Sending an EMail using Wordpress使用 Wordpress 发送电子邮件
【发布时间】:2012-11-09 10:07:55
【问题描述】:

我尝试了很多不同的方法,但无法在 PHP 中使用 mail() 函数通过 SMTP 成功发送电子邮件。

 <?php
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
$phpmailer->SMTPAuth = true;
$phpmailer->Username = 'skchourasia@asselslutions.com';
$phpmailer->Password = 'password01';
 
$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host       = "mail.asselsolutions.com"; // SMTP server
$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message];                      //HTML Body
$phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$phpmailer->WordWrap   = 50; // set word wrap
$phpmailer->MsgHTML($_POST[your_message]);
$phpmailer->AddAddress('support@wordpressapi.com/files/', 'Wordpress support');
//$phpmailer->AddAttachment("images/phpmailer.gif");             // attachment
if(!$phpmailer->Send()) {
 echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
 echo "Message sent!"; 
}
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$message =  $_REQUEST['message'];
$from = $_REQUEST['from'];
$headers = "From:" . $from;

$mail = mail($to,$subject,$message,$headers);

echo "Mail Sent.";
 ?>

我做错了什么?我收到以下错误:

解析错误:语法错误,第 8 行 C:\xampp\htdocs\wp-vtr\wp-content\themes\twentyeleven\content.php 中的意外 T_VARIABLE

 $phpmailer->IsSMTP(); // telling the class to use SMTP"

【问题讨论】:

  • 您的错误信息不完整,缺少行号。转到该行号,并告诉我们前几行中的代码是什么。错误就在那里。
  • 我已经更新了我的代码,错误在第 8 行,请检查一下
  • 您可以参考here。您可以使用wp_mail 函数发送邮件。
  • 尽管您发布的代码并不完美,但我没有看到任何可能导致解析错误的严重错误。真的是你发的content.php的代码吗?

标签: php wordpress sendmail


【解决方案1】:

这个:

$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message]; 

$phpmailer->MsgHTML($_POST[your_message]);

应该是这样的:

$phpmailer->FromName   = $_POST['your_email'];
$phpmailer->Subject    = $_POST['your_subject'];
$phpmailer->Body       = $_POST['your_message']; 

$phpmailer->MsgHTML($_POST['your_message']);

无论如何,您似乎正在尝试通过 PHPMailer 类和 mail() 本机 PHP 函数发送电子邮件。您可能只是在测试,但我不确定您要做什么。

【讨论】:

  • 在 your_message 中应用单引号后,出现与之前相同的错误
  • 请告诉我如何通过 PHPMailer 类或 mail() 函数使用 smtp 发送邮件
  • @digitalis 您对丢失的单引号是正确的,但这确实不是解析错误的原因。数组键周围缺少单引号会导致通知或警告,而不是错误。
猜你喜欢
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 2016-12-26
  • 2012-11-24
  • 2023-03-02
相关资源
最近更新 更多