Problem with testing my email.php

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Re: Problem with testing my email.php

Postby Chico Felipe » 01. June 2013 22:03

Ok. I tried to follow your advices and integrate my form into php file which I now call newversion.php. I also did some validation and sanitation (not sure if any of these is correct). I requested it via http://localhost/email/newversion.php. However, it's still not working. This php is killing me already :(

Here is all in one file called newversion.php:

Code: Select all
<?php 
require_once('PHPMailer/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

// You should do some data validation / data sanotation here
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

$mail = new PHPMailer();

// base parameters. working for me
$mail->IsSMTP(); // use SMTP
$mail->Host = "smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true; // turn on SMTP authentication

// adjust these lines
$mail->Username = "mymail@gmail.com";
$mail->Password = "xxxxxxxx";
$mail->SetFrom($email, $name);
$mail->AddAddress('someoneelse@gmail.com', 'SomeoneElse');
$mail->Subject = "Feedback Form Results";
$mail->Body    = $message;

if (isset($_POST['Submit'])) { 
 
        if ($_POST['name'] != "") { 
            $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING); 
            if ($_POST['name'] == "") { 
                $errors .= 'Please enter a valid name.<br/><br/>'; 
            } 
        } else { 
            $errors .= 'Please enter your name.<br/>'; 
        } 
 
        if ($_POST['email'] != "") { 
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); 
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>"; 
            } 
        } else { 
            $errors .= 'Please enter your email address.<br/>'; 
        } 
 
        if ($_POST['message'] != "") { 
            $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING); 
            if ($_POST['message'] == "") { 
                $errors .= 'Please enter a message to send.<br/>'; 
            } 
        } else { 
            $errors .= 'Please enter a message to send.<br/>'; 
        } 

if (!$errors) { 
            $mail_to = 'mymail@gmail.com'; 
            $subject = 'New Mail from Form Submission'; 
            $message  = 'From: ' . $_POST['name'] . "\n"; 
            $message .= 'Email: ' . $_POST['email'] . "\n";   
            $message .= "Message:\n" . $_POST['message'] . "\n\n"; 
            mail($to, $subject, $message); 
 
            echo "Thank you for your email!<br/><br/>"; 
        } else { 
            echo '<div style="color: red">' . $errors . '<br/></div>'; 
        } 
    } 
?> 

<!DOCTYPE html>
<html>
<head>
   <title>Slide Down Contact Form</title>
   <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
   <link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
   <script type='text/javascript' src='dropbox.js'></script>
   <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<body>
   <div class="container">

      <div id="button" class="title">
         <h6>Contact</h6>
      </div>
      
      <div id="dropbox">
         <header class="title">
            <h6>Whats up?</h6>
         </header>
         
         <div class="contact-form">
            <form action="newversion.php" method="post">
                  <h6><img src="img/person.png" alt="" /> Name</h6>
               <input type="text" name="name" placeholder="Please enter your full name here" required />
                  <h6><img src="img/email.png" alt="" /> E-mail</h6>
               <input type="email" name="email" placeholder="Please enter your e-mail address" required/>
                  <h6><img src="img/message.png" alt="" /> Message</h6>
               <textarea name="message" placeholder="Type your message..." required/></textarea>
               
               <input type="submit" value="Submit">
            </form>
         </div>
      </div>
      
   </div>

<script src="dropbox.js"></script>

</body>
</html>


What a mess, huh? :shock: I prefered separated html and separated php because it looks more neat. Now this integrated version is confusing me.

I really thought these web forms are easier to make. Now I feel bad to bother you so much with this but also would feel bad to give up when we went this far.
Chico Felipe
 
Posts: 22
Joined: 31. May 2013 13:30
Operating System: Windows 7

Re: Problem with testing my email.php

Postby Altrea » 02. June 2013 02:05

Here is an working example of a self referencing form:
Code: Select all
<?php
require_once 'PHPMailer/class.phpmailer.php';

// form url sanitiziing
$php_self = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

// variable initializing
$name    = '';
$email   = '';
$message = '';
$errors  = array();

// is form send?
if( isset( $_POST['submit'] ) ) {

    // validate $_POST['name']
    $name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
    if( '' == $name ) {
        $errors[] = 'Please enter a valid name';
    }

    // validate $_POST['email']
    $email = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL );
    if( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
        $errors[] = 'Please enter a valid email';
    }

    // validate $_POST['message']
    $message = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
    if( '' == $message ) {
        $errors[] = 'Please enter a valid message';
    }

    // If no errors
    if( empty( $errors ) ) {
        //values are valid, lets send an email

        $mail = new PHPMailer();

        // base parameters. working for me
        $mail->IsSMTP(); // use SMTP
        $mail->Host       = "smtp.gmail.com"; // GMail
        $mail->Port       = 465;
        $mail->SMTPSecure = "ssl";
        $mail->SMTPAuth   = true; // turn on SMTP authentication

        // adjust these lines
        $mail->Username   = "YOUR-GMAIL-NAME@gmail.com";
        $mail->Password   = "YOUR-GMAIL-PASSWORD";
        $mail->SetFrom($email, $name);
        $mail->AddAddress($mail->Username);
        $mail->Subject    = "Feedback Form Results";
        $mail->Body       = $message;

        // sending
        if(!$mail->Send()) {
            // first error message is just for debugging. This don't generate messages a user should read
            // comment this and uncommend the second message for a more user friendly message
             $errors[] = "Mailer Error: " . $mail->ErrorInfo;
             //$errors[] = "email couldn't be send";

             // Output Sanitizing for repopulating form
            $name    = filter_var( $name,    FILTER_SANITIZE_FULL_SPECIAL_CHARS );
            $email   = filter_var( $email,   FILTER_SANITIZE_FULL_SPECIAL_CHARS );
            $message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
        } else {
            // maybe you should generate/fill a success message here

            // clear fields
            $name    = '';
            $email   = '';
            $message = '';
        }

    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>self referencing form</title>
    <link rel='stylesheet' href='http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css'/>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div id="button" class="title">
        <h6>Contact</h6>
    </div>

    <div id="dropbox">
        <header class="title">
            <h6>Whats up?</h6>
        </header>
    <?php if(!empty($errors)): ?>
        <ul class="error">
            <li><?php echo join('</li><li>', $errors); ?></li>
        </ul>
    <?php endif; ?>
        <div class="contact-form">
            <form action="<?php echo $php_self; ?>" method="post">
                <!-- input element for the name -->
                <h6><img src="img/person.png" alt=""> Name</h6>
                <input type="text"
                       name="name"
                       value="<?php echo $name; ?>"
                       placeholder="Please enter your full name here"
                       required>

                <!-- input element for the email -->
                <h6><img src="img/email.png" alt=""> E-mail</h6>
                <input type="email"
                       name="email"
                       value="<?php echo $email; ?>"
                       placeholder="Please enter your e-mail address"
                       required>

                <!-- input element for the message -->
                <h6><img src="img/message.png" alt=""> Message</h6>
                <textarea  name="message" placeholder="Type your message..." required><?php echo $message; ?></textarea>

                <!-- input element for the submit button -->
                <input name="submit" type="submit" value="Submit">
            </form>
        </div>
    </div>
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script src='dropbox.js'></script>
</body>
</html>

I tried to put some comments to make it a little bit easier for you to understand what the code does.
Feel free to ask if anything isn't really clear for you.

Chico Felipe wrote:What a mess, huh? :shock: I prefered separated html and separated php because it looks more neat. Now this integrated version is confusing me.

Well, separating php from html code is a best practice. If you feel more comfortable separating those, feel free to do so.
I prefer to use the MVC pattern respectively a framework that uses such type of pattern to separate.
But that would be too complicated for a forum post :D

The fast and easy way would be to put the part between <?php and ?> into a separate file and simply require_once it like the phpmailer class.

Chico Felipe wrote:I really thought these web forms are easier to make.

Well... that depends. If security, databases and email sending is not important for you, forms can be very easy.
But creating secure and user friendly forms is one of the most challenging tasks a webdesigner can do.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Problem with testing my email.php

Postby Chico Felipe » 02. June 2013 13:52

Hi Altrea!

Thank you for fixing the php code for me. Adding the comments is very helpful! Appreciate it. Unfortunately, it's still not working.

I created a completely new php file and named it newnew.php. Then I copied the fixed code from above and I added my email and password. Later I requested http://localhost/email/newnew.php and I filled the form. When I clicked on Submit button I got two messages:
1. First message is in my browsers window and it says:
SMTP Error: Could not connect to SMTP host.
2. Second message showed up inside my webform (right above "name" field) and it says:
MAILER ERROR: SMTP ERROR: COULD NOT CONNECT TO SMTP HOST.

I believe that the base parameters that are working for you are not working for me. I tried to change the port from 465 to 567 and 25 but the message was the same. Not sure what else to do?

Altrea says: But creating secure and user friendly forms is one of the most challenging tasks a webdesigner can do.


Well, this makes it a bit easier for me. I thought I am a basket case :oops: But now when you mention this I believe there is a hope for me :) After all, I started with web design only 2 and 1/2 months ago so obviously this is too tricky for me. Anyway, I see the improvements on a daily basis so it keeps me glued to my computer!
Chico Felipe
 
Posts: 22
Joined: 31. May 2013 13:30
Operating System: Windows 7

Re: Problem with testing my email.php

Postby Altrea » 02. June 2013 14:29

Chico Felipe wrote:I believe that the base parameters that are working for you are not working for me. I tried to change the port from 465 to 567 and 25 but the message was the same. Not sure what else to do?

Try port 587 not 567 or 25. Gmail use either port 465 or 587.
If this will not work, i can't really help you out there. :shock:

But you will get into another problem i didn't realized before. You cannot send Mails in the name of someone else. That's a spam protection feature most of the mail address hoster use (you would get the exect same results with fake sendmail mail() too.
The other way round should work (send someone else a mail from your account).

Chico Felipe wrote:After all, I started with web design only 2 and 1/2 months ago so obviously this is too tricky for me. Anyway, I see the improvements on a daily basis so it keeps me glued to my computer!

Simply do one step after another :D
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Problem with testing my email.php

Postby Chico Felipe » 02. June 2013 19:35

I just tried port 587 but with no success. I keep receiving the same message as in my last post above. :cry:

Altrea says: But you will get into another problem i didn't realized before. You cannot send Mails in the name of someone else. That's a spam protection feature most of the mail address hoster use (you would get the exect same results with fake sendmail mail() too.
The other way round should work (send someone else a mail from your account).


I actually have 3 different emails (all of them on gmail) that I use for my testings. So, I was not sending mails in someone else's name.

However, I'll keep trying and reading more until I find the solution.

I wanted to thank you again for your patience. You are very knowledgeable and I believe that people who work with you are very lucky to have you there. I apologize once again if I bothered you too much with this. All I wanted was to install Xampp and test (what I thought is) a simple web form but at the end it came out quite complicated. Once when I figure out the solution I will come back and post it here for future web design newbies.
Chico Felipe
 
Posts: 22
Joined: 31. May 2013 13:30
Operating System: Windows 7

Re: Problem with testing my email.php

Postby Chico Felipe » 04. June 2013 17:34

I finally made it!!! Woohoo :)

This was the correct solution (everything else is the same as above):

$mail->Port = 587;
$mail->SMTPSecure = "tls"; // It was "ssl" before

Does anyone knows what "tls" and "ssl" stand for??? Just curious.

Alright! I can move forward now. There is still a lot to learn. Thanks again Altrea!
Chico Felipe
 
Posts: 22
Joined: 31. May 2013 13:30
Operating System: Windows 7

Re: Problem with testing my email.php

Postby Altrea » 04. June 2013 20:41

I'm glad you got it working.

Transport Layer Security (TLS) is an enhancement of SSL since ssl version 3.
I have tried tls in my Gmail configuration too but this doesn't work for me :D
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Problem with testing my email.php

Postby Chico Felipe » 05. June 2013 20:12

Alright! I added a comment into my php code and right next to the "tls" which says that the other option is "ssl". Just for the case :)
Chico Felipe
 
Posts: 22
Joined: 31. May 2013 13:30
Operating System: Windows 7

Re: Problem with testing my email.php

Postby tango12 » 19. September 2013 10:05

I have encounter pretty much the problem as this... i just followed the advice and its solved!! Thank You so much!! :)
tango12
 
Posts: 1
Joined: 19. September 2013 08:51
Operating System: windows 7

Previous

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 104 guests