tony noriega
tony noriega

Reputation: 7673

Sending HTML email, via PHP form

I am trying to send my website visitors and email with some directions and tips before they show up to my studio via PHP form mailer. ( i am simplifying some of the form fields )

However the HTML formatting is not working correctly.... did i not declare the mime type of charset correctly?

<?php 

            if (isset($_POST['submit'])) { 

            //if (empty ($_POST['name']) || empty($_POST['email'])) 
            //{
            //echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>";
            //}

            $name = $_POST['name'];
            $email = $_POST['email'];
            $email2 = $_POST['email2'];
            //A bunch of other fields are here

            //Additional Headers

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


            //$yoursite = "My Site";
            //$youremail = $email;

            $subject = "Website Form";
            $message = "
            $name would like you to contact them about your service.
            Contact PH:  $phone
            Email:  $email
            Legal Guardian: $legal              
            //more stuff here
            ";


            $subject2 = "Directions and Information";

                            $message2 = "<html><head></head><body>
            $message2 .= "<h1>Directions</h1>

            <p>text</p>

            <p><a href='http://example.com/schedules'>Click here</a>

            <h2>How Do I find your Photo Studio?</h2>

            <h2>What do I have to bring with me?</h2>
            </p>";

            $message2 .= "</body></html>";



            $email3 = "[email protected]";
            $email4 = "[email protected]";




            //This email sends their details to me from the visitor
            mail($email3, $subject, $message, "From: $email");


                            //This email sends directions to the visitor from me
            mail($email, $subject2, $message2, "From: $email4");

            echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";


            }
            ?>

Upvotes: 2

Views: 1042

Answers (2)

Michael Irigoyen
Michael Irigoyen

Reputation: 22957

I swear by the PEAR Mail_Mime package. It's simple and powerful.

PEAR: Mail_Mime

//Basic mail headers
$headers['To'] = "[email protected]";
$headers['From'] = "[email protected]";
$headers['Subject'] = "Test";

//Set up the mail module
$mime = new Mail_mime("\r\n");
$mime->setTXTBody("This is a test");
$mime->setHTMLBody("<p>This is a test.</p>");
$body = $mime->get();
$headers = $mime->headers($headers);

//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($headers['To'], $headers, $body);

Upvotes: 0

Hoatzin
Hoatzin

Reputation: 1454

There is a lot of random junk that needs to be done to an email to send correctly. I generally tend to outsource all that responsibility to a pre-packaged class that exists in the wild, something like http://swiftmailer.org/

Maybe someone else would have a better class to offer.

Upvotes: 2

Related Questions