I have a website with a custom php contact form which I would like to submit the emails to an exchange 365 email address. It doesn't work and I realise it is due to microsoft 365 exchange needing authentication
I have allowed the IP in the exchange settings but to no luck
The contact form works fine for a secondary non-exchange email, i.e An email hosted on my server with POP3 settings - so I know the form is fine when not using an exchange email
I have done some research and found that perhaps it needs SMTP passwords and usernames in the contact form. I am rather new to PHP to be honest, is this the only way? it doesn't feel that safe to have the passwords etc in the code
Here is my code for the form, which works for non-exchange 365 email:
PHP:
<?php
$pagetitle = "Contact Us";
$description = "";
$keywords = "";
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = '[email protected]';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
?>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $message, $headers)) {
echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
Could someone please clarify if I can use this contact form to send emails to the exchange email address and if so what my options are?
I really do not want to use a 3rd party contact form like Contact Form 7 or anything like this if possible please.
Any help much appreciated,
Thanks
I have two suggestions for you.
Go to https://github.com/PHPMailer/PHPMailer and download it. Put it inside a directory called mail and then in the directory below use this code:
include("mail/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = false;
$mail->Username = "your-username";
$mail->Password = "your-password";
$mail->From = '[email protected]';
$mail->FromName = 'Your Name';
$mail->AddAddress('[email protected]');
$mail->IsHTML(true);
$mail->Subject = 'Subject line goes here';
$mail->Body = 'Email body goes here';
$mail->AltBody = 'Email body goes here';
$mail->Send();
Putting your SMTP authentication details in here is fine.
Next step is to go to https://www.mail-tester.com and copy the email address you see. Send an email to this address and then go back to mail-tester.com to check the result. This will tell you if you have any problems - maybe DKIM / SPF records missing. If this is the case then have a word with your host and they'll be able to help you out (or post a new question on here and somebody else can help you out)
Using PHPMailer is a much more robust way of sending emails than using PHP's mail() function.
EDIT - something like this ...
<?php
$pagetitle = "Contact Us";
$description = "";
$keywords = "";
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = '[email protected]';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
if (isset($_POST['submit']) && $human == '4') {
include("mail/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = false;
$mail->Username = "your-username";
$mail->Password = "your-password";
$mail->From = $from;
$mail->FromName = $name;
$mail->AddAddress($to);
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
$mail->Send();
echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
} elseif (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>