如何使用Slim框架电子邮件功能发送电子邮件? [英] How to send email using Slim framework email functionality?

查看:91
本文介绍了如何使用Slim框架电子邮件功能发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户从IOS应用向我的Web应用进行API调用,我想向其发送电子邮件。

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

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

在上述网址中-测试@ email.com是我要向其发送电子邮件的用户电子邮件,其中包含指向电子邮件正文中另一个URL的链接。例如, http://testurl.com/resetpassword/test@email.com_44646464646

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

我的Web应用程序使用Slim框架,我打算在其中定义以下路线:

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
}

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

How can I send my email using Slim?

推荐答案

Slim没有任何内置的邮件功能。毕竟,它是一个 Slim微框架。

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

如评论者所建议的那样,您应该使用第三方软件包,例如 PHPMailer Swift Mailer

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

在PHPMailer中:

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

这篇关于如何使用Slim框架电子邮件功能发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆