PHP未发送邮件

时间:2018-06-22 16:04:12

标签: php

我承认我对PHP不太了解。我发现此脚本可以发送电子邮件,并且在其他页面之一上也可以正常工作,但现在无法正常工作,我也不知道为什么。我唯一更改的是,我在脚本中间添加了“ if”语句来处理电子邮件,从而使电子邮件保持私密性。有人有什么想法吗?

<?php
/*if(!isset($_GET['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}*/ //This error seems to just get in the way. I am cutting it out.
$mailTo = $_GET['to'];
$subject = $_GET['subject'];
$name = $_GET['name'];
$visitor_email = $_GET['email'];
$message = $_GET['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

//If statements to change value from html to actual email addresses
switch($mailTo) {
    case "ED":      $mailTo = "example@example.com";
                    break;
    case "AS":      $mailTo = "example@example.org";
                    break;
    case "OM":      $mailTo = "example@example.org";
                    break;
    case "VC":      $mailTo = "example@example.org";
                    break;
    case "Pres":    $mailTo = "example@example.com";
                    break;
    case "VP":      $mailTo = "example@example.net";
                    break;
    case "Sec":     $mailTo = "example@example.com";
                    break;
    case "Treas":   $mailTo = "example@example.com";
                    break;
    case "Dir1":    $mailTo = "example@example.com";
                    break;
    case "Dir2":    $mailTo = "example@example.com";
                    break;
    case "Dir3":    $mailTo = "example@example.net";
                    break;
    case "Dir4":    $mailTo = "example@example.com";
                    break;
    case "Dir5":    $mailTo = "example@example.com";
                    break;
    case "Dir6":    $mailTo = "example@example.gov";
                    break;
    case "Dir7":    $mailTo = "example@example.com";
                    break;
    case "Dir8":    $mailTo = "example@example.com";
                    break;
    case "Dir9":    $mailTo = "example@example.com";
                    break;
}

$email_from = $visitor_email;//<== update the email address
$email_subject = $subject;
if($subject==="Please sign me up for the example Newsletter")
{
    $email_body = "$name has requested to be subscribed to the example 
Newsletter\n";
    $email_body .= "$name's email address is $visitor_email\n";
}
else
{
    $email_body = "*** The following message is from the user ***\n";
    $email_body = $message;
}
if($message!=="")
    $email_body .= "They have also added a custom message:\n $message";

$to = $mailTo;//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
if(mail($to,$email_subject,$email_body,$headers))
    echo "Mail submitted successfully";
else
    echo "Mail not sent";
//done. redirect to thank-you page.
//header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

在我的控制台中,没有任何错误,我正在使用AJAX,也没有任何错误,返回值始终是成功提交,并且正在提交的查询字符串有效。我将其范围缩小到PHP中必须弄乱的地方并使电子邮件无效。

这里是AJAX功能的JS。我的验证函数调用AJAX,然后将其发送到PHP。所有的JS已经调试完毕并且可以正常工作。

function ajaxFunction(caller)
{
    'use strict';
    console.log("1. *** BEGINNING AJAX FUNCTION ***");
    var ajaxRequest;

    try{
        // Real browsers
        ajaxRequest = new XMLHttpRequest();
    } catch(e) {
        // IE browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                // Something went wrong
                alert("Your browser broke");
                return false;
            }
        }
    }
    // Function that will recieve data sent from the server
    ajaxRequest.onreadystatechange = function() {
        console.log("2. The current ready state is: " + ajaxRequest.readyState);
        if(ajaxRequest.readyState === 4){
            var errorP = document.getElementById("errorP");
            console.log("Response Text is " + ajaxRequest.responseText);
            errorP.innerHTML = ajaxRequest.responseText;
        }
    }
    // Create the date object and send it to the server
    var to;
    var subject;
    var select = document.getElementById("select");
    var subj = document.getElementById("subject");
    var name = document.getElementById("name");
    var nameVal = name.value;
    var email = document.getElementById("email");
    var emailVal = email.value;
    var message = document.getElementById("message");
    var messageVal = message.value;
    if(caller==="newsletter")
        {
            to = "newsletter@al-van.org";
            subject = "Please sign me up for the Al-Van Newsletter";
        }
    else if(caller==="contact")
        {
            to = select.value;
            subject = subj.value;
        }
    var queryString = "?name=" + nameVal + "&email=" + emailVal + "&message=" + messageVal + "&to=" + to + "&subject=" + subject;
    console.log("6. The query string is: " + queryString);
    ajaxRequest.open("GET", "form-to-email.php" + queryString, true);
    ajaxRequest.send(null);
}

1 个答案:

答案 0 :(得分:0)

从许多不同来源进行研究后,我似乎已经发现了问题。似乎BlueHost使用了过滤器和阻止程序,阻止人们使用其服务器进行垃圾邮件或网络钓鱼。这意味着PHP脚本中的$from字段必须是服务器上的有效电子邮件才能发送。看来,当我使用其他$from字段时,电子邮件要么需要花费数小时,要么根本不需要花费。我不需要添加SMTP凭据或安装任何其他软件包即可完成此操作。感谢您的帮助,您的许多建议和想法对帮助我达成自己的答案非常有用。