0

I am trying to send email from Mysql database record using PHP. Below is the code I have written so far. It works when I type an email address manually in $to like $to='myemailid@mail.com' but not working when i am trying to fetch email address from database. Below is my code. what's wrong I am doing

/*send mail*/

if ($_POST['do'] == 'mail') {
    $sql = "SELECT email FROM members where username='Mim'";
    $result = mysql_query($sql);

    if ($result->num_rows > 0) {
        $email_array = array();
        while ($row = $result->fetch_assoc()) {
            $email_array[] = $row["email"];
        }
    }
    $emails = implode(",", $email_array);
    $to = $emails;
    $subject = 'Marriage Proposalsssssssss';
    $from = 'no_reply@mail.com';

// To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
    $headers .= 'From: ' . $from . "\r\n" .
        'Reply-To: ' . $from . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
    $message = '<html><body>';
    $message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
    $message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
    $message .= '</body></html>';

// Sending email
    if (mail($to, $subject, $message, $headers)) {
        echo 'Your mail has been sent successfully.';
    } else {
        echo 'Unable to send email. Please try again.';
    }
}
catcon
  • 1,295
  • 1
  • 9
  • 18
Mithu
  • 665
  • 1
  • 8
  • 38
  • 1
    `mysql_*` lib does not offer object-oriented programming style. I think you mixing between `mysqli_*` and `mysql_*`. Try migrate your code to `mysqli_*` lib since `mysql_*` is already deprecated from PHP 5.5 and deleted from PHP 7. [mysqli_query()](https://www.php.net/manual/en/mysqli.query.php) – catcon Nov 01 '19 at 06:06
  • Are you sending mail correctly, – Ahmed Ali Nov 01 '19 at 06:07

0 Answers0