SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formatting email via php

    I'm putting together a contact form that's php-driven. Right now, the info is lumped into one line in the email the form sends. How can I format it where each item is on a separate line?

    Here's the code for the mail form:
    <?php
    function clean_input_for_email($value) {
    $patterns[0] = '/content-type:/i';
    $patterns[1] = '/to:/i';
    $patterns[2] = '/cc:/i';
    $patterns[3] = '/bcc:/i';
    $patterns[4] = '/\r/i';
    $patterns[5] = '/\n/i';
    $patterns[6] = '/&#37;0a/i';
    $patterns[7] = '/%0d/i';
    return htmlspecialchars(preg_replace($patterns, "", $value));
    }
    extract( $_POST );
    //send email enquiry
    $sendto = 'email@tattletalealarm.com' ;
    $subject = 'tattletale Digital Upgrade Form' ;
    $message = 'Name: ' . clean_input_for_email($name) . "\n";
    $message .= 'Email: ' . clean_input_for_email($email) . "\n";
    $message .= 'Phone: ' . clean_input_for_email($phone) . "\n";
    $message .= 'Upgrade Choice: ' . clean_input_for_email($option) . "\n";
    $headers = "";
    $headers = 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1 ' . "\n";
    $headers .= 'X-Priority: 3' . "\n";
    $headers .= 'X-MSMail-Priority: Normal' . "\n";
    $headers .= 'X-Mailer: php' . "\n";
    $headers .= 'From: mailform@creativeservicesltd.com' . "\n";

    mail($sendto, $subject, $message, $headers);
    @header('Location: emailconfirm.php');
    ?>

    Here's the URL for the form: http://www.tattletalealarm.com/digit...sion/index.php

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another thing. How do I code a conditional statement that displays a certain page if all the info isn't filled out correctly? Right now, the site displays the emailconfirm.php page regardless if the email sends or not (it doesn't send if the form isn't completed).

  3. #3
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To answer your questions in order. You already appear to be formatting your e-mails, however, PHP has an in-built constant that will vary depending on the system it is being used on. This is due to the fact that Unix and Windows handle \r and \n differently. Thus your line may look like the following:

    PHP Code:
    $message .= 'This is a line' PHP_EOL;
    $message .= 'This is on a new line' PHP_EOL
    Many e-mail clients do not accept HTML due to their insecure nature. Thus \r and \r (Or in our case, PHP_EOL) should be used instead to format the e-mail. The e-mail will then be sent as the MIME type text/plain.

    To answer your last question, I don't quite understand because unless there the first 3 mandatory arguments are not set then it will send it anyway. Even the 1st argument may be null because if you are sending CC and/or BCC e-mails, these will be contained within the headers.

    However, if you're expecting the mail() function to fail if there is no 2nd or 3rd argument then you would perform the conditional statement like so:

    PHP Code:
    if(!@mail($sendto$subject$message$headers))
    {
        
    // Failed
        
    return;
    }

    // Success 
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, let me explain that I know next to nothing about PHP. My background is design. I replaced the "\n" with PHP_EOL and I'm getting the same result: all four required items are in one line instead of each being on their own line. How can I code the script to put each item on one line?

    Also, I don't know if I'm following your response to the second question properly, but I added your code above to the end of mine, but it still displays the confirm message (emailconfirm.php) if the form isn't complete. The following code is in the actual form itself, if that helps:

    <input type="hidden" name="required" value="name:Name,email:E-Mail,phone:Phone,option:Option"/>

    What am I doing wrong?

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you have exactly the same problem as this guy: http://www.sitepoint.com/forums/show...ight=mime+html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •