PHP contact form sending but not receiving emails


PHP contact form sending but not receiving emails



I have created a contact form using PHP to send the data. The form seems to send fine as there are no errors and success message appears, however I am not receving the emails into the designated inbox.



I am using PHPMailer as I originally tried just using the php 'mail' command which I now understand is a bit hit and miss.



I cannot see why I would not be receving the emails so I would be very grateful for any help that could be given.



I'm quite new to PHP so please be patient with me :)


<?php

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

$msg = "";

if (isset($_POST['submit'])) {
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

function sendemail ($to, $from, $body) {
$mail = new PHPMailer(true);
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = 'Contact Form - Email';
$mail->Body = $body;
$mail->IsHTML(false);

return $mail->send();
}

$email = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];

if (sendemail('gareth.langley1@gmail.com', $email, $subject, $body)) {
$msg = 'Email has been sent, thank you!';
} else
$msg = 'Email failed, please try again later';

}



?>




<title>Test Contact Form Using PHPMailer</title>

<body>


×

Get in touch



Email address:

Subject:

What would you like to ask us?

Submit







</body>







There are a lot of reasons that your email cannot reach the mailbox. First thing you can check your SMTP server's outgoing log. Another thing you can do is to check using third-party analysis service such as mail-tester.com.
– Raptor
Jul 3 at 9:24





Don’t try to force the “from” address, that is a pretty sure way to get your mails classified as spam if you are not actually sending “via” this account (meaning via actual SMTP credentials), resp. from a domain that is allowed to do so. Either use SMTP mode and properly authorize against that very account - or specify it as the reply-to address only (and live with whatever e-mail your system uses as default sender in that case.)
– CBroe
Jul 3 at 9:36






Thank you both for your answers. I have ran a test through mailtester.com and it has come back with the following message 'Your email client, plugin or extension said "sent", and it sure did. But your email got stopped just after, and never got sent. Why? Your SMTP or web host is probably blocking your emails. Quite common. Get in touch with them now to find out.' So it looks like its SMTP related. Honestly I do not really understand what this means and how I can resolve it :( Could you point me in the right direction please?
– Coder
Jul 3 at 10:00





2 Answers
2



You're passing 4 parameters to a function that only needs 3.



function sendemail ($to, $from, $body)


function sendemail ($to, $from, $body)



sendemail('gareth.langley1@gmail.com', $email, $subject, $body)


sendemail('gareth.langley1@gmail.com', $email, $subject, $body)



Try editing like this :


if (sendemail('gareth.langley1@gmail.com', $email, $body)) {
$msg = 'Email has been sent, thank you!';
} else
$msg = 'Email failed, please try again later';
}



Most likely you server doesn't have a Mail Transport Agent (MTA) installed, or it does but it's not configured properly. Examples of MTAs for Linux: sendmail, postfix, exim4, qmail.



Installing and configuring an MTA can get complicated.



Can you send emails from the command line on the same server?






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?