Search code examples
phpmysqlemailphpmailerheidisql

PHP mail error domain missing or malformed<EOL>


I'm trying to send a mail to a customers email thats set in my database.

$subject = 'Testing PHP Mail';
$txt = 'This mail is sent using the PHP mail function';
$headers = "FROM: [email protected]";
$query = ("SELECT email FROM ps_customer where id_customer = 2");
$result = $dbc->query($query);
$row = $result->fetch_assoc();
echo $row['email'];
$to_email = (string)$row;

//while ($row = $result->fetch_assoc()) {
//    echo $row['email'];
//    $to_email = (string)'$row <@>';
    if (mail($to_email, $subject, $txt, $headers)) {
        echo "send";
    } else {
        echo "failed";
    }

this is my code that's need to send the email to the email out of the database.

but when i try to send it i get the error: : domain missing or malformed


Solution

  • You only want the address, and it's the only field you're fetching, so I'd go with:

    $query = ("SELECT email FROM ps_customer where id_customer = 2");
    $result = $dbc->query($query);
    $row = $result->fetch_row();
    $to_email = $row[0];
    

    No need to use an assoc array for this.

    You've not mentioned it, but the messages you send will probably be rejected. You're sending via mail(), which means you are not sending through gmail's servers, but you're using a gmail from address. This is forgery and will mean your messages will be bounced or spam filtered. You can't solve this using mail() (other than by not using gmail for your from address); you would need to send using SMTP via gmail using PHPMailer (that you tagged this question with).