Send php mail to user if form submitted

末鹿安然 提交于 2020-01-07 03:24:09

问题


I am trying to use the phpmail function to send an email to a user if their post was accepted. First I am capturing the user email in a query if a form is submitted, but I'm not sure how to implement the mail function. Should it be something like this?:

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:


If I understand you correctly, you could do the following:

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.
}

Let me know if this is what you were going for.



来源:https://stackoverflow.com/questions/31889426/send-php-mail-to-user-if-form-submitted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!