[英]Unable to send email using REST API build in php?
I am designing REST API in php. 我正在用php设计REST API。 I am using slim framework to design API.
我正在使用苗条的框架来设计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 <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>");
}
?>
});
My code of sending email is working if i check this into my separate file. 如果将其签入单独的文件,则我发送电子邮件的代码有效。 But this code is not working in API.
但是此代码在API中不起作用。
This is error which is generating :- 这是正在生成的错误:-
please suggest me what should i do for this? 请建议我该怎么办?
Slim approaches PHP errors in a object-oriented way. Slim以面向对象的方式处理PHP错误。 It transforms all errors into exceptions using PHP standard class
ErrorException
. 它使用PHP标准类
ErrorException
将所有错误转换为异常。
Errors have error levels, such as E_NOTICE
or E_WARNING
. 错误具有错误级别,例如
E_NOTICE
或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. 您使用的PEAR邮件程序的版本引起了次要弃用通知。 Usually it would be hidden and you wouldn't know about it, but since it is transformed to exception, Slim shows you an error.
通常,它会被隐藏,并且您不会知道,但是由于它已转换为异常,因此Slim向您显示错误。 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: 为了解决这个问题,您可以尝试更新您的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();
Why are these 2 lines in reverse? 为什么这两行相反?
$app->get('/sendemail', function () {
<?php
Shouldn't it be: 不应该是:
<?php
$app->get('/sendemail', function () {
Hey i have send email very efficiently. 嘿,我非常有效地发送了电子邮件。
i used php mailer for this. 我为此使用php mailer。
you should use this link. 您应该使用此链接。
http://moneyandjobs.blogspot.com/2008/06/phpmailer-settings-to-send-email-using.html?showComment=1321171320586 http://moneyandjobs.blogspot.com/2008/06/phpmailer-settings-to-send-email-using.html?showComment=1321171320586
Thank you all who tried to help me. 谢谢所有试图帮助我的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.