0

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: [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(); 
} 
6
  • you sure this is right select $user_email Commented Aug 8, 2015 at 3:47
  • and what you mean send mail to $results????? you wana send email to mysql query? Commented Aug 8, 2015 at 3:48
  • The select $user_email is just a for this post to convey idea. I am mainly asking about the commented part after that query. I know I need to get the user email when they're post is accepted (which I will capture with a query and save in something like $results), but what I can't figure out is how to actually send the mail to that user at their email ($results) Commented Aug 8, 2015 at 3:50
  • it will be better if you convert your idea into working code so we can see what's not working and it's not hard to fetch data from db, load into variables and send email, just by searching you can get alot of result with working examples, start with google, SO is not code writing service. Commented Aug 8, 2015 at 3:57
  • I see a lot of info about the function, but nothing on how to call/implement it into if(isset($_POST ['submit'])). That's why I ended up here. Do I just type mail(); where I have the commented area in my example? Commented Aug 8, 2015 at 4:00

1 Answer 1

0

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

This is assuming you want the user sent an email only in their comment is accepted into the database. If you want it sent if they simply attempt to post comment, then it would be a little different. Keep me posted and if my answer helps, please check it.
I think you get it. Please see my updated code to see if I am implementing your answer right. Do I need to add something like mail(); to execute the php mail or include phpmail or anything? It's my first time using this function. I'm skipping the Try/Catch since I don't have anything for the Do part...
That looks right JW500. Take out the echo "your message was sent part". That was left over from my testing. If you want the mail only to go out if the user's post is accepted then leave the try catch part. What will happen is this: if the post gets into the database, then mail will be sent, otherwise nothing will happen. So you don't have to have anything in the catch part. this is kind of like an if else statemnet where if something happens do a certain behavior, but if that something doesn't happen, don't fo anything.
That did the trick (without the Try/Catch stuff). I don't need Try/Catch because this whole code block only runs if the post is accepted which makes try/catch redundant. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.