Page 1 of 1

sending email with html

Posted: Fri May 04, 2007 1:40 pm
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>";
}

Posted: Fri May 04, 2007 1:45 pm
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.

Posted: Fri May 04, 2007 1:48 pm
by mjmacarty
Thanks. It runs but I just get the HTML tags in the email. I will try your suggestion.

Posted: Fri May 04, 2007 1:56 pm
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.

Posted: Fri May 04, 2007 2:13 pm
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.

Posted: Fri May 04, 2007 2:32 pm
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.

Posted: Fri May 04, 2007 2:52 pm
by mjmacarty
Cool. I am figuing it all out. I thought it was a PHP library extension like gd...

Posted: Fri May 04, 2007 3:02 pm
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 :)

Posted: Fri May 04, 2007 3:04 pm
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.

Posted: Sat May 05, 2007 6:38 pm
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?

Posted: Sat May 05, 2007 7:02 pm
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.