正在获取自定义php联系人表单以向Exchange 365帐户发送电子邮件

我有一个网站与定制的php联系人表格,我想提交的电子邮件到一个交换365的电子邮件地址。它不工作,我意识到这是因为Microsoft 365 Exchange需要身份验证

我已在交换设置中允许该IP,但没有成功

联系人表单适用于第二个非交换电子邮件,即托管在我的服务器上且具有POP3设置的电子邮件-因此,我知道该表单在不使用交换电子邮件时也适用

我做了一些研究,发现它可能需要联系人表单中的SMTP密码和用户名。老实说,我对PHP还很陌生,这是唯一的方法吗?在代码中包含密码等并不安全

以下是我的表单代码,适用于非Exchange 365电子邮件:

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 = 'hello@mydomain.co.uk'; 
    $subject = "Enquiry from Visitor " . $name;
    $human = ($_POST['human']);

   $headers = 'From: ' . $email . "
" .
   'Reply-To: ' . $email . "
" .
   '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>';
    }
?>

谁能澄清一下我是否可以使用此联系人表单将电子邮件发送到Exchange电子邮件地址,如果可以,我的选择是什么?

我真的不想使用像Contact Form 7这样的第三方联系人表单,如果可能的话,请使用类似这样的表单。

如有任何帮助,非常感谢

谢谢


解决方案

我对您有两点建议。

转到https://github.com/PHPMailer/PHPMailer并下载。将其放入名为mail的目录中,然后在下面的目录中使用以下代码:

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     = 'you@domain.com';
$mail->FromName = 'Your Name';

$mail->AddAddress('recipient@domain.com'); 

$mail->IsHTML(true);

$mail->Subject  =  'Subject line goes here';

$mail->Body     =  'Email body goes here';
$mail->AltBody  =  'Email body goes here';

$mail->Send();

在此处输入您的SMTP身份验证详细信息即可。

下一步是转到https://www.mail-tester.com并复制您看到的电子邮件地址。向此地址发送电子邮件,然后返回mail-tester.com查看结果。这将告诉您是否有任何问题-可能DKIM/SPF记录丢失。如果是这种情况,那么与你的主人谈谈,他们将能够帮助你(或者在这里发布一个新问题,其他人可以帮助你)

使用PHPMailer发送电子邮件比使用PHP的mail()函数可靠得多。

编辑-类似于...

<?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 = 'hello@mydomain.co.uk'; 
    $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>';
    }

?>

相关文章