0

I am using a php script that send email automatically to a logged in user when he access some pages in his account. The client table has two different emails in two different columns (mail1 and mail2), mail1 is used as username. The script can send to the primary email (the email he used to login), but can not send to his secondary email (mail2). I want the email to be sent to the his second email address stored in the other database column, not to the one he used to login.

Here is my code

<?php 
if(isset($_SESSION["mail1"]))
{
    $to      = $_SESSION["mail1"]; 
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: my@example.com' . "\r\n" .
        'Reply-To: my@dexample.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    echo 'Email Sent.';
}
?>

I works like this but when I change $to = $_SESSION["mail1"] to$to = $`$_SESSION["mail2"] it won't work, and i want it to work with mail2

How to make it send the email to mail2?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Balde
  • 3
  • 5

3 Answers3

1

You have to load the mail2 in your session memory, like JohnP said. After starting your session do this:

$_SESSION['mail2'] = $mail2;

where $mail2 may your result from a database request.

This database-request could look like this: Edited to prevent SQL injection, --> see André Laszlo's comment

$dbConnection= new mysqli("localhost", "my_user", "my_password", "dbname");
$stmt = $dbConnection->prepare('SELECT mail2 FROM users WHERE mail1 = ? LIMIT 1');
$stmt->bind_param('s', $mail1);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    $mail2 = $row;
}

User is your table in your database where the information is stored.

After doing this, it should work like you tried.

progNewbie
  • 4,362
  • 9
  • 48
  • 107
  • 1
    WARNING: Your query is vulnerable to sql injection. Please see http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – André Laszlo Oct 13 '14 at 12:27
0

@Balde, I am assuming that you are already retrieve email data from database & store in to $_SESSION["mail2"].

You can print session data to check $_SESSION["mail_2"] & then you need to change your if condition variable too i.e $_SESSION["mail1"] to $_SESSION["mail2"].

So code for $_SESSION["mail2"] look like below.

<?php 
if(isset($_SESSION["mail2"])) {
    $to      = $_SESSION["mail2"]; 
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: my@example.com' . "\r\n" .
        'Reply-To: my@dexample.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    echo 'Email Sent.';
} ?>
-2
<?php 


     if(isset($_SESSION['mail1']))
        {
        $mail1=$_SESSION['mail1'];
        $query=mysql_query("select * from table_name where username='$mail1'");
while($row=mysql_fetch_array($query))
{
$mail2=$row['mail2'];
}

            $to      = $mail2; 
            $subject = 'the subject';
            $message = 'hello';
            $headers = 'From: dev1@dev1.diiwal.com' . "\r\n" .
                'Reply-To: dev1@dev1.diiwal.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();

            mail($to, $subject, $message, $headers);

            echo 'Email Sent.';
        }
        ?>
Hara Prasad
  • 704
  • 6
  • 15