简体   繁体   中英

How to pull email address form user table and send email to that address using $_SESSION[user_id] variable?

How to pull email address form user table and send email to that address using $_SESSION[user_id] variable ?

At the end of the payment process I would like to send confirmation email to the user. User id is stored in session and I would like to send confirmation to the email adress stored in user table.

<?php
$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

how to using query insert appropriate email associated with $_SESSION[user_id] variable?

$q = "SELECT email from users where id='".$_SESSION['user_id']."'";
$r = mysqli_query ($dbc, $q);
$to = "email";
$subject = "Confirmation";
$body = "Hi,\n\nConfirmation?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

I have info email successfully sent but email never arrives... during the registration process I sent email using :

$body = "Thank you for registering at <...>. Welcome to digital world of knowledge.";
mail ($_POST['email'], 'Registration Confirmation', $body, 'From: admin@ttt.com');

You have to write a select query based on the user id to get email something like this

$sql = "SELECT email from user where user_id='".$_SESSION[user_id]."'";

then use that email to perform mail functionality

You need to obtain the email address from the user object:

$user  = User::getByID($_SESSION[user_id]);
$email = $user->getEmail();

This code is just exemplary, it depends how your user-object works. If you don't have one, then my answer is a suggestion that you create yourself a user object.

With out getting an email address from the Database , its not possible to do that,

or else

After the user login success, you can set one more session varaible

$_SESSION['email'] = 'abc@example.com'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM