Skip to content

Commit

Permalink
Merge pull request #53 from thecodingaviator/mailer
Browse files Browse the repository at this point in the history
mail!
  • Loading branch information
gordoncd authored Jul 20, 2023
2 parents 3416d6c + 996d901 commit 1c2eb6a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
40 changes: 40 additions & 0 deletions mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

if (session_status() === PHP_SESSION_NONE) {
session_start();
}

require_once 'vendor/autoload.php';
include "utils/config.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

function sendMail($subject, $content, $recipient){
$mail = new PHPMailer(true);

try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'c3-database-repo@coastcowconsumer.com';
$mail->Password = '!!83~market~ROLL~table~55!!';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('c3-database-repo@coastcowconsumer.com', 'C3 Database Repo');
$mail->addAddress($recipient);

$mail->Subject = 'Subject';
$mail->Body = $content;
$mail->send();
echo "Mail has been sent successfully!";
return true;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
return false;
}
}


?>
17 changes: 8 additions & 9 deletions resetpassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
session_start();

include "utils/config.php";

include "mail.php";
function generateTemporaryPassword()
{
$length = 8;
Expand Down Expand Up @@ -42,18 +42,17 @@ function generateTemporaryPassword()
// Send the email with the temporary password
$subject = "Password Reset";
$message = "Your temporary password is: $temporaryPassword";
$headers = "From: your_email@example.com";

if (mail($email, $subject, $message, $headers)) {
$error = "An email with the temporary password has been sent to your email address.";
if (sendMail($subject, $message, $email)) {
$update = "An email with the temporary password has been sent to your email address.";
} else {
$error = "Failed to send the email. Please try again later.";
$update = "Failed to send the email. Please try again later.";
}
} else {
$error = "There was an error resetting your password. Please try again later.";
$update = "There was an error resetting your password. Please try again later.";
}
} else {
$error = "Invalid userID or email. Please check your details and try again.";
$update = "Invalid userID or email. Please check your details and try again.";
}
}
?>
Expand All @@ -74,10 +73,10 @@ function generateTemporaryPassword()
</head>

<body>
<?php if (!empty($error)): ?>
<?php if (!empty($update)): ?>
<div class="error-div">
<p id="error-message">
<?php echo $error; ?>
<?php echo $update; ?>
</p>
</div>
<?php endif; ?>
Expand Down
3 changes: 2 additions & 1 deletion signup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
session_start();

include "utils/config.php";
include "mail.php";

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
header("Location: dashboard.php");
Expand Down Expand Up @@ -48,7 +49,7 @@
$headers = "From: your_email@example.com"; // Replace with your own email address

// Uncomment the following line to send the email
if (mail($to, $subject, $message, $headers)) {
if (sendMail($subject, $message, $to)) {
$_SESSION['email_sent'] = true;
} else {
$_SESSION['email_sent'] = false;
Expand Down

0 comments on commit 1c2eb6a

Please sign in to comment.