LuisMiguelGT
LuisMiguelGT

Reputation: 47

Send Email with PHP Code

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);
}
}

Upvotes: 2

Views: 103

Answers (3)

Satish Sharma
Satish Sharma

Reputation: 9635

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);
    }
}

Upvotes: 0

ashutosh
ashutosh

Reputation: 81

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);
}
}

Upvotes: 0

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);
    }
}

Upvotes: 1

Related Questions