Home > Mobile >  Send a mail from a registration form with PHP
Send a mail from a registration form with PHP

Time:12-08

Is it possible to send a mail from the code?

I want to make a registration and the credentials should be sent to the user again.

but simply no email arrives

what am I doing wrong

Here is the whole code

<?php

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

require 'C:/xampp/htdocs/Kulinarik/PHPMailer-master/src/Exception.php';
require 'C:/xampp/htdocs/Kulinarik/PHPMailer-master/src/PHPMailer.php';
require 'C:/xampp/htdocs/Kulinarik/PHPMailer-master/src/SMTP.php';

$con= mysqli_connect('localhost','root','123456', 'userdata',);

$email = $_POST['email'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$password = rand(0, 999999);


$result = $con->query("SELECT email FROM signup WHERE email = '$email'");
if($result->num_rows == 0) {
    $sql = "INSERT INTO signup (firstname, lastname, email, password) VALUES ('$firstname', '$lastname', '$email', '$password')";

    $mail = new PHPMailer();

    |
    |
    |
    |
    |
    |



    if ($con->query($sql) === TRUE) {
        header("Location: Login.html");
      } else {
        echo "Error: " . $sql . "<br>" . $con->error;
      }
} 
else {
    echo "User ist bereits registriert";
}


?>

I would really appreciate your help

EDIT 1 i added the phpmailer to my file and loaded it into the code but how am i going to send an email with it ?

CodePudding user response:

I assume you're a windows user and you don't have a mail server installed, if so I'd recommend using phpmailer PHPmailer link, it's much easier to use and gives way more options then the regular mail()

CodePudding user response:

PHP indeed has a mail() function, but using it with a simple String for its subject and text is bound to fail (it will either be considered as spam by your recipient, or won't be sent at all by your own server).

Properly using mail() required to set corrects headers and encoding. As said in the comments, it would be easier to use the PHPMailer library instead.

  • Related