0

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 ?

Tzi138
  • 1
  • 1
  • 1
    Do you have a mail server? `mail()` does not actually send mail, it looks in the php.ini for the mail server you have configured for it to use. If you are a windows user, you definitely dont have a mail server installed, Look to use `phpMailer` to send mail – RiggsFolly Dec 07 '21 at 14:13
  • I have a windows 2019 server using xampp for the webserver So there is probably no mail configured – Tzi138 Dec 07 '21 at 14:17
  • Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174). Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187) You should always use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenating user provided values into the query. Never trust ANY user input! – RiggsFolly Dec 07 '21 at 14:20

2 Answers2

0

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()

Ovab
  • 56
  • 1
  • 7
0

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.

E-telier
  • 766
  • 5
  • 15