Jump to content


Sending HTML with a PHP script

  • Please log in to reply
3 replies to this topic

#1 chasdrury

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 28 September 2006 - 10:52 AM

Hi guys,

I am a complete PHP newbie - I am helping a mate design a form on his website for people to sign up for a newsletter, and when they do they get emailed a HTML 50% off voucher.

I have the MySQL database, the form and the PHP working to submit the details to the SQL and that works perfectly.

There are only 3 fields on the form - Name, phone and email. What I am now trying to do is send an HTML email to the email address they specify - so effectively it sends the voucher automatically. When the script is run it forwards them to a thankyou html page.

I found the PHP for the HTML email on this site somewhere, and put it in my script. When I press submit on the form, it submits the data and displays the thanks page, and no email appears! The code is below - any ideas where I am going wrong?

I have only been using PHP for 3 days now, and its great! I hope to get into it more!


$from = "info@hairkandi.com";
$subject = "Hello! This is HTML email";

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$name','$phone','$email')";

    //begin message
    $message = <<<EOF
  <body bgcolor="#DCEEFC">
        <b>TEST EMAIL</b> <br>
        <font color="red">DOES THIS WORK?</font> <br>
        <a href="http://www.google.com/">* maaking.com</a>
      <br><br>TEST TEST <br> TEST<br>TEST
   //end of message
    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    $email = "$email, $from"; // this will send to both emails at the same time.
    // now lets send the email.
    mail($email, $subject, $message, $headers);
header( "Location: thanks.htm" );


#2 alpine

  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 28 September 2006 - 12:12 PM


#3 thedarkwinter

  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 28 September 2006 - 03:59 PM


it could be that mail is not set up correctly in the php.ini

I'm not 100% sure how to set it up, but the way i do it is to set the smtp server dynamically:

    // now lets send the email.
          ini_set("SMTP", "");  // or another ip address foran smtp server ifthe local machine is running an smtp server
    mail($email, $subject, $message, $headers);
        ini_restore("SMTP");              // revert back to php.ini settings

Hope that helps??
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#4 watts

  • Members
  • PipPip
  • Member
  • 23 posts

Posted 28 September 2006 - 08:06 PM

Sending HTML email can be tricky with the mail() class.  I would suggest using PHPMailer.  It's a class you can download that makes it much easier to send HTML emails.  Check out the site http://phpmailer.sourceforge.net

I've been trying to sort out email problems lately and this made all the difference.  There's also a good tutorial on the same site that walks you through how to do it.


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users