reddragon88
New member
I am currently designing a website in vs code that uses a login and registration form, mysql database using phpmyadmin via mamp on macos with m1 chip. The problem is, once I implemented PHPMailer, the content of my site won't load.
Here is the registration page:
Here is the code for the sendwelcomeemail() function:
Here is the registration page:
Code:
<?php
include_once('templates/header.php');
include('config/dbh.php');
include('includes/functions.php');
include('mail/welcomemail.php');
//declare variables
$firstName = $lastName = $phoneNumber = $emailAddress = $companyName = $numberOfUsers = $password = $confirmPassword = "";
$firstErr = $lastErr = $phoneErr = $emailErr = $companyErr = $numUsrErr = $pwErr = $cpwErr = $stmtErr = "";
$errors = array('firstErr' => '', 'lastErr' => '', 'phoneErr' => '', 'emailErr' => '', 'companyErr' => '', 'numUsrErr' => '', 'pwErr' => '', 'cpwErr' => '', 'stmtErr' => '');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// first name
if (!empty($_POST["firstName"])) {
$firstName = test_input($_POST["firstName"]);
if (!preg_match("/^[a-zA-Z\s]+$/", $firstName)) {
$errors['firstErr'] = " * Only letters and white space allowed";
}
} else {
$errors['firstErr'] = " * Required";
}
// last name
if (!empty($_POST["lastName"])) {
$lastName = test_input($_POST["lastName"]);
if (!preg_match("/^[a-zA-Z\s]+$/", $lastName)) {
$errors['lastErr'] = " * Only letters and white space allowed";
}
} else {
$errors['lastErr'] = " * Required";
}
// phone number
if (!empty($_POST["phoneNumber"])) {
$phoneNumber = test_input($_POST["phoneNumber"]);
if (!preg_match("/^[0-9]{10}+$/", $phoneNumber)) {
$errors['phoneErr'] = " * Must contain 10 digits";
}
} else {
$errors['phoneErr'] = " * Required";
}
// email
if (!empty($_POST["emailAddress"])) {
$emailAddress = test_input($_POST["emailAddress"]);
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$errors['emailErr'] = " * Invalid email address";
} else if (emailExists($conn, $emailAddress) == "true") {
$errors['emailErr'] = " * Email is already in use";
}
} else {
$errors['emailErr'] = " * Required";
}
// company
if (!empty($_POST["companyName"])) {
$companyName = test_input($_POST["companyName"]);
if (!preg_match("/^[a-zA-Z0-9\s]+$/", $companyName)) {
$errors['companyErr'] = " * Only letters, numbers, and white space allowed";
}
} else {
$errors['companyErr'] = " * Required";
}
// number of users
if (!empty($_POST["numberOfUsers"])) {
$numberOfUsers = test_input($_POST["numberOfUsers"]);
} else {
$errors['numUsrErr'] = " * Required";
}
// password
if (!empty($_POST["password"])) {
$password = test_input($_POST["password"]);
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$minchar = "8";
if (!$uppercase || !$lowercase || !$number || strlen($password) < $minchar) {
$errors['pwErr'] = " * Requires one uppercase, one lowercase, one number, and at least 8 characters in length";
}
} else {
$errors['pwErr'] = " * Required";
}
// confirm password
if (!empty($_POST["confirmPassword"])) {
$confirmPassword = test_input($_POST["confirmPassword"]);
if ($password !== $confirmPassword) {
$errors['cpwErr'] = " * Passwords must match";
}
} else {
$errors['cpwErr'] = " * Required";
}
if (!array_filter($errors)) {
//generate random secure token
$token = openssl_random_pseudo_bytes(16);
//convert binary to hexadecimal
$token = bin2hex($token);
if (createUser($conn, $firstName, $lastName, $phoneNumber, $emailAddress, $companyName, $numberOfUsers, $password, $token) == "true") {
sendWelcomeEmail($firstName, $emailAddress, $token); // here is where the welcome email is sent
} else {
echo "Please try again.";
}
}
} //end of post check
?>
<section class="get-started" id="get-started">
<div class="container container-fluid">
<div class="row">
<div class="col-md-5 content">
<form name="register" id="register" method="POST" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div class="form-content">
<h2 class="form-title">Create Account</h2>
<div class="err-txt"><?= $errors['stmtErr'] ?></div>
<hr class="mb-3">
<div class="form-row">
<div class="col form-group">
<label for="firstName">First Name </label><span><?= $errors['firstErr'] ?></span>
<input name="firstName" id="firstName" class="form-control" type="text" placeholder="First Name" value="<?= htmlspecialchars($firstName) ?>">
</div>
<div class="col form-group">
<label for="lastName">Last Name </label><span><?= $errors['lastErr']; ?></span>
<input name="lastName" id="lastName" class="form-control" type="text" placeholder="Last Name" value="<?= htmlspecialchars($lastName) ?>">
</div>
</div>
<div class="form-group">
<label for="phoneNumber">Phone Number </label><span><?= $errors['phoneErr']; ?></span>
<input name="phoneNumber" id="phoneNumber" class="form-control" type="phoneNumber" placeholder="Phone Number" value="<?= htmlspecialchars($phoneNumber) ?>">
</div>
<div class="form-group">
<label for="emailAddress">Email Address </label><span><?= $errors['emailErr']; ?></span>
<input name="emailAddress" id="emailAddress" class="form-control" type="email" placeholder="Email" value="<?= htmlspecialchars($emailAddress) ?>">
</div>
<div class="form-group">
<label for="companyName">Company Name </label><span><?= $errors['companyErr']; ?></span>
<input name="companyName" id="companyName" class="form-control" type="text" placeholder="Company" value="<?= htmlspecialchars($companyName) ?>">
</div>
<div class="form-group">
<label for="numberOfUsers">Number of Users </label><span><?= $errors['numUsrErr']; ?></span>
<select class="form-control" id="numberOfUsers" name="numberOfUsers">
<option selected disabled>Choose...</option>
<option value="1" <?php if (isset($numberOfUsers) && $numberOfUsers == "1") echo "selected" ?>>1</option>
<option value="2" <?php if (isset($numberOfUsers) && $numberOfUsers == "2") echo "selected" ?>>2</option>
<option value="3" <?php if (isset($numberOfUsers) && $numberOfUsers == "3") echo "selected" ?>>3</option>
<option value="4" <?php if (isset($numberOfUsers) && $numberOfUsers == "4") echo "selected" ?>>4</option>
<option value="5" <?php if (isset($numberOfUsers) && $numberOfUsers == "5") echo "selected" ?>>5</option>
<option value="6" <?php if (isset($numberOfUsers) && $numberOfUsers == "6") echo "selected" ?>>6</option>
<option value="7" <?php if (isset($numberOfUsers) && $numberOfUsers == "7") echo "selected" ?>>7</option>
<option value="8" <?php if (isset($numberOfUsers) && $numberOfUsers == "8") echo "selected" ?>>8</option>
<option value="9" <?php if (isset($numberOfUsers) && $numberOfUsers == "9") echo "selected" ?>>9</option>
<option value="10 <?php if (isset($numberOfUsers) && $numberOfUsers == "10") echo "selected" ?>">10</option>
<option value="11 <?php if (isset($numberOfUsers) && $numberOfUsers == "11") echo "selected" ?>">11</option>
<option value="12 <?php if (isset($numberOfUsers) && $numberOfUsers == "12") echo "selected" ?>">12</option>
<option value="13 <?php if (isset($numberOfUsers) && $numberOfUsers == "13") echo "selected" ?>">13</option>
<option value="14 <?php if (isset($numberOfUsers) && $numberOfUsers == "14") echo "selected" ?>">14</option>
<option value="15 <?php if (isset($numberOfUsers) && $numberOfUsers == "15") echo "selected" ?>">15</option>
<option value="16 <?php if (isset($numberOfUsers) && $numberOfUsers == "16") echo "selected" ?>">16</option>
</select>
</div>
<div class="form-group">
<label for="password">Password </label><span><?= $errors['pwErr']; ?></span>
<input name="password" id="password" class="form-control" type="password" placeholder="Password" value="">
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password </label><span><?= $errors['cpwErr']; ?></span>
<input name="confirmPassword" id="confirmPassword" class="form-control" type="password" placeholder="Confirm Password" value="">
</div>
<button id="signupButton" name="register_user" type="submit" class="mt-3 btn btn-custom btn-block">Submit</button>
<hr class="mb-3">
<p class="pt-2 text-center small">Already have an account? <a href="login.php"><b>Sign In Here</b></a></p>
</div>
</form>
</div>
</div>
</div>
</section>
<?php include('templates/footer.php') ?>
Here is the code for the sendwelcomeemail() function:
Code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
function sendWelcomeEmail($firstName, $emailAddress, $token)
{
$fname = $firstName;
$usrEmail = $emailAddress;
$tkn = $token;
$host = 'mail.*COMPANYNAME*.com;'; // removed for security purposes
$from = 'mailer@*COMPANYNAME*.com';
$fromname = '*COMPANYNAME*';
$mailerpw = '*COMPANYPASSWORD*';
$subject = 'Verify your email address with *COMPANYNAME*';
$body = 'Welcome, ' . $fname . '!
Thank you for creating an account with *COMPANYNAME*.
Please verify your account and email address by clicking the link below.
<hr>
Please click this link to activate your account:
http://localhost:8888/*COMPANYNAME*/verify.php?email=' . $usrEmail . '&token=' . $tkn . '
';
$altbody = '
Welcome, ' . $fname . '!
Thank you for creating an account with *COMPANYNAME*.
Please verify your account and email address by clicking the link below.
Please click this link to activate your account:
http://www.*COMPANYNAME*.com/verify.php?email=' . $usrEmail . '&token=' . $tkn . '
';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = gethostbyname($host);
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = $from;
$mail->Password = $mailerpw;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->setFrom($from, $fromname);
$mail->addAddress($usrEmail, $fname);
$mail->Subject = ("$subject");
$mail->Body = $body;
$mail->AltBody = $altbody;
$mail->send();
echo 'message has been sent';
header("location: login.php?error=none");
} catch (Exception $e) {
echo "Message could not be sent. Mailer error: {$mail->ErrorInfo}";
}
}