首页 > 解决方案 > 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);
}

标签: php

解决方案


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


推荐阅读