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

时间:2017-03-24 20:45:55

标签: php email exchange-server contact contact-form

我有一个自定义php联系表单的网站,我想将电子邮件提交到Exchange 365电子邮件地址。它不起作用,我意识到这是由于需要身份验证的Microsoft 365交换

我已在交换设置中允许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>';
    }
?>

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

如果可能的话,我真的不想使用第三方联系表格,如联系表格7或类似的任何内容。

任何帮助非常感谢,

由于

1 个答案:

答案 0 :(得分:1)

我有两个建议给你。

转到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>';
    }

?>