如何将php mail()函数转换为smtp

时间:2018-09-30 10:51:45

标签: php email smtp

我使用ajax和php mail()为WordPress创建了一个简单的联系表单,但我的最大邮件将发送到垃圾邮件文件夹中。我的代码有什么问题?或任何其他解决方案?像SMTP。

<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// Subject
if (empty($_POST["subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $subject = $_POST["subject"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}

//receiver email address
$EmailTo = "info@mydomain.com";

$Subject = $subject;
$form  =  $name;
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$headers  = "From: " .($email) . "\r\n";
$headers .= "Reply-To: ".($email) . "\r\n";
$headers .= "Return-Path: ".($email) . "\r\n";;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
// send email
$success = mail($EmailTo, $Subject, $Body, $headers);


// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

1 个答案:

答案 0 :(得分:0)

将电子邮件视为垃圾邮件有多种原因。通常,它与发送邮件的服务器有关,除了邮件本身的内容或标头之外。例如。 DNS可能未正确配置,网络上有一些工具可能会帮助您进行故障排除,但不幸的是,我找不到几年前使用的工具。 Google可能会带给您一些检查DNS配置的工具。 关于该主题的堆栈溢出已经有一些讨论,请检查this thread以避免重复讨论。

我建议使用PHPMailer之类的库来处理邮件。