How to send email using Slim framework email functionality?

14,642

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('[email protected]', 'BadgerDating.com');
    $mail->addAddress($id);
    $mail->addReplyTo('[email protected]', '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 [email protected].
        </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);
    }     
}
Share:
14,642
Ohm
Author by

Ohm

Updated on July 17, 2022

Comments

  • Ohm
    Ohm almost 2 years

    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/[email protected]

    In the above url - "[email protected]" 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/[email protected]_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?