embedding images and formatting html into a email with php script

  • # April 8, 2013 at 7:12 pm

    Im trying to setup a php email submit script that will allow me to embed images and format the email with html. There was an example on this forum but comments are no longer allowed. When I try to use the code from the example, the email shows the html tags. When I try to modify the posted example to work with my script the script seems to fail and does not send a email. I need some help finding out why the html formating is not working.


    Here is my script that is failing.

    < ?php
    if(isset($_POST)) {

    $email_to = “”;

    function died($error) {
    // your error code can go here
    echo “We are very sorry, but there were error(s) found with the form you submitted. “;
    echo “These errors appear below.

    echo $error.”

    echo “Please go back and fix these errors.


    // validation expected data exists
    if(!isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST) ||
    !isset($_POST) ||
    died(‘We are sorry, but there appears to be a problem with the form you submitted.’);

    $first_name = $_POST; // required
    $email_from = $_POST; // required
    $email_doctor = $_POST; // required
    $output_comments = $_POST;
    $comments = $_POST; // required

    $error_message = “”;
    $email_exp = ‘/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/’;
    if(!preg_match($email_exp,$email_from)) {
    $error_message .= ‘The Patient Email you entered does not appear to be valid.

    if(!preg_match($email_exp,$email_doctor)) {
    $error_message .= ‘The Doctor Email you entered does not appear to be valid.
    $string_exp = “/^[A-Za-z .’-]+$/”;
    if(!preg_match($string_exp,$first_name)) {
    $error_message .= ‘The First Name you entered does not appear to be valid.
    if(strlen($comments) < 2) {
    $error_message .= ‘The Comments you entered do not appear to be valid.
    if(strlen($error_message) > 0) {

    function clean_string($string) {
    $bad = array(“content-type”,”bcc:”,”to:”,”cc:”,”href”);
    return str_replace($bad,””,$string);
    // format the message
    $email_message = “Form details below.nn”;
    $email_message = ‘‘;
    $email_message .= ‘Website Change Request‘;
    $email_message .= “First Name: “.clean_string($first_name).”n”;
    $email_message .= “Patient Email: “.clean_string($email_from).”n”;
    $email_message .= “Doctor Email: “.clean_string($email_doctor).”n”;
    $email_message .= “OutPut Comments: “.clean_string($output_comments).”n”;
    $email_message .= “Comments: “.clean_string($comments).”n”;
    $email_message .= ““;

    // format the subject
    $email_subject = “referral form for ” .$first_name ;
    // create email headers
    $headers = ‘MIME-Version: 1.0’ . “rn”;
    $headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “rn”;
    $headers = ‘From: ‘.$email_doctor.”rn”.
    ‘Reply-To: ‘.$email_doctor.”rn” .
    ‘CC: ‘.$email_doctor. ‘,’ .$email_to.”rn” .
    ‘X-Mailer: PHP/’ . phpversion();
    @mail($email_from, $email_subject, $email_message, $headers);


    Thank you for contacting us. We will be in touch with you very soon.

    < ?php

    # April 9, 2013 at 11:07 am

    This reply has been reported for inappropriate content.

    Take a look at Chris’ post about constructing PHP emails here.

    From looking at your code block, the img tag is missing the opening less than and the call to the mail function has an @ in front, possibly a different syntax I’m unaware of.

    Also, try wrapping your $email_message in html > body tags.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.