繁体   English   中英

无法使用php中构建的REST API发送电子邮件?

[英]Unable to send email using REST API build in php?

我正在用php设计REST API。 我正在使用苗条的框架来设计API。 我想发送一个页面以发送电子邮件。 这是我发送电子邮件的代码:

$app->get('/sendemail', function () {



 require_once "Mail.php";
 $from = "Sender <sender@domain.com>";
 $to = "Recipient <recipient@anotherDomain.com>";
 $subject = "Hi!";
 $body = "Hi,\n\nHey Recipient, you done it...";
 $host = "my host";
 $username = "myuserid";
 $password = "password";
 $headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);

 if (PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
   echo("<p>Message successfully sent!</p>");
  }
 ?>
});

如果将其签入单独的文件,则我发送电子邮件的代码有效。 但是此代码在API中不起作用。

这是正在生成的错误:-

在此处输入图片说明

请建议我该怎么办?

Slim以面向对象的方式处理PHP错误。 它使用PHP标准类ErrorException将所有错误转换为异常。

错误具有错误级别,例如E_NOTICEE_WARNING 例外没有。 您要么有例外,要么没有。

您使用的PEAR邮件程序的版本引起了次要弃用通知。 通常,它会被隐藏,并且您不会知道,但是由于它已转换为异常,因此Slim向您显示错误。 这是一件好事。 您的代码中没有通知。

为了解决这个问题,您可以尝试更新您的mailer类,以避免使用过时的功能,或者您可以暂时自己捕获错误:

function ignoringHandler($level, $str, $file='', $line='', $context=array())
{
    // Tell PHP that we have "processed" the error
    return true;
}

// Change the handler to ours for notices
$slimHandler = set_error_handler('ignoringHandler', E_NOTICE | E_DEPRECATED);

// Your code accessing older library

restore_error_handler();

为什么这两行相反?

$app->get('/sendemail', function () {

<?php

不应该是:

<?php

$app->get('/sendemail', function () {

嘿,我非常有效地发送了电子邮件。

我为此使用php mailer。

您应该使用此链接。

http://moneyandjobs.blogspot.com/2008/06/phpmailer-settings-to-send-email-using.html?showComment=1321171320586

谢谢所有试图帮助我的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM