2

I'm trying to send an email in PHP that is written with CKEditor, or HTML. When the email is sent the HTML code appears in the email, I know it but the Headers already tried putting immense and none works.

Below is my code to send the email.

function mail_users($titulo, $conteudo){
$query = mysql_query("SELECT `Email`, `Nome` FROM `utilizadores` WHERE `Newsletter` = 'Ativada'");
while (($row = mysql_fetch_assoc($query)) !== false){
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
email($row['Email'], $titulo, "Olá " . $row['Nome'] . ",\n\n" .  $conteudo, $header);
}
}

3 Answers 3

1

You should do this way..

  • Move the header outside of the while loop.
  • You are doing a mistake in concatenation.
  • Function should be mail() instead of email() [Unless you have written a wrapper for the same]

The right way....

function mail_users($titulo, $conteudo){
    $query = mysql_query("SELECT `Email`, `Nome` FROM `utilizadores` WHERE `Newsletter` = 'Ativada'");
    
    $header = "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    while (($row = mysql_fetch_assoc($query)) !== false){
        
        mail($row['Email'], $titulo, "Olá " . $row['Nome'] . ",\n\n" .  $conteudo, $header);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The function should be mail why are using email() ? Did you make that change ?
i change this to mail($row['Email'], $titulo, "Olá " . $row['Nome'] . ",\n\n" . $conteudo, $headers); but doesn't work..
@LuisMiguelGT, So the mail() was the problem :)
0

Try this i think you just have to make header blank each time it enter into the loop.

and check

@mail($email, $subject, $message, $headers);

the last line has all the values accordingly .

function mail_users($titulo, $conteudo){
$query = mysql_query("SELECT `Email`, `Nome` FROM `utilizadores` WHERE `Newsletter` ='Ativada'");
while (($row = mysql_fetch_assoc($query)) !== false){
$header='';
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
@mail($row['Email'], $titulo, "Olá" . $row['Nome'] . ",\n\n" .  $conteudo, $header);
}
}

Comments

0

try this

function mail_users($titulo, $conteudo)
{
    $header = "MIME-Version: 1.0\r\n";
    $header .= "From: [email protected]";
    $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $query = mysql_query("SELECT `Email`, `Nome` FROM `utilizadores` WHERE `Newsletter` = 'Ativada'");
    while($row = mysql_fetch_assoc($query))
    {

         mail($row['Email'], $titulo, "Olá " . $row['Nome'] . ",\n\n" .  $conteudo, $header);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.