0

So i'm triyng to send emails using phpmailer (after committing a html form) to an email address provided by Hostinger.

Everything works fine, i also get the 'Mail was successfully sent!' message after hitting the submit button, but i don't see emails in the inbox.

No error alert displayed and SMTPDebug seems useful in this case. Also, this code works pretty fine in other websites (with different host password and username values).

php code

<?php

$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$subject = $_POST['subject'] ?? '';
$msg = $_POST['content'] ?? '';

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

  require 'vendor/autoload.php';

  $mail = new PHPMailer;

  try {

    $mail->Host='smtp.hostinger.es';
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Port=587;
    $mail->SMTPAuth=true;
    $mail->Username='emailLogin';
    $mail->Password='passwordLogin';

    $mail->setFrom($email, $name);  // who send the email
    $mail->addAddress('emailProvidedByHostinger'); // who recive the email

    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $msg;

    $mail->send();
        header('Location: contacts.php?mailsent=1');
        die(); // we don't want exactly anything after sending redirect header.
        } catch (Exception $e){
            echo '';
        }

?>

html code

                <form action="data.php" method="post">
                    <span class="contact">To contact us please compile this form and we will 
                                          answer you as soon as possible.
                    </span>
                    <ul class="form-content">
                    <?php
                        if (isset($_GET['mailsent']) && intval($_GET['mailsent']) == 1 ){
                            echo '<div class="messagesbox animate__animated animate__fadeInDown" style= "color: #1a5479;" >Mail was successfully sent!</div>';
                        }
                    ?>
                        <li class="form-input">
                            <input class="input" id="name" type="text" name="name" placeholder="Full name" required>
                        </li>
                        <li class="form-input">
                            <input class="input" id="mail" type="text" name="email" placeholder="E-mail" required>
                        </li>
                        <li class="form-input">
                            <input class="input" id="subject" type="text" name="subject" placeholder="Subject" required>
                        </li>
                        <li class="form-input">
                            <textarea class="input" id="comment" type="text" name="content" placeholder="Insert text" cols="30" rows="10" style="resize: none;" required></textarea>
                        </li>
                        <li class="form-input paddingBottom0">
                            <input class="input" id="form-button" type="submit" value="submit" onclick="sendEmail()">
                        </li>
                    </ul>
                </form>

I've talked to hostinger support and, from their side, everything is ok, so, i guess, i got to fix something in my code.

Anyone knows a solution?

Elle
  • 43
  • 6
  • I don't see any syntax issues with that code. And tbh, if the same code works on other servers for other mail providers, it sounds like it actually is an issue on the mail hosting end. If PHPMailer reports that it was successfully sent, then it's out of PHP's hands. – M. Eriksson May 12 '21 at 16:27
  • Actually they where able to send me an email to my hostinger email account – Elle May 12 '21 at 16:31
  • 1
    Sure, but when PHPMailer::send() returns true, what it actually means is that the mail server accepted the email and has put it in it's mail queue so it doesn't actually mean that the mail was sent to the receiver. Did they check the logs on their mail server to see what happened to those mails after they got added to their mail queue? If the server accepted the email, we can only assume that the code is correct, or that their server reports success on failure as well. – M. Eriksson May 12 '21 at 16:35
  • it was a very poor service support, so i don't think so. Thanks for the clarification about PHPMailer::send() – Elle May 12 '21 at 16:37
  • 1
    You should also double check that the email your sending didn't end up in some spam folder (or got deleted as spam, depending on what kind of protection the receiving mail server have). Those are quite common issues as well. – M. Eriksson May 12 '21 at 16:40
  • I did everything i know in order to find those emails..frustrating. Thanks a lot, if a possible solution comes to your mind, let me know! – Elle May 12 '21 at 16:44
  • This is the defacto checklist for everything mail-related: https://stackoverflow.com/a/24644450/231316 – Chris Haas May 12 '21 at 18:27

0 Answers0