获取自定义 php 联系表格以发送电子邮件至 Exchange 365 帐户

getting custom php contact form to send email to exchange 365 account

我有一个带有自定义 php 联系表格的网站,我想将电子邮件提交到 exchange 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 . "\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>';
    }
?>

有人可以澄清一下我是否可以使用此联系表向交易所电子邮件地址发送电子邮件,如果可以,我的选择是什么?

我真的不想使用第 3 方联系表,如联系表 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 记录。如果是这种情况,请与您的房东谈谈,他们将能够帮助您(或者post在这里提出一个新问题,其他人可以帮助您)

使用 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>';
    }

?>