Search code examples
phphtmlphpmailer

How would I get the phone number and name displayed on emails I recieve - PHPMailer


So the emails I recieve only shows the subject, who it was sent from, who it was sent to but not the senders name or phone number, this is not done locally but from where my website is hosted. I want it to show the senders name and phone number after the body. This is my code:

<?php

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

require 'vendor/autoload.php';

    $mail = new PHPmailer();

    $mail->Host = "";
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->Username = "";
    $mail->Password = "";
    $mail->SMTPSecure = "tls";
    $mail->Port = 587;

    $mail->addAddress('');
    $mail->setFrom($_POST['email']);
    $mail->Subject = $_POST['subject'];
    $mail->isHTML(true);
    $mail->Body = $_POST['message'];
    $mail->Name = $_POST['name'];
    $mail->Phone = $_POST['number'];


    if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
    echo 'Message has been sent';

    }
        //echo $mail->ErrorInfo;
?>

This is my html:

<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
    <div class="input-grids">
        <input type="text" name="name" placeholder="Full name or Business" 
        class="contact-input" required=""/>

        <input type="email" name="email" placeholder="Your email" class="contact- 
        input" required=""/>

        <input type="text" name="subject" placeholder="Subject" class="contact- 
        input" required=""/>

        <input type="text" name="number" placeholder="Phone number" 
        class="contact-input" />
    </div>
    <div  class="form-input">
        <textarea name="message" placeholder="Type your message here" 
        required=""></textarea>
    </div>
    <div  class="form-input mb-5">
        <label class="container"><p>Send me a copy <a href="#">privacy policy. 
        </a></p>
        <input type="checkbox">
        <span class="checkmark"></span>
        </label>
    </div>
    <button class="btn submit">Submit</button>
</form>

I am not sure if I need to do something like this:

if (isset($_POST['submit'])) {
    require 'vendor/autoload.php';

    $mail = new PHPmailer();

    $mail->Host = "";
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->Username = "";
    $mail->Password = "";
    $mail->SMTPSecure = "tls";
    $mail->Port = 587;

    $mail->addAddress('');
    $mail->setFrom($_POST['email']);
    $mail->Subject = $_POST['subject'];
    $mail->isHTML(true);
    $mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
}

$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];

and just give my submit button this attribute

<button class="btn submit" name="submit">Submit</button>

Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.


Solution

  • You basically want to create a $message variable and assign that to $mail->Body.

    The second example you have shown is close but you need to make sure that the below is within your $_POST['submit'] if statement.

    $body = $_POST['message'];
    $name = $_POST['name'];
    $phone = $_POST['number'];
    

    Like so:

        if (isset($_POST['submit'])) {
            require 'vendor/autoload.php';
    
        $body = $_POST['message'];
        $name = $_POST['name'];
        $phone = $_POST['number'];
    
            $mail = new PHPmailer();
    
            $mail->Host = "";
            $mail->isSMTP();
            $mail->SMTPAuth = true;
            $mail->SMTPDebug = SMTP::DEBUG_OFF;
            $mail->Username = "";
            $mail->Password = "";
            $mail->SMTPSecure = "tls";
            $mail->Port = 587;
    
            $mail->addAddress('');
            $mail->setFrom($_POST['email']);
            $mail->Subject = $_POST['subject'];
            $mail->isHTML(true);
            $mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
    $response = $mail->send(); //to actually send the email
        }
    

    For: Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.

    You could subject the form via ajax and listen to the $response value. If its 1 then good to go, else there was an issue.