How to send email using Slim framework email functionality?

跟風遠走 提交于 2019-12-24 15:31:33

问题


I would like to send an email to a user if they make an API call from an IOS app to my web application.

i.e. http://testurl.com/forgotpassword/test@email.com

In the above url - "test@email.com" is the user email to whom I want to send an email with a link to another URL in the email body. For example, http://testurl.com/resetpassword/test@email.com_44646464646

My web application uses the Slim framework, within which I plan to define the following routes:

$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
}

How can I send my email using Slim?


回答1:


Slim doesn't have any built-in mail functionality. After all, it is a "Slim" microframework.

As one of the commenters suggested, you should use a third-party package like PHPMailer or Swift Mailer.

In 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);
    }     
}


来源:https://stackoverflow.com/questions/35358804/how-to-send-email-using-slim-framework-email-functionality

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!