Jump to content

Recommended Posts

How can i make it where my script below can send html emails?

Right now it works, it sends emails, but how can i make it so it can send html emails

 

<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        $nrows = mysql_num_rows($result);
        $count = 0;

        for($i=0; $i<$nrows; $i++) {
        $row = mysql_fetch_assoc($result);
        $count++;
        mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@test.com") or die("Could not send email");
        echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />";
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>
<a href="addemail.php">Add an Email to the List</a>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/
Share on other sites

You need to specify html headers:

 

// 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";

 

http://www.php.net/function.mail#id3102134

 

You could also use phpMailer, or a similar class.

 

http://phpmailer.sourceforge.net/

i tried that and all the html code displays in the email, and now the email is sent to junk?

How can i fix this

Here is my code:


<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        $nrows = mysql_num_rows($result);
        $count = 0;

        for($i=0; $i<$nrows; $i++) {
        $row = mysql_fetch_assoc($result);
        $count++;
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: contact@test.com';
        mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], $headers) or die("Could not send email");
        echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />";
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>
<a href="addemail.php">Add an Email to the List</a>
</body>
</html>

i made the changes, but in my emails it still shows the html code for instance

Content-type: text/html; charset=iso-8859-1

From: contact@test.com

 

 

<html><body><h1>frank</h1></body></html>

 

 

here is the code below, how would i fix that

<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        $nrows = mysql_num_rows($result);
        $count = 0;

        for($i=0; $i<$nrows; $i++) {
        $row = mysql_fetch_assoc($result);
        $count++;
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: contact@test.com' . "\r\n";
        mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], $headers) or die("Could not send email");
        echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />";
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>
<a href="addemail.php">Add an Email to the List</a>
</body>
</html>

Is there a reason why my script wont work, it doesnt send html, just text,

I try putting html into it

for instance

<html><body><h1>frank</h1></body></html>

and that is exactly what appears in the email???

its probably something simple  :-\

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.