PHP Mailer shows no errors but doesn't send the email

非 Y 不嫁゛ 提交于 2019-12-24 03:51:24

问题


I'm creating a booking form as a favour but have decided to send mail via the domain, not the server. This is mainly for better security and less limits in responses and data transfers.

I've been working on this for the last few days and trying to teach myself how to get it to work. I now have a very simple working example which can be seen here below.

This is the simple booking form:

<form method="post" name="process.php" action="process.php">
<p>Name:</p><br><input type="text" name="name"><br><br>
<p>Email Address:</p><br><input type="email" name="email"><br><br>
<br>
<input type="submit" name="submit" value="Send Email">

Then in process.php I have this working code:

<?php

use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit'])) 
{
    // Values need to be santiised

    $name = $_POST['name']; //Name of the person requesting a booking
    $email = $_POST['email']; //Email of the person requesting a booking    

    require '../vendor/autoload.php';

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'test@handler.net';
    $mail->Password = '[PASSWORD]';



    $mail->setFrom('test@handler.net'); // All emails would be sent from the handler.net domain to the bookings email of the other domain. (Using test to test, will be noreply)

    $mail->addAddress('bookings@salon.com'); // Recipient of the email should be the bookings address, this won't change.

    $mail->addReplyTo($email); // The reply to address will be the email address of the user who submitted the booking enquiry.

    $mail->addBCC('outbox@handler.net'); // This is to keep a record of all emails that have been sent. Responses will also be saved to a CSV file.

    $mail->Subject = 'Booking Request'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

    $mail->isHTML(TRUE);
    $mail->Body = 'Booking request from '.$name.' with email '.$email; // Shows the salon the form response via the email and when they reply a new thread should start in order to compose a new email to reply to the email of the form submitter. 

    if(!$mail->send()) { // Send the email.
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo 'Message has been sent.';
    }
}
?>

The code above works and sends the emails from the right address, to the right address. That was on a site in public_html/testing and has been moved to a different site in public_html/booking so the relative paths will be the same. The only files within this directory are index.php (the form) and send.php (the process file with confirmation message)

For some reason this new code with all of the form values will not send. I'm honestly not too sure as to why it won't work now so any pointers at all would be massively appreciated.

<?php

use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit'])) 
{
    // Values need to be santiised

    $forename = $_POST['forename'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $service = $_POST['service'];
    $date = $_POST['date'];
    $time = $_POST['time'];

    require '../vendor/autoload.php';

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'noreply@handler.net';
    $mail->Password = '[PASSWORD]';



    $mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.

    $mail->addAddress('bookings@salon.com'); // Email form responses sent to bookings@salon.com
    $mail->addReplyTo($email); // Reply to the user who submitted the form.

    $mail->addBCC('outbox@handler.net'); // Store record of all emails sent via the system.

    $mail->Subject = 'Booking Request'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

    $mail->isHTML(TRUE);
    $mail->Body = '

    Booking request from '.$forename.' with email '.$email;'
    Test Values: $forename $surname $email $phone $service $date $time



    if(!$mail->send()) { // Send the email.
      echo '';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo '';
    }
}
?>

I don't need the echo statements at the end, I just need the email to send following this sort of format:

<img src="https://via.placeholder.com/300x150" width="15%">
<p><b>Name:</b> $forename $surname</p>
<p><b>Email:</b> $email</p>
<p><b>Phone:</b> $phone</p>
<p><b>Service:</b> $service</p>
<p><b>Date:</b> $date</p>
<p><b>Time:</b> $time</p>

I'm just not sure why the emails now will not send when they have done before. Any pointers would be appreciated.

UPDATE

Here's the updated code with the progress made with thanks to Mr Perfect

<?php
mail("bookings@salon.com", "test", "message");
use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit'])) 
{
    // Values need to be santiised

    $forename = $_POST['forename'];
    $surname = $_POST['surname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $service = $_POST['service'];
    $date = $_POST['date'];
    $time = $_POST['time'];

    $message  = <<<DELIMETER
<img src="https://via.placeholder.com/300x150" width="15%">
    <p><b>Name:</b> {$forename} {$surname}</p>
    <p><b>Email:</b> {$email}</p>
    <p><b>Phone:</b> {$phone}</p>
    <p><b>Service:</b> {$service}</p>
    <p><b>Date:</b> {$date}</p>
    <p><b>Time:</b> {$time}</p>
DELIMETER;

    require '../vendor/autoload.php';

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 3;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'noreply@handler.com';
    $mail->Password = '[PASSWORD]';

    $mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.

    $mail->addAddress('bookings@salon.com','ADMIN'); // Email form responses sent to bookings@salon.com.

    $mail->addReplyTo($email); // Reply to the user who submitted the form.

    // $mail->addBCC('outbox@handler.net'); // Store record of all emails sent via the system.

    $mail->Subject = 'Booking Request | SUBMISSION'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

    $mail->isHTML(TRUE);
    $mail->Body    = $message;
    $mail->AltBody = $message;

    if(!$mail->send()) { // Send the email.
      echo '';
      echo '' . $mail->ErrorInfo; // I don't need to echo any errors because the submission page has the text above already.
    } else {
      echo '';
    }
}
?>

回答1:


You should look at your $mail->addAdress, $mail->addBCC and $mail->addReplyTo fields and follow the correct syntax for those fields.

Test the code below.

    <?php

    use PHPMailer\PHPMailer\PHPMailer;

    if(isset($_POST['submit'])) 
    {
        // Values need to be santiised

        $forename = $_POST['forename'];
        $surname = $_POST['surname'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $service = $_POST['service'];
        $date = $_POST['date'];
        $time = $_POST['time'];

        require '../vendor/autoload.php';

        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->Host = 'smtp.hostinger.com';
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = 'test@handler.net';
        $mail->Password = '[PASSWORD]';

        $mail->setFrom('test@handler.net','Bookings'); // Emails sent via Noreply.

        $mail->addAddress('bookings@salon.com',''); // Email form responses sent to bookings@salon.com

        $mail->addReplyTo($email,$forename.' '.$surname); // Reply to the user who submitted the form.

        $mail->addBCC('outbox@handler.net',''); // Store record of all emails sent via the system.

        $mail->Subject = 'Booking Request'; // Subject of the email sent to admin@handler.net that the form responses will be contained within.

        $mail->isHTML(TRUE);
        $mail->Body = <<<EOD
            Booking request from {$forename} with email {$email}.<br  />
            Contact details: <br />
            Full name: {$forename} {$surname}<br />
            Email: {$email} <br />
            Phone number: {$phone} <br />
            Service: {$service} <br />
            Date: {$date} {$time}
EOD;
        if(!$mail->send()) { // Send the email.
          echo '';
          echo 'Mailer error: ' . $mail->ErrorInfo;
        } else {
          echo '';
        }
    }
    ?>



回答2:


This block of code is wrong, some quotes are missing

$mail->Body = '

    Booking request from '.$forename.' with email '.$email;'
    Test Values: $forename $surname $email $phone $service $date $time

To enable variable replacement on a string use double quotes, and you won't need to concatenate variables with dot(.), also this make possible to use escaped caracters like \n, try to adjust your code like this:

$mail->Body = "Booking request from $forename with email $email\n" .
              "Test Values: $forename $surname $email $phone $service $date $time";



回答3:


Firstly we have to view the error so therefore you have to set

$mail->SMTPDebug = 0;

into

$mail->SMTPDebug = 3;

So you can get that error to post against that error.



来源:https://stackoverflow.com/questions/57813178/php-mailer-shows-no-errors-but-doesnt-send-the-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!