无法使用 PHP 中构建的 REST API 发送电子邮件?
我正在 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 :-
please suggest me what should i do for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Slim 以面向对象的方式处理 PHP 错误。它使用 PHP 标准类
ErrorException
将所有错误转换为异常。错误具有错误级别,例如
E_NOTICE
或E_WARNING
。例外则不然。你要么有例外,要么没有。您正在使用的 PEAR 邮件程序版本发出了一个较小的弃用通知。通常它会被隐藏,你不会知道它,但由于它被转换为异常,Slim 会向你显示一个错误。这是一件好事;你的代码不应该有通知。
要解决此问题,您可以尝试更新邮件程序类以避免弃用的功能,或者您可以自己暂时捕获错误:
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
orE_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:
为什么这两条线是相反的?
不应该是:
Why are these 2 lines in reverse?
Shouldn't it be: