0

I have analyzed a lot of similar questions that pertains to this situation and I am still unable to successfully complete this task. So I apologize for the redundancy.

I created a website form and I am trying to send the user's information to my GoDaddy Webmail account (and from there I send a copy to my gmail account). So, when I've had endless trials of sending a user's information to my email, it fails every time.

I've also tried running PHPMail class in some other file I created, and it tells me that it's having a hard time connecting to the SMTP server. I've made sure to that everything was correct and it was still not connecting. Please help!

 HTML FORM: 
<h3>Send us a Message</h3>
            <form name="sentMessage" id="contactForm" novalidate action="contact_me.php" method="post">
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Full Name:</label>
                        <input type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Phone Number:</label>
                        <input type="tel" class="form-control" id="phone" required data-validation-required-message="Please enter your phone number.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Email Address:</label>
                        <input type="email" class="form-control" id="email" required data-validation-required-message="Please enter your email address.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label>Message:</label>
                        <textarea rows="10" cols="100" class="form-control" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                    </div>
                </div>
                <div id="success"></div>
                <!-- For success/fail messages -->
                <button type="submit" class="btn btn-primary">Send Message</button>
            </form>
        </div>

 The contact.php form it calls:
      <?php
   // Check for empty fields
   if(empty($_POST['name'])      ||
     empty($_POST['email'])     ||
     empty($_POST['phone'])     ||
     empty($_POST['message'])   ||
     !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
     {
     echo "No arguments Provided!";
     return false;
     }

  $name = strip_tags(htmlspecialchars($_POST['name']));
  $email_address = strip_tags(htmlspecialchars($_POST['email']));
  $phone = strip_tags(htmlspecialchars($_POST['phone']));
  $message = strip_tags(htmlspecialchars($_POST['message']));

  // Create the email and send the message
  $to = 'cochranlawoffice@cochranlawoffice.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
  $email_subject = "Website Contact Form:  $name";
  $body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
  $headers .= "Reply-To: $email_address";   

  /*$smtp = Mail::factory('smtp', array(
          'host' => 'localhost',
          'port' => '25',
          'auth' => true,
          'username' => 'cochranlawoffice@cochranlawoffice.com', //your gmail account
          'password' => 'password' // your password
      )); 

  // Send the mail
  $mail = $smtp->send($to, $email_subject, $headers, $body); */

  mail($to,$email_subject,$body,$headers);
  return true;         
  ?>

Here is the class that I was using to run the PHPMail and getting the error of not being able to connect:

  <?php
    /*
     *
     * This example shows how to handle a simple contact form.
     */
    $msg = '';
    //Don't run this unless we're handling a form submission
    if (array_key_exists('email', $_POST)) {
        date_default_timezone_set('Etc/UTC');
        require './PHPMailer/PHPMailerAutoload.php';
        //Create a new PHPMailer instance

        $mail = new PHPMailer(); 

        $mail->IsSMTP(); 
        $mail->SMTPDebug = 1; 
        $mail->SMTPAuth = true; 
        $mail->SMTPSecure = 'tls'; 
        $mail->Host = "localhost";

        $mail->Port = 25; 
        $mail->IsHTML(true);

        $mail->Username = "cochranlawoffice@cochranlawoffice.com";
        $mail->Password = "password";
        //Use a fixed address in your own domain as the from address
        //**DO NOT** use the submitter's address here as it will be forgery
        //and will cause your messages to fail SPF checks
        $mail->setFrom('donotreply@email.com', 'name');
        //Send the message to yourself, or whoever should receive contact for submissions
        $mail->addAddress('cochranlawoffice@cochranlawoffice.com', 'John Doe');
        //Put the submitter's address in a reply-to header
        //This will fail if the address provided is invalid,
        //in which case we should ignore the whole request
        if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
            $mail->Subject = 'PHPMailer contact form';
            //Keep it simple - don't use HTML
            $mail->isHTML(false);
            //Build a simple message body
            $mail->Body = <<<EOT
    Email: {$_POST['email']}
    Name: {$_POST['name']}
    Message: {$_POST['message']}
    EOT;
            //Send the message, check for errors
            if (!$mail->send()) {
                //The reason for failing to send will be in $mail->ErrorInfo
                //but you shouldn't display errors to users - process the error, log it on your server.
                $msg = 'Sorry, something went wrong. Please try again later. Error: ' . $mail->ErrorInfo;

            } else {
                $msg = 'Message sent! Thanks for contacting us.';
            }
        } else {
            $msg = 'Invalid email address, message ignored.';
        }
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Contact form</title>
    </head>
    <body>
    <h1>Contact us</h1>
    <?php if (!empty($msg)) {
        echo "<h2>$msg</h2>";
    } ?>
    <form method="POST">
        <label for="name">Name: <input type="text" name="name" id="name"></label><br>
        <label for="email">Email address: <input type="email" name="email" id="email"></label><br>
        <label for="message">Message: <textarea name="message" id="message" rows="8" cols="20"></textarea></label><br>
        <input type="submit" value="Send">
    </form>
    </body>
  • What happens when you use this code? Do you get an error? Do you get an email? – John Conde Apr 30 '17 at 18:44
  • When I was running code similar to this, using PHPMail class, it was telling me it was unable to connect to the SMTP server. But I don't know if that's the exact case here. I commented on connecting exactly where the mail should be sent " /*$smtp = Mail::factory('smtp', array(" and I receive nothing – Airforceone Apr 30 '17 at 18:47
  • This code could be failing for a variety of reasons if you aren't getting a PHP error. If you switch back to PHPMailer, double check the credentials for your SMTP server is correct. – John Conde Apr 30 '17 at 18:49
  • I inserted the other code I used for PHPMail – Airforceone Apr 30 '17 at 18:51

0 Answers0