Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP电子邮件脚本的一些问题_Php_Mysql - Fatal编程技术网

PHP电子邮件脚本的一些问题

PHP电子邮件脚本的一些问题,php,mysql,Php,Mysql,我对脚本有一些问题,基本上我希望脚本做的是获取表单数据并发布到数据库(它做得很好),然后我希望它发送一封感谢电子邮件,这是不起作用的一点,只是没有通过。另外,从我所知道的,脚本将发送电子邮件,即使它无法执行脚本,我该如何解决这个问题 我对PHP相当陌生,刚刚发现它能做什么,所以我非常感谢您的帮助 <?php error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', TRUE); ini_set('display_

我对脚本有一些问题,基本上我希望脚本做的是获取表单数据并发布到数据库(它做得很好),然后我希望它发送一封感谢电子邮件,这是不起作用的一点,只是没有通过。另外,从我所知道的,脚本将发送电子邮件,即使它无法执行脚本,我该如何解决这个问题

我对PHP相当陌生,刚刚发现它能做什么,所以我非常感谢您的帮助

<?php 

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$name = $_POST['name'];
$email = $_POST['email'];


require_once('Connections/connection.php');
mysql_select_db($database_connection);

$query = "INSERT INTO mailing_list ( name, email)
        VALUES ( '$name', '$email');";
mysql_query($query);

mysql_close();

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

 $email_from = 'mailinglist@myemail.com';
    $email_subject = "Welcome to our mailinglist";
    $email_body = "
$name,

Welcome to our mailing list, we will now keep you updated on whats going on here.

To unsubscribe go to.".


  $to = "$email";
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $email \r\n";
  mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.php');


// 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;
  }
}


 ?>

这是您使用PDO重新编写的代码。我还删除了回复值,因为我不知道你可以使用用户的电子邮件作为回复,电子邮件客户端可能会阻止它

<?php 

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$name = $_POST['name'];
$email = $_POST['email'];

//I recommend rewriting this to NOT use mysql_* functions and instead use PDO
//The documentation is located at http://www.php.net/manual/en/book.pdo.php
/*
require_once('Connections/connection.php');
mysql_select_db($database_connection);
*/
$host = 'host_name';
$dbname = 'database_name';
$user = 'user_name';
$pass = 'user_pass';
try
{
    $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
}
catch(PDOException $e)
{  
    echo $e->getMessage();  
}

$query = "INSERT INTO mailing_list ( name, email) VALUES ( '?', '?');";

$sth = $DB->prepare($query);

//This way only a success will send an email
if($sth->execute(array($name, $email)))
{
    if(IsInjected($email))
    {
        echo "Bad email value!";
        exit;
    }

    $email_from = 'mailinglist@myemail.com';
    $email_subject = "Welcome to our mailinglist";
    $email_body = "
    $name,

    Welcome to our mailing list, we will now keep you updated on whats going on here.

    To unsubscribe go to.".


    $to = $email;
    $headers = "From: $email_from \r\n";
    //I don't know that you can use the to email as the reply-to,
    //most emails will probably block that.
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.php');
}else{
    echo "Failed to insert into database.";
    exit;
}



// Function to validate against any email injection attempts
// Stole this from w3schools, because it is well done. link is at the bottom
    function spamcheck($field)
    {
        //filter_var() sanitizes the e-mail
        //address using FILTER_SANITIZE_EMAIL
        $field=filter_var($field, FILTER_SANITIZE_EMAIL);

        //filter_var() validates the e-mail
        //address using FILTER_VALIDATE_EMAIL
        if(filter_var($field, FILTER_VALIDATE_EMAIL))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

 ?>

你确定它不会进入垃圾邮件文件夹吗?真的应该避免使用mysql_*函数,因为它们现在已经贬值了。。它们不再得到维护,并且已经开始使用。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,.您在哪里设置
$visitor\u email
?您确实需要阅读一些关于插入内容的教程,您需要执行查询,但也需要检查查询结果-即,它是否有效。。。非常感谢您在这方面的帮助和时间,电子邮件似乎仍然无法通过…尝试注释header()调用,而不是回显传递到mail()调用中的值。这可能会帮助您确定是否发送了错误的值。非常感谢您的帮助,我错过了$to=$email;-我有时会像个傻瓜!:-)每个人都会遇到这种情况,成为一个傻瓜有时只是生活的一部分