2015-08-08 104 views
0

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

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: [email protected]\r\n"; 
$headers .= "Reply-To: [email protected] \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(); 
} 
+0

你确定这是正确的'select $ user_email' – Shehary

+0

和你的意思'发送邮件到$ results ?????你wana发送电子邮件到mysql查询? – Shehary

+0

选择$ user_email只是为了表达想法。我主要询问查询后的评论部分。我知道我需要在邮件被接受时收到用户的电子邮件(我将用查询捕获并保存诸如$ results的内容),但我无法弄清楚的是如何实际发送邮件到该邮件用户在他们的电子邮件($结果) – JW500

回答

0

如果我理解正确的话,你可以做到以下几点:

try{ 

/*YOUR INSERT STATEMENT FROM ABOVE*/ 

$to=$results; 
$subject="Whatever you want your subject to be"; 
$headers = "From: [email protected]\r\n"; 
$headers .= "Reply-To: [email protected] \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. 
} 

让我知道如果这是你所追求的。

+0

这是假设您希望用户发送的电子邮件仅在他们的评论中被接受到数据库中。如果你想发送它,只要他们只是试图发表评论,那么它会有点不同。保持我张贴,如果我的答案有帮助,请检查它。 – jjones150

+0

我想你明白了。请查看我的更新代码,看看我是否正在实施您的答案。我需要添加诸如mail()之类的东西吗?执行php邮件或包含phpmail或任何东西?这是我第一次使用这个功能。我正在跳过Try/Catch,因为我没有做Do部分的任何东西... – JW500

+0

这看起来正确JW500。取出回声“你的信息被发送了一部分”。这是我的测试结果。如果您希望邮件只在用户的帖子被接受时才发送出去,请离开try catch部分。会发生什么事情是这样的:如果帖子进入数据库,那么邮件将被发送,否则什么都不会发生。所以你不必在捕捉部分有任何东西。这有点像一个其他状态,如果发生了某种事情,但是如果事情没有发生,不要做任何事情。 – jjones150