# PHP God Needed: HTML formatted email from PHP mailer

**URL:** https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105
**Category:** programming
**Created:** [February 5, 2004, 8:17am UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105 "2004-02-05T08:17:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Illuminae](https://avatars.discourse-cdn.com/v4/letter/i/b5ac83/32.png) [@Illuminae](https://forum.kirupa.com/u/Illuminae)
#### Post date: [February 5, 2004, 8:17am UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/1 "2004-02-05T08:17:23Z")

</div>

Hey guys. I thought this was hard because I’m an uber-beginner to PHP/MySQL (3rd day!!). Now, after a good 6 hours of searching the web, asking all my script buddies, and good ol’ trial and error, I have come to the conclusions that this is, in fact, a difficult thing to do.

I am trying to send out a nice HTML formatted email from a PHP script that sends to all emails in a database. The HTML mail also grabs data from a db as well. I have tried with and without the added php code, many different ways of settign up the file, and even without the html. No matter what i do i get an error. if anyone can help me build a working file that sends html emails i will be eternally grateful. I stripped the script down to the bare minimum, which is what youll see here. i need to have that loop there to get all the emails in the db but it seems to be clashing with the headers. (the script works fine without the headers added).

```php

<?
	mysql_connect ("localhost", "user", "pass");
	mysql_select_db ("database");

	$sql = "SELECT * FROM redmail";
	$result = mysql_query($sql);
	$subject = $_POST['subject'];
	$message = $_POST['message'];
$headers = "MIME-Version: 1.0
";
$headers .= "Content-type: text/html; charset=iso-8859-1
";
	While ($row = mysql_fetch_array($result)) 
{
	$email = $row['email'];
	$success = mail("$email","$subject","$message","$headers","From: [email]emailaddy@redgolgi.com[/email]");
	if ($success) 
		echo "The newsletter was successfully sent<br />"; 
	else 
		echo "An error occurred when sending the newsletter<br />";

}

mysql_close();
?>

```

thanks in advance!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 8:45am UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/2 "2004-02-05T08:45:54Z")

</div>

You’d have to post the errors you get, also, how many emails are you trying to send, could be that the scripts max\_execution time is over before it’s done…?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 8:56am UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/3 "2004-02-05T08:56:18Z")

</div>

sorry, i just edited the above code, i posted the wrong one…

i added in the

```php
    if ($success) 
        echo "The newsletter was successfully sent<br />"; 
    else 
        echo "An error occurred when sending the newsletter<br />";

```

part before.  
The error I get is simply my homemade error message, “An error occurred when sending the newsletter”. And at this point I’m only testing with two emails.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 8:56am UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/4 "2004-02-05T08:56:56Z")

</div>

is there a better way to test whats going wrong?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 1:08pm UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/5 "2004-02-05T13:08:40Z")

</div>

Just one thing. These are variables inside the mail() function. No need to have " " around them…

```php
mail($email, ,$subject,$message,$headers,"From: [email]emailaddy@redgolgi.com[/email]");

```

That might fix it…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 1:24pm UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/6 "2004-02-05T13:24:07Z")

</div>

thanks man! i fixed it. i wound up changing the whole thing around and this one works now.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 2:26pm UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/7 "2004-02-05T14:26:52Z")

</div>

Bit late if you sussed it but this may be of some use toyou

[http://phpmailer.sourceforge.net/](http://phpmailer.sourceforge.net/)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 5, 2004, 2:33pm UTC](https://forum.kirupa.com/t/php-god-needed-html-formatted-email-from-php-mailer/42105/8 "2004-02-05T14:33:18Z")

</div>

hey thats really neat. thanks! ill keep it in mind if i ever need it. 🙂
