user3456641
user3456641

Reputation: 11

send html in an email via php, can't seem to do it

I have a list of subscribers who I am trying to send a HTML type email to (essentially a news letter). But currently, it is just sending plainly and I can't seem to figure how to make it look better.

I've searched through other questions but can't seem to apply the answers to my own code, so could someone help? Code is shown below:

<?php

$user = "example"; 
$password = "example"; 
$host = "example"; 
$dbase = "example"; 
$table = "example"; 

$from= 'example';//specify here the address that you want email to be sent from

$subject= $_POST['subject'];
$body= $_POST['body'];

// Connection to DBase 
$dbc= mysqli_connect($host,$user,$password, $dbase) 
or die("Unable to select database");

$query= "SELECT * FROM $table";
$result= mysqli_query ($dbc, $query) 
or die ('Error querying database.');

while ($row = mysqli_fetch_array($result)) {
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email= $row['email'];

$msg= "Dear $firstname $lastname,\n$body";
mail($email, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}

mysqli_close($dbc);
?>

So I have a message box where I can type a message but how would I make that message box, html style? So I can add in H2 etc tags? Or just make the email like a html newsletter

Upvotes: 1

Views: 40

Answers (3)

raghuveer999
raghuveer999

Reputation: 709

Use PHP Mailer Class instead of mail()

$mail  = new PHPMailer();// defaults to using php "mail()"

$mail->SetFrom("frommail");
    $mailAdmin->AddAddress("tomail");   
    $mailAdmin->Subject = $subject;                 
    $mailAdmin->MsgHTML($message);
    $mailAdmin->IsHTML(true);
    $mailAdmin->Send();

You just need to include a class file phpmailer class file wich can be downloaded online.

Upvotes: 0

For that you need to set your headers as text/html

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

and pass that to your mail() function.

Something like this..

mail($to, $subject, $msg, $headers);

Your Modified Code

$msg= "<h3>Dear $firstname $lastname</h3>,<br><br><b>$body</b>"; //<-- Added some basic formatting
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Yourname <'.$from.'>' . "\r\n";
mail($to, $subject, $msg, $headers);

Refer : PHP Manual

Upvotes: 1

Kevin Lynch
Kevin Lynch

Reputation: 24721

You need to send headers with html declared: example

$to = '[email protected]';

$subject = 'Your subject';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = // your html code here


mail($to, $subject, $message, $headers);

Upvotes: 1

Related Questions