Hi guys, this is my first post on the forum and I am in need of some help. I am a PHP beginner and I have been trying to come up with a solution to my issue for a while now. I am trying to make a system where a user registers and after the submit button is pressed a welcome message is displayed on the screen along with a confirmation email. This is where I am struggling. I got my registeration system working however integrating this with the confirmation email is very difficult and I don't understand how to structure this at all. I would be very grateful is somebody could help me out. Thank you!

`<?php
include 'form.html.php';
require_once('SMTP.php');
require_once('PHPMailer.php');
require_once('Exception.php');
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;

//insert new user
if (isset($_GET['addform']))

{
    include '../admin/includes/db.inc.php';
    try
    {
        $sql = 'INSERT INTO author SET name = :name, email = :email, password = :password';
        $s = $pdo->prepare($sql);
        $s->bindvalue(':name', $_POST['name']);
        $s->bindvalue(':email', $_POST['email']);
        $s->bindvalue(':password', md5($_POST['password']. '')); //MY SALT IS MEANT TO BE WITHIN THE '' AT THE END
        $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Error with registration.';
        include 'error.html.php';
        exit();
    }

    $mail=new PHPMailer(true); // Passing `true` enables exceptions

try {
    //settings
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host='smtp.gmail.com';
    $mail->SMTPAuth=true; // Enable SMTP authentication
    $mail->Username=''; // SMTP username - MY DETAILS GO HERE 
    $mail->Password=''; // SMTP password - MY DETAILS GO HERE 
    $mail->SMTPSecure='ssl';
    $mail->Port=465;

    $mail->setFrom(''); //MY DETAILS GO HERE  

    //recipient
    $mail->addAddress('');     // Add a recipient //MY DETAILS GO HERE  

    //content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject='Registeration Successful';
    $mail->Body='Thank You for registering with us. We hope to see you soon! <b>Happy Cooking</b>';
    $mail->AltBody='This is the body in plain text for non-HTML mail clients';

    $mail->send();

    echo 'Message has been sent';
} 
catch(Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: '.$mail->ErrorInfo;
}



    $authorid = $pdo->lastInsertId();
    try
    {
        $sql = 'INSERT INTO authorrole SET 
        authorid = :authorid, 
        roleid = :roleid';
        $s = $pdo->prepare($sql);
        $s->bindvalue(':authorid', $authorid);
        $s->bindvalue(':roleid', 'User');
        $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Error assigning selected role to user.';
        include 'error.html.php';
        exit();
    }
    header('Location:welcome.html.php');
}`

Recommended Answers

You should tell us what's not working. A quick look at the code doesn't tell me what is broken.

Jump to Post

The process/method of sending email varies with each site and host. You'll need to determine the method used for your current host. I can't tell you this. You must get that information from your hosting provider.

-> When you do your first email via code, don't mix it in with …

Jump to Post

All 5 Replies

You should tell us what's not working. A quick look at the code doesn't tell me what is broken.

commented: The main issue is that the email doesn't automatically send. The email functionality doesn't work but the registration process does. +0

The main issue here is that I am trying to create a registeration system which sends an email to the user once the form has been submitted. At present, the user is only able to sign up but no email is sent as confrimation. This is where my problem lies and I am not sure on how to fix it. Sorry for any confusion caused. Thank you

The process/method of sending email varies with each site and host. You'll need to determine the method used for your current host. I can't tell you this. You must get that information from your hosting provider.

-> When you do your first email via code, don't mix it in with all your other code at first. Write a small thing that sends you a test email so you know you got the method/steps correct for your site/host.

commented: Hi there, I have done this within a separate file and I can confirm that it does work. I just don't know how to integrate it with registration form +0

I have managed to make the email functionality work on its own, however integrating it with the registeration form is the main issue as I don't know how to put the two different pieces of code together to create a registeration form where the user fills in the form and it sends an email to myself as confirmation that the registeration was successful. The client used to host this is known as PHPMailer. I would be grateful on any advice. Thank you!

Here is the code which works, that sends an email:

<?php
require_once('SMTP.php');
require_once('PHPMailer.php');
require_once('Exception.php');

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

$mail=new PHPMailer(true); // Passing `true` enables exceptions

try {
    //settings
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host='smtp.gmail.com';
    $mail->SMTPAuth=true; // Enable SMTP authentication
    $mail->Username=''; // SMTP username - MY DETAILS
    $mail->Password=''; // SMTP password - MY DETAILS 
    $mail->SMTPSecure='ssl';
    $mail->Port=465;

    $mail->setFrom('MY EMAIL GOES HERE', 'Customer ');

    //recipient
    $mail->addAddress(''MY EMAIL GOES HERE', 'Customer ');     // Add a recipient

    //content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject='Registeration Successful';
    $mail->Body='Thank You for registering with us. We hope to see you soon! <b>Happy Cooking</b>';
    $mail->AltBody='This is the body in plain text for non-HTML mail clients';

    $mail->send();

    echo 'Message has been sent';
} 
catch(Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: '.$mail->ErrorInfo;
}

?>

So this is "it doesn't work" code/question. I see in the top post at line 58, 59 the error message and ErrorInfo but no mention this was seen along with the ErrorInfo.

Beyond sharing this information you know to insert var_dump where needed to see if all is ready for the $mail->send(); on line 53.

Tutorial at https://www.w3schools.com/php/func_var_var_dump.asp

I can't send this without noting that one person left out they succeeded on their home server but not on the actual host. Details like that are needed.

commented: I'm sorry, I don't understand what you're trying to say as I am a beginner at PHP. ] +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.