sending email with html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

sending email with html

Post by mjmacarty »

I have created from that is supposed to allow you to enter and send a message, and that part works, but I want to be able to add some formatting, which is where I run into trouble. Can this not be done from a form?

Code: Select all

// get email from database
  $sql = "SELECT email FROM subscribers";
  $result = mysql_query($sql,$conn) or die(mysql_error());
  
  // create a from mail header
  $headers = "From: xxx <xxx@xxx.com>\n Content-Type: text/html\r\n";
  
  // loop through results and send mail
  while ($row = mysql_fetch_array($result)) {
    set_time_limit(0);
    $email = $row['email'];
   	mail($email, stripslashes($_POST[subject]), stripslashes($_POST[message]), 
	 $headers);
 
    echo "newsletter sent to: $email<br>";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It might be the way that you are not wrapping your array indexes in quotes which is causing PHP to not like your code. Otherwise, it should run as it is.
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Post by mjmacarty »

Thanks. It runs but I just get the HTML tags in the email. I will try your suggestion.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What kind of server are you on? It might have to do with the \r\n in the header. You might also want to look at Swiftmailer as a mailing tool. It is absolutely awesome and written by our very own d11wtq.
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Post by mjmacarty »

UNIX. I am looking at SwiftMailer. It's a library right? This could be problematic since it's not my server. Thanks for your help.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you have access to the server to put code on it you can certainly use Swift. Just download the app and plug in to it.
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Post by mjmacarty »

Cool. I am figuing it all out. I thought it was a PHP library extension like gd...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mjmacarty wrote:Cool. I am figuing it all out. I thought it was a PHP library extension like gd...
It's a library, but written with PHP code so all you have to do is place it on the server and inlcude the relevant files. The documentation (hopefully improved soon :() explains how to get up and running :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Follow the simplest of examples from one of the two sets of documentation and you will be well on your way to using it efficiently. If not, post back and I can reply with some sample, easy, swift tests.
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Post by mjmacarty »

Thanks guys. I am still playing with Swift, but I got some thing to work that was much more, shall we say, basic. But I don't know why this should work. HTML came through just fine when I concatenated the Content-Type... with the From email address.

Code: Select all

$headers = "From: xxx <xxx@xxx>\r\n" . "Content-Type: text/html";
What's the diff?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mjmacarty wrote:Thanks guys. I am still playing with Swift, but I got some thing to work that was much more, shall we say, basic. But I don't know why this should work. HTML came through just fine when I concatenated the Content-Type... with the From email address.

Code: Select all

$headers = "From: xxx <xxx@xxx>\r\n" . "Content-Type: text/html";
What's the diff?
HINT: \r\n vs. \n

This is just one major drawback with mail(). It's seemingly unpredictable unless you prance around and try to figure out what language it's talking.
Post Reply