Reputation: 171
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?
Upvotes: 2
Views: 16323
Reputation: 1210
Yeah you can easily send mail from slim framework using PHPMailer.
Here is full code of index.php
, that will make your sense that what actually the code is doing.
<?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="[email protected]";
$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="[email protected]";
$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();
Upvotes: 0
Reputation: 8688
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);
}
}
Upvotes: 6