如果表单已提交,则将php邮件发送给用户

时间:2015-08-08 03:45:31

标签: php phpmailer

我正在尝试使用phpmail函数向用户发送电子邮件,如果他们的帖子被接受了。首先,如果提交了表单,我会在查询中捕获用户电子邮件,但我不确定如何实现邮件功能。它应该是这样的吗?:

if(isset($_POST ['submit'])){

//Some query to get the user email address
$results = $dbh->prepare("select $user_email from wp_users where
wp_users.ID=$user_ID");


$to=$results;
$subject="Whatever you want your subject to be";
$headers = "From: WHATEVER@WHATEVER.COM\r\n";
$headers .= "Reply-To: WHATEVER@WHATEVER.COM \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message= "WHATEVER MESSAGE";
mail ($to , $subject , $message, $headers);
echo "Your message has been sent";


$insrt = "INSERT INTO table(ID,
        text,
        VALUES (
        :ID,
        :text)";
$stmt = $dbh->prepare($insrt);
$stmt->bindParam(':ID', $user_ID, PDO::PARAM_INT);       
$stmt->bindParam(':text', $_POST['post_text'], PDO::PARAM_STR); 
$stmt->execute(); 
} 

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以执行以下操作:

try{

/*YOUR INSERT STATEMENT FROM ABOVE*/

$to=$results;
$subject="Whatever you want your subject to be";
$headers = "From: WHATEVER@WHATEVER.COM\r\n";
$headers .= "Reply-To: WHATEVER@WHATEVER.COM \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message= "WHATEVER MESSAGE";
mail ($to , $subject , $message, $headers);
echo "Your message has been sent";
}
Catch($error){
Do whatever you want here. Add another mail function to let you know that     the post was not accepted, etc.
}

请告诉我这是否适合你。