PHPMailer& WordPress“通过电子邮件发布” - 没有创建帖子

时间:2018-01-12 16:00:25

标签: wordpress phpmailer

我正在使用PHP 5.4.16(更新不在本项目的范围内)为客户端创建脚本,因此PHPMailer 5.2.25。如果我将SetAddress更改为我的个人Gmail帐户(我收到包含主题,正文和附件的电子邮件(后者是使用PHPMailer的原因)),我的脚本(下面)有效,但是如果我将SetAddress更改为我的WordPress“通过电子邮件发送“地址,似乎没有任何内容。

问题:

  1. 我的剧本有什么问题吗?缺少标题?格式不正确的电子邮件?
  2. 如果我的剧本显然没问题,可能会有哪些其他调查渠道?
  3. 由于

    <?php
    echo "<p>" . date("h:i:sa");
    ?>
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    ?>
    <?php require_once($_SERVER['DOCUMENT_ROOT'] .  '/../www/phpmailer/class.phpmailer.php'); ?>
    <?php require_once($_SERVER['DOCUMENT_ROOT'] . '/../www/phpmailer/class.smtp.php'); ?>
    
    <?php
    $mail = new PHPMailer();
    
    $mail->IsSMTP(); // enable SMTP
    //$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages     only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; // or 587
    $mail->IsHTML(true);
    $mail->Username = "my@gmail.com";
    $mail->Password = "mypassword";
    
    $mail->From     = "my@gmail.com";
    $mail->FromName = "Me";
    //$mail->AddAddress("caju317davu@post.wordpress.com");
    $mail->AddAddress("my@gmail.com");
    $mail->AddReplyTo("my@gmail.com","developer");
    $mail->AddAttachment($_SERVER['DOCUMENT_ROOT'] . '/../www/_assets/articles/223/610/7f10120230e612e03eea9aa54a48a68f.jpg', 'attachment.jpg');
    
    $mail->Subject  = "My test email";
    $mail->Body     = "Hi! This is my first successful post created through email.";
    
    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo 'Message has been sent.';
    }
    
    ?>
    

2 个答案:

答案 0 :(得分:0)

现在我不确定这是否会有所帮助,但下面是我在使用PHP版本5.4.16的网站上运行的PHPMailer v5.2.9的工作版本的代码,因此可能有所帮助。我可以看到的明显差异是收件人的部分没有“设置”或“添加”,而且我似乎无法找到启动器($ mail-&gt; send();)。如果这有帮助,请告诉我以及任何需要额外澄清的方面。

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.host.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail@example.co.uk'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

//Recipients
$mail->setFrom('email@example.co.uk', 'Company Name');
$mail->addAddress($email); //Gets the email the enquiry form field contains
$mail->addReplyTo('email@example.co.uk', 'Company Name');

$body='<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, 
sans-serif; font-size:12px; margin:0; padding:0;">
<p>Thank you for your enquiry, a member of the team will be in touch shortly to discuss your requirements. In the mean time, please double check the below details entered on our website. If any of these are incorrect or you wish to add to it, please contact us via email or over the phone.</p></div>';

//Content
$mail->isHTML(true);
$mail->Subject = $subject ;
$mail->Body    = $body;
$mail->AltBody = strip_tags($body);

$mail->send();
echo $thankyou_text;
} catch (Exception $e) {
  echo $failed_text;
  echo 'Mailer Error: ' . $mail->ErrorInfo;
}

答案 1 :(得分:0)

来自幸福工程师&#39;在WordPress:

  

你好!

     

在WordPress.com,我们不允许自动发送帖子   诸如PHPMailer之类的脚本。

我想这就是答案!现在忘记PHPMailer,并尝试回到默认的PHP mail():(

很抱歉打扰了你们所有人