php – Send attachment after form submission to user email and post data to admin

I have a form which on submit must send the data collected to the admin email (i grab it via post), and the user gets a gift – an email with an attachment is sent to him. How can i do this ?

Html code:

<div class="modal">
                            <form id="program-four-modal" method="post" action="makepdf.php">

                                <!-- Hidden Required Fields -->
                                <input type="hidden" name="project_name" value="">
                                <input type="hidden" name="admin_email" value="">
                                <input type="hidden" name="form_subject" value="Vreau cadoul - Lorem ipsum">
                                <!-- END Hidden Required Fields -->


                                <input type="text" name="NumePrenume" placeholder="Nume Prenume..."
                                    title="Nume Prenume" required><br>
                                <input type="email" name="Email" placeholder="E-mail..." title="E-mail" required><br>
                                <input type="tel" name="NrDeTelefon" placeholder="Nr. de telefon..."
                                    class="checkphone" title="Nr de telefon" required>

                                <div class="program-four-modal-send-wrapper">
                                    <button type="submit" class="program-four-modal-send">Trimite</button>


                        <a href="#program-four-modal" class="btn program-four-btn" role="button" rel="modal:open">I want the gift</a>

Here i grab the variables from the form via post. Currently both user and admin get the same email , .

Php code:


use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

require_once __DIR__ . '/vendor/autoload.php';

// Grab variables
$subject = $_POST('form_subject');
$name = $_POST('NumePrenume');
$email = $_POST('Email');
$phone = $_POST('NrDeTelefon');

// Create PDF
$data = '';
$data .= '<h1>Your details</h1>';

// Add data
$data .= '<strong>Name</strong> ' . $name . '<br />';
$data .= '<strong>Email</strong> ' . $email . '<br />';
$data .= '<strong>Phone</strong> ' . $phone . '<br />';

    // Instantiation and passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       = '';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = '';                     // SMTP username
        $mail->Password   = 'password';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        // Recipients
        $mail->setFrom('', 'Gift Form');
        $mail->addAddress($email);     // Add a recipient

        // Attachment

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $data;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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