Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Email PHP | Not sending/receiving email

kelloggs_cpt

New Coder
Good day,

Not sure is my mail_form.php is correct? I am not receiving the email once i click "Accept" (submit). Your help would be greatly appreciated:

[CODE lang="php" title="mail_form"]<?php

use PHPMailer\PHPMailer\PHPMailer;
require __DIR__ . '/vendor/autoload.php';

$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];


if (empty($name)) {
$errors[] = 'Name is empty';
}

if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}


if (!empty($errors)) {
$allErrors = join('<br/>', $errors);
$errorMessage = "<p style='color: red;'>{$allErrors}</p>";
} else {
$mail = new PHPMailer();

// specify SMTP credentials
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'P@ssword123';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom($email, 'Mailtrap Website');
$mail->addAddress('[email protected] ', 'Me');
$mail->Subject = 'RSVP Church';

// Enable HTML if needed
$mail->isHTML(true);

$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", nl2br($message)];
$body = join('<br />', $bodyParagraphs);
$mail->Body = $body;

echo $body;
if($mail->send()){

header('Location: thank-you.html'); // redirect to 'thank you' page
} else {
$errorMessage = 'Oops, something went wrong. Mailer Error: ' . $mail->ErrorInfo;
}
}
}

?>[/CODE]
[CODE lang="html" title="html code | contains form"]<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jemma's Confirmation RSVP</title>
<link rel="stylesheet" href="church.css">
</head>

<body>
<form action="/mail_form.php" method="POST" id="church-form">
<div class="top">
<nav>
<a href="index.html" class="mybutton">Home</a>
</nav>
</div>
<div class="form">


<div class="info">
<h1>RSVP</h1>
<h2>for the Confirmation Service of</h2>
<h1>Name</h1>
<p class="line">________________________________________</p>
<h2>The Details</h2>
<p>Sunday, 28 February 2021</p>
<p>10:00 AM</p>
<p>Please RSVP by 15 February 2021</p>
<h2>Confirmation Service</h2>
<p><a href="#" target="_blank">
Calvyn Protestant Church Diep River</a></p>
<p>10 Keswick St, Elfindale</p>
<p class="line">________________________________________</p>

<input type="text" value="name" placeholder="Name">
<input type="email" value="email" placeholder="Email">
</div>
<?php echo((!empty($errorMessage)) ? $errorMessage : '') ?>
<button type="submit" class="accept" value="Send">Accept</button>
<button type="submit" class="regret" value="Send">Regret</button>
</div>
</form>

<script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
<script>
const constraints = {
name: {
presence: {allowEmpty: false}
},
email: {
presence: {allowEmpty: false},
email: true
}
};

const form = document.getElementById('church-form');

form.addEventListener('submit', function (event) {
const formValues = {
name: form.elements.name.value,
email: form.elements.email.value
};

const errors = validate(formValues, constraints);

if (errors) {
event.preventDefault();
const errorMessage = Object
.values(errors)
.map(function (fieldValues) {
return fieldValues.join(', ')
})
.join("\n");

alert(errorMessage);
}
}, false);
</script>



</body>

</html>[/CODE]
 
Hello kelloggs_cp,

Sorry for the very late reply! Did you solve it?

Do you send the mail from a Gmail account? If so, you must activate a configuration on your Gmail so it can be used by external programs such as PHP.
Do you use your own mailserver? If so, does it work by itself? Many internet providers block the port 25 and therefore mailservers can not send mails.
 
Back
Top Bottom