如何使用Slim框架电子邮件功能发送电子邮件?

时间:2016-02-12 09:19:38

标签: php email slim

如果用户从IOS应用程序拨打API到我的网络应用程序,我想向用户发送电子邮件。

即。 http://testurl.com/forgotpassword/test@email.com

在上面的网址中 - “test@email.com”是我要向其发送电子邮件的用户电子邮件,其中包含指向电子邮件正文中其他网址的链接。例如,http://testurl.com/resetpassword/test@email.com_44646464646

我的Web应用程序使用Slim框架,我计划在其中定义以下路由:

$app->get('/forgotpassword/:id', function($id) use ($app) {
    // from here i want to send email 
}

$app->get('/resetpassword/:id/:param', function($id, $param) use ($app) {
    // from here i want to update password
}

如何使用Slim发送电子邮件?

2 个答案:

答案 0 :(得分:4)

Slim没有任何内置邮件功能。毕竟,它是一个" Slim" microframework。

正如其中一位评论者建议的那样,您应该使用第三方软件包,例如PHPMailerSwift Mailer

在PHPMailer中:

$app->get('/forgotpassword/:id', function($id) use ($app) {
    $param = "secret-password-reset-code";

    $mail = new PHPMailer;

    $mail->setFrom('admin@badger-dating.com', 'BadgerDating.com');
    $mail->addAddress($id);
    $mail->addReplyTo('no-reply@badger-dating.com', 'BadgerDating.com');

    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = 'Instructions for resetting the password for your account with BadgerDating.com';
    $mail->Body    = "
        <p>Hi,</p>
        <p>            
        Thanks for choosing BadgerDating.com!  We have received a request for a password reset on the account associated with this email address.
        </p>
        <p>
        To confirm and reset your password, please click <a href=\"http://badger-dating.com/resetpassword/$id/$param\">here</a>.  If you did not initiate this request,
        please disregard this message.
        </p>
        <p>
        If you have any questions about this email, you may contact us at support@badger-dating.com.
        </p>
        <p>
        With regards,
        <br>
        The BadgerDating.com Team
        </p>";

    if(!$mail->send()) {
        $app->flash("error", "We're having trouble with our mail servers at the moment.  Please try again later, or contact us directly by phone.");
        error_log('Mailer Error: ' . $mail->errorMessage());
        $app->halt(500);
    }     
}

答案 1 :(得分:2)

是的,您可以使用PHPMailer从苗条框架轻松发送邮件。

这里是index.php的完整代码,可以使您理解代码的实际作用。

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

//From PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Require autoload.php of PHPMailer
require_once '../vendor/autoload.php';

$app = new \Slim\App;

$app->post('/forgotpassword',function(Request $request, Response $response)
{
        $requestParamter = $request->getParsedBody();
        $email =  $requestParamter['email'];
        $id = $requestParamter['id'];
        sendVerificationEmail($email,$id);

});


//Function to send mail, 
function sendVerificationEmail($email,$id)
{      
    $mail = new PHPMailer;

    $mail->SMTPDebug=3;
    $mail->isSMTP();

    $mail->Host="smtp.gmail.com";
    $mail->Port=587;
    $mail->SMTPSecure="tls";
    $mail->SMTPAuth=true;
    $mail->Username="socialcodia@gmail.com";
    $mail->Password="12345";

    $mail->addAddress($email,"User Name");
    $mail->Subject="Verify Your Email Address For StackOverFlow";
    $mail->isHTML();
    $mail->Body=" Welcome to StackOverFlow.<b><b> Please verify your email adress to continue..";
    $mail->From="SocialCodia@gmail.com";
    $mail->FromName="Social Codia";

    if($mail->send())
    {
        echo "Email Has Been Sent Your Email Address";
    }
    else
    {
        echo "Failed To Sent An Email To Your Email Address";
    }


}


$app->run();