简体   繁体   中英

Unable to send email using REST API build in php?

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 <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.

This is error which is generating :-

在此处输入图片说明

please suggest me what should i do for this?

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

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.

you should use this link.

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

Thank you all who tried to help me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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