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

标签 php email slim

如果用户从 IOS 应用程序调用 API 到我的 Web 应用程序,我想向他们发送一封电子邮件。

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

在上面的 url 中 - “test@email.com” 是我要向其发送电子邮件的用户电子邮件,其中包含指向电子邮件正文中另一个 URL 的链接。例如,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 发送电子邮件?

最佳答案

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

正如其中一位评论者所建议的,您应该使用第三方包,例如 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);
    }     
}

关于php - 如何使用 Slim 框架的电子邮件功能发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35358804/

相关文章:

php - jQuery:使用动态添加的按钮删除最接近的 <tr>

javascript - $$var(在 php 中)等同于 javascript

php - 如何在php中的pdo准备中查找rowCount

powershell - 发送带有已在批处理文件中输入的凭据的电子邮件

php - 在后台运行 PHP 函数

php - Httpful:无法将响应解析为 JSON

php - 未定义索引 : (this is for multiple lines of code in 2 different documents)

email - Moqui-通过SSL连接连接到IMAP服务器吗?

mysql - 查询返回空结果但数据存在

javascript - 如何在 RESTful 中使用 VisualCaptcha 与 AngularJS 和 slimPHP