Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.

    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Hi,

    I have just created a form with the form action beeing "mailto:email" but the results show up in the email quite messy and as an attachment.
    Anyone know of any tutorials or would like to share som tips on how i can format the results so they will show up like a normal email ?
    Like:

    Name: Answer
    Age: Answer
    •  
      CommentAuthoreltiare
    • CommentTimeMay 18th 2008 edited
     permalink
    More than likely your server has PHP installed. Something like this might work for you:

    In a file called send_email.php:
    <?php
    $mail_this = '';
    foreach($_POST as $var => $val) {
    $mail_this .= "$var: $val\n"
    }
    mail('your_email@address.com', 'Subject Line', $mail_this);
    ?>

    In your HTML:
    <form action="send_email.php" method="POST">
    <label for="name">Name:</label> <input type="text" name="Name" id="name"/>
    <label for="age">Age:</label> <input type="text" name="Age" id="age"/>
    <button>Send!</button>
    </form>

    It is worth noting that anyone can build a simple program to post to your form multiple times and send noxious emails your way. A similar problem exists with mailto:email, but in this case spammers get your email directly and then can send you email whenever they want.
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008 edited
     permalink
    Here is my code now:

    <?php
    $comments = $_REQUEST['comments'];
    mail( "example@example.com", "Subject", $comments, "From: $email" );

    ?>

    This works and gives me the result of $comments in my email perfectly, but how do i format it with more variables ? Like not only comments but email, name, age and so on. I tried writing it like this:

    <?php
    $variable= $_REQUEST['variable'];
    $secondvariable= $_REQUEST['secondvariable'];
    $thirdvariable = $_REQUEST['thirdvariable'];
    mail( "example@example.com", "Subject", $variable,$secondvariable,$thirdvariable, "From: $email" );

    ?>

    But it resulted in error.
  1.  permalink
    You can't use a comma between the variables. Try $variable . $secondvariable . $thirdvariable instead.
    Thankful People: Avean
    •  
      CommentAuthoreltiare
    • CommentTimeMay 18th 2008
     permalink
    With that, you won't get any formatting. Try this:

    $email_this = "Comments: " . $_REQUEST['comments'] ."\n";
    $email_this .= "Second Variable " . $_REQUEST['secondvariable'] ."\n";
    // etc...
    mail( "example@example.com", "Subject", $email_this, "From: $email" );

    Please note the ".=" after the first assignment. This tells PHP to add the string on to the end of the variable instead of replacing it completely.
    Thankful People: Avean
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Thank you to both of you ! That did the trick :) Awesome. Really appreciated the help, good karma your way ! ;)
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008 edited
     permalink
    Need more help i think, after putting in more variables to send i dont recieve the email anymore. I dont get any syntax errors either so i dont know why. Here is the code:

    <?php
    $name = "Name:" . $_REQUEST['name'] ."\n";
    $nick = "Nickname:" . $_REQUEST['nick'] ."\n";
    $gender = "Gender:" . $_REQUEST['gender'] ."\n";
    $age = "Age:" . $_REQUEST['age'] ."\n";
    $email = "Email:" . $_REQUEST['email'] ."\n";
    $about = "About:" . $_REQUEST['about'] ."\n";
    $experience = "Experience:" . $_REQUEST['experience'] ."\n";
    $personality = "Personality:" . $_REQUEST['personality'] ."\n";

    mail("example@example.com", "Subject",
    $name . $nick . $gender . $age . $email . $about . $experience . $personality, "From: $email" );

    ?>

    Here is the HTML:

    <form action="application_handle.php" method="post">
    <fieldset><legend>Enter your information in the form below:</legend>

    <p><span class="formlabel">Your name:</span>
    <input type="text" name="name" size="20" maxlength="40" /></p>

    <p><span class="formlabel">Nickname:</span>
    <input type="text" name="nick" size="20" maxlength="40" id="nick" /></p>

    <p><b class="formlabel">Email Address:</b>
    <input type="text" name="email" size="40" maxlength="60" /></p>

    <p><b class="formlabel">Gender:</b>
    <input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" /> Female</p>

    <p><b class="formlabel">Age:</b>
    <input type="text" name="age" size="5" maxlength="2" id="age" /></p>

    <p><b class="formlabel">About:</b>
    <textarea name="about" rows="3" cols="40"></textarea></p>

    <p><b class="formlabel">experience:</b>
    <textarea name="experience" rows="3" cols="40"></textarea></p>

    <p><b class="formlabel">personality:</b>
    <textarea name="personality" rows="3" cols="40"></textarea></p>

    </fieldset>
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>
    </form>
  2.  permalink

    There doesn't seem to be any errors, or at least I didn't spot any.

    Try these lines at the beginning of the PHP file and see if you get any errors to show up:

    ini_set("display_errors", 1);

    ini_set('error_reporting', E_ALL);

    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    hehe no errors. But whats weird is that if i have 3 variables it will send the email, but as soon as i put in more i wont get the email. Like i have just crossed a limitation of some sort.
  3.  permalink
    That would have been my next guess as well.

    Try

    $message = $name . $nick . $gender . $age . $email . $about . $experience . $personality;
    mail("example@example.com", "Subject", $message, "From: $email" );


    and see if that'll work.
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Didnt work either, strange. This should work.
  4.  permalink
    Do you get the entire message body if you just put

    echo $message;

    instead of the mail function?
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Yeah that worked, i can echo all the variables like your example. So that means it must has something to do with the mail function, but what it is .....
    •  
      CommentAuthorkari.patila
    • CommentTimeMay 18th 2008 edited
     permalink
    There seems to be a 70 character limit for each line of the message body - do you have anything that goes beyond that?

    You can use

    $message = wordwrap($message, 70);

    to get past that limitation.
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Tried to use as little characters as possible, doesnt matter how many characters it is, as long as i enter the 4th Variable i dont get the email.
    Didnt work with the last idea either. Not sure if i did it correct but here is the code:

    <?php
    ini_set("display_errors", 1);

    ini_set('error_reporting', E_ALL);

    $name = "Name:" . $_REQUEST['name'] ."\n";
    $nick = "Nickname:" . $_REQUEST['nick'] ."\n";
    $gender = "Gender:" . $_REQUEST['gender'] ."\n";
    $age = "Age:" . $_REQUEST['age'] ."\n";
    $email = "Email:" . $_REQUEST['email'] ."\n";
    $about = "About:" . $_REQUEST['about'] ."\n";
    $experience = "Experience:" . $_REQUEST['experience'] ."\n";
    $personality = "Personality:" . $_REQUEST['personality'] ."\n";
    $message = $name . $nick . $gender . $age . $email . $about . $experience . $personality;
    $message = wordwrap($message, 70);
    mail( "example@example.com", "Subject",
    $message, "From: $email" );
    print "Congratulations your request has been sent";
    ?>
  5.  permalink
    The mail function is working, but nothing is being sent. This is probably one of the reasons I've always used something like PHPMailer to send my messages.

    I'll send you a working example if you haven't resolved this by tomorrow.
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008
     permalink
    Yeah PHPMailer worked immediatly, but its a bit complicated app if i want to customize it.
    •  
      CommentAuthorAvean
    • CommentTimeMay 18th 2008 edited
     permalink
    After 6 hours of straight scripting i finally did it. The problem was because of the "From: $email"); part.
    So i made the code like this:

    $headers = 'From: email@email.com' . "\r\n" .
    'Reply-To: email@email.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    mail( "example@example.com", "Subject", $message, $headers );

    And now it works.
    But do you know how i can format the output ?

    Like this line for example:

    $name = "Name:" . $_REQUEST['name'] ."\n";

    How do i enter <b> </b> in there ? So Name: gets bold.
  6.  permalink
    Almost everything you need to know about PHP can be found at php.net

    http://php.net/function.mail - example #4:

    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    •  
      CommentAuthorAvean
    • CommentTimeMay 19th 2008
     permalink
    Yeah i will :) Thanks for the help Kari.
    •  
      CommentAuthorAvean
    • CommentTimeMay 19th 2008 edited
     permalink
    .
  7.  permalink
    I actually had to use this script my self the very next day, so thanks to you as well.
Add your comments
    Username Password
  • Format comments as (Help)