0

New here, in advance, thank you. I have form to insert data into a table, working. After insert, the mail() should send a notification to that specific client. mail() works if I hard code an email address but not when the address comes form the db:

mail('email-from-table','Subject','Body');
Chris
  • 1
  • 1
  • need to see more code than that –  Nov 02 '15 at 20:13
  • Welcome to Stack Overflow. You will need to expand on this and provide more of the code. Specifically, what variables are you using, how are you collecting the details from your Database, what is your Query? What are the expected results? Please edit your post and include more details. – Twisty Nov 02 '15 at 20:15
  • can you echo out all your fields to check formatting for and slashes ect. – C.Wetherell Nov 02 '15 at 20:20
  • Standard db connection and insert statement. The idea then: is to collect the email address ($email_to = $email_to = $row_getFam['fs_email'];) and then mail('$email_to','',''); - am I under engineering this? Thx – Chris Nov 02 '15 at 20:20
  • whats the value of the email-from-table variable and any error when it fails? – Lucky Chingi Nov 02 '15 at 20:21
  • No error and no email arrives – Chris Nov 02 '15 at 20:22
  • We need to see the data coming from the db. Also why does the data come from the db? If you are writing it to the db shouldn't the data already be in the PHP.. – chris85 Nov 02 '15 at 20:23
  • Show more code, preferably where you fetch the email from db. – Akshay Nov 02 '15 at 20:23

2 Answers2

2

the problem is your using single quotes around your variable. only varibles in double quotes will be evaluated.

so your trying to send email to an actull email address of $email_to

mail('$email_to','',''); 

should be

mail("$email_to",'',''); 

or more commonly

mail($email_to,'',''); 
0

You need to collect the email addresses from your database and iterate over your results. Also mail() is the least verbose; I would advise using PEAR or another SMTP library.

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT email FROM table")) {
    printf("<p>Select returned %d rows.</p>\r\n", $result->num_rows);
    echo "<ul>\r\n";
    while($row = $result->fetch_assoc()){
        if(mail($row['email'], 'My Spam Subject', 'My Spam Email')){
            echo "<li>Spam sent to {$row['email']}</li>\r\n";
        } else {
            echo "<li>Mail failed.</li>\r\n";
        }
    }
    echo "</ul>\r\n";
    /* free result set */
    $result->close();
}
$mysqli->close();
?>
Twisty
  • 30,304
  • 2
  • 26
  • 45