PHP Mailer senders email is the same as the recipient

I am new to PHP and I am making a page where I have form on it. When the client fills all of the information from the input fields and clicks submit I am receiving a mail. The problem that I am facing is that even though I am entering different senders email address on the form , when I click the submit btn , the mail in my inbox that I am receving it turns out to be from me ( i.e. like I am sending it to myself -even though there is a different senders email address on the form). I have tried everything I really do not know where my problem is. In here I am using $$$ because I am hiding my real info.
Here is my code

<form action="send-mail.php" method="post">
            <div class="b-form__inputWrap">
                <label for="name">Name</label>
                <input name="name" id="name">
            </div>

            <div class="b-form__inputWrap">
                <label for="email">email</label>
                <input name="email" id="email">
            </div>

            <div class="b-form__inputWrap">
                <label for="name">Subject</label>
                <input name="subject" id="subject">
            </div>

            <div class="b-form__inputWrap">
                <label for="name">Message</label>
                <textarea name="message" id="message" name="message" placeholder="enter message"></textarea>
            </div>

            <button class="b-form__btn" type="submit" name="submit">Send</button>
        </form>

PHP

         <?php
         error_reporting(E_ALL);
         ini_set('display_errors', 1); 

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

         require 'vendor/phpmailer/phpmailer/src/Exception.php';
         require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
         require 'vendor/phpmailer/phpmailer/src/SMTP.php';

         if (isset($_POST["submit"])) {
         try {
         $mail = new PHPMailer(true);

         $mail->isSMTP();
         $mail->Host="smtp.gmail.com";
         $mail->SMTPAuth = true;
         $mail->Username="$$$$$$$$$$";
         $mail->Password = '$$$$$$$$$$$$$$$$$'; 
         $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            
         $mail->Port = 465;   

    
         $mail->setFrom($_POST['email']);
         $mail->addAddress('$$$$$$$$$$$$$');
         $mail->addReplyTo($_POST['email']);
    
         $mail->isHTML(isHtml:true);

         $mail->Subject = $_POST["subject"];
         $mail->Body = $_POST["message"];

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


      ?>

  • $mail->setFrom($_POST['email']); – sets sender from form and $mail->addAddress('$$$$$$$$$$$$$'); sets to where send email and I guess it’s your email. Switch in place these values.

    – 

  • $mail->addReplyTo($_POST['email']); is also email from form

    – 

  • Form labels, except for email points to name input element

    – 

  • 1

    No, you will have issues if sender does not match actual user of SMTP. from must be same as in $mail->Username. You can not send emails from different from as it’s security issue and most providers does not allows it. Image I send you email from = info@your_bank_name.com – very wrong!

    – 

  • 1

    @mp89 You can not. It’s security issue.

    – 

Leave a Comment