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

发布于 2024-12-15 00:30:26 字数 1303 浏览 3 评论 0原文

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

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



 require_once "Mail.php";
 $from = "Sender <[email protected]>";
 $to = "Recipient <[email protected]>";
 $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 中不起作用。

这是正在生成的错误:-

在此处输入图像描述

请建议我为此该怎么做?

I am designing REST API in php. I am using slim framework to design API. I want to send a page to send email. This is my code to send email:

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



 require_once "Mail.php";
 $from = "Sender <[email protected]>";
 $to = "Recipient <[email protected]>";
 $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>");
  }
 ?>
});

My code of sending email is working if i check this into my separate file. But this code is not working in API.

This is error which is generating :-

enter image description here

please suggest me what should i do for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-12-22 00:30:26

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

错误具有错误级别,例如 E_NOTICEE_WARNING。例外则不然。你要么有例外,要么没有。

您正在使用的 PEAR 邮件程序版本发出了一个较小的弃用通知。通常它会被隐藏,你不会知道它,但由于它被转换为异常,Slim 会向你显示一个错误。这是一件好事;你的代码不应该有通知。

要解决此问题,您可以尝试更新邮件程序类以避免弃用的功能,或者您可以自己暂时捕获错误:

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();

Slim approaches PHP errors in a object-oriented way. It transforms all errors into exceptions using PHP standard class ErrorException.

Errors have error levels, such as E_NOTICE or E_WARNING. Exceptions do not. You either have an exception either you don't.

The version of PEAR mailer you are using is raising a minor deprecation notice. Usually it would be hidden and you wouldn't know about it, but since it is transformed to exception, Slim shows you an error. This is a good thing; your code should not have notices.

To solve this you can try updating your mailer class as to avoid deprecated features or you could temporarily catch errors yourself:

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();
永言不败 2024-12-22 00:30:26

为什么这两条线是相反的?

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

<?php

不应该是:

<?php

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

Why are these 2 lines in reverse?

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

<?php

Shouldn't it be:

<?php

$app->get('/sendemail', function () {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文