Eimear
Eimear

Reputation: 1

PHPmailer fuction only will receive from one email

I have searched for this answer for the past few days and have tried everything to make this work so please help.

I had made a contact form but I will only receive an email if I use the email that the form is submitting to. If I use any other email it will still said the email is sent but when I check my inbox there is nothing there. Same as my junk and sent folders. Although if I complete the form with my email I will get an email in my inbox. (So the form email and the email address submitting to is the same).

This is my index.php

    <!DOCTYPE html>
<html>
<head>
    <title>Send an Email</title>
</head>
<body>

    <center>
        <h4 class="sent-notification"></h4>

        <form id="myForm">
            <h2>Send an Email</h2>

            <label>Name</label>
            <input id="name" type="text" placeholder="Enter Name">
            <br><br>

            <label>Email</label>
            <input id="email" type="text" placeholder="Enter Email">
            <br><br>

            <label>Subject</label>
            <input id="subject" type="text" placeholder=" Enter Subject"> 
            <br><br>

            <p>Message</p>
            <textarea id="body" rows="5" placeholder="Type Message"></textarea>
            <br><br>

            <button type="button" onclick="sendEmail()" value="Send An Email">Submit</button> 
        </form>
    </center>

    <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        function sendEmail() {
            var name = $("#name");
            var email = $("#email");
            var subject = $("#subject");
            var body = $("#body");

            if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) {
                $.ajax({
                   url: 'sendEmail.php',
                   method: 'POST',
                   dataType: 'json',
                   data: {
                       name: name.val(),
                       email: email.val(),
                       subject: subject.val(),
                       body: body.val()
                   }, success: function (response) {
                        $('#myForm')[0].reset();
                        $('.sent-notification').text("Message Sent Successfully.");
                   }
                });
            }
        }

This is my sendEmail.php

<?php

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

if(isset($_POST['name']) && isset($_POST['email'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];

require 'vendor/autoload.php';


$mail = new PHPMailer();


    //Server settings

    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.mail.yahoo.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = '[email protected]';                     //SMTP username
    $mail->Password   = 'aaa';                               //SMTP password
    $mail->SMTPSecure = 'tsl';            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`



   

    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->setFrom($email, $name);
    $mail->addAddress("[email protected]");
    $mail->Subject = ("$email ($subject)");
    $mail->Body = $body;

    if($mail->send()){
        $status = "success";
        $response = "Email is sent!";
    }
    else
    {
        $status = "failed";
        $response = "Something is wrong: <br>" . $mail->ErrorInfo;
    }

    exit(json_encode(array("status" => $status, "response" => $response)));
}

?>
    

This may sound confusing so please ask any questions if you don't understand.

Upvotes: 0

Views: 827

Answers (2)

Isaac Lim&#243;n
Isaac Lim&#243;n

Reputation: 2058

You can call multiple times:

$mail->AddAddress('[email protected]', 'Juan');
$mail->AddAddress('[email protected]', 'Pepe');

I recommend use https://mailtrap.io/ for testing emails

Upvotes: 0

Synchro
Synchro

Reputation: 37710

Two problems. Firstly you're forging the from address, which is very probably the cause of your problems. This is well documented, and I recommend following the advice given in the PHPMailer contact form example.

A minor issue is that you're setting this to a non-existent value:

$mail->SMTPSecure = 'tsl';

That should be:

$mail->SMTPSecure = 'tls';

Upvotes: 1

Related Questions