为什么php-slim api路由没有丢失任何错误,尽管没有发送电子邮件

时间:2017-09-17 06:58:44

标签: php rest phpmailer postman slim

您好,我无法使用PHP-SlimPHP Mailer发送电子邮件。

这是我的get网址=> http://localhost/PHP-Slim-Restful/api/sendMail

即使我的error未被发送,我也没有得到任何email

低于是我的postman屏幕截图

enter image description here

RELIEF FOR ME:我能够登录但未能发送email

<?php
require 'config.php';
require 'Slim/Slim.php';


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';


\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->post('/login','login'); /* User login */
$app->get('sendMail','sendMail');  /* send Email */     


$app->run();

/************************* USER LOGIN *************************************/
/* ### User login ### */
function login() {
      // Code for login API route, it is there with slim api folder
 }


/* ### send email test ### */  

function sendMail()     
{

    // below credential is my actual mailtrap credential



    $mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.mailtrap.io';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '1fd562fc56d940';                 // SMTP username
    $mail->Password = 'af5ec48a984df1';                           // SMTP password
    $mail->SMTPSecure = 'PLAIN';                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 2525;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    // $mail->addAddress('ellen@example.com');               // Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

}


?>

下面是我使用mailtrap电子邮件凭证的整个项目文件夹

(DROPBOX链接):https://www.dropbox.com/s/rrl8j198nd1rfqh/PHP-Slim-Restful.zip?dl=0

请使用我项目的上述dropbox链接。

我认为我的API route没有达到function,但我可以点击login路由为什么?

偶数echo "hello world"未显示。我认为邮递员没有走这条路线?为什么

1 个答案:

答案 0 :(得分:0)

你错过了一些事情.. 添加以下

$mailer->isSMTP();
$mailer->SMTPDebug = 2;
$mailer->Debugoutput = 'html';
$mailer->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);