Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP form still not working need help. SMTP Error

In workspaces my form works perfectly and sends an email to my yahoo email. When I upload the files to my real godaddy site the form is producing the following error and not sending any email can somebody help me fix this?

2017-09-26 17:51:17 SMTP ERROR: Failed to connect to server: Connection refused (111)

Below is my code:

<?php


if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

    if($name == "" || $email == "" || $details == ""){
    echo "You must fill out all the fields in the form please go back and try again.";
    exit;
    }


    require("PHPMailer.php");
    require("SMTP.php");

    $mail = new \PHPMailer\PHPMailer\PHPMailer;




    $mail->SMTPDebug = 2; // Enable verbose debug output
    $mail->isSMTP();     // Set mailer to use SMTP
    $mail->Host = 'smtp.mail.yahoo.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;     // Enable SMTP authentication
    $mail->Username = 'platinumhavocproductions@yahoo.com';     // SMTP username
    $mail->Password = '';     // SMTP password
    $mail->SMTPSecure = 'ssl';     // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;

    if (!isset($error_message) && !$mail->ValidateAddress($email)) {
    $error_message = "Invalid Email Address";
    }

    if (!isset($error_message)) {
    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .= "Email " . $email . "\n";
    $email_body .= "Details " . $details . "\n";

    $mail->setFrom('platinumhavocproductions@yahoo.com', $name);
    $mail->addAddress('platinumhavocproductions@yahoo.com', 'PlatHavPro');     // Add a recipient

    $mail->isHTML(false);     // Set email format to HTML

    $mail->Subject = 'Price quote from ' . $name;
    $mail->Body    = $email_body;
    }
  if(!$mail->ValidateAddress($email)) {
      echo "Invalid Email";
      exit;
  }




$email_body = "";
$email_body .= "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .= "Details: " . $details . "\n";





 if(!$mail->send()) {
     echo 'Message could not be sent.';
     echo 'Mailer Error: ' . $mail->ErrorInfo;
     exit;
 }

   header("location:pricequote.php?status=thanks");
}

include("header.php");

  if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
      echo "<p1> Your Free Price Quote will be emailed to you within 24 hours. Thanks for inquiring with Sitespressions.</p1>";
  } else { ?>











      <form action="pricequote.php" method="post">

      <legend class="formtitle">Please fill out this form to see if you qualify to receive a free web site.</legend>
      <fieldset>

      <label for="name">Your name?</label>
      <input type="text" id="name" name="name">

      <label for="email">Your email?</label>
      <input type="email" id="email" name="email">

      <label for="details">Describe the website you want built and please include who will be hosting it:</label>
      <textarea id="details" name="details"></textarea>

      <button type="submit">Get a free quote.</button>
      </fieldset>








      </form>




  <?php }  ?>
Alena Holligan
Alena Holligan
Treehouse Teacher

Yahoo and Gmail will both require that an App Password be setup. The wont just let an "insecure app" connect.

If you want to connect to yahoo smtp, you're required to either "turn on two-step verification"or "enable yahoo account key". See more here https://help.yahoo.com/kb/create-third-party-password-sln15241.html

15 Answers

I did that already Alena. It looks like it might have been a problem with my hosting. The hosting wasn't setup to receive a remote mail exchanger. I just fixed that, but have to wait a few hours for it to take effect hopefully that will resolve the issue for me and help anybody else with this problem. Grrr if the code is working on local host make sure your real host is set up for a remote mailing.

Haydn Morris
Haydn Morris
9,187 Points

Is this part of one of the classes? If so which one? I'm trying to do something similar myself and I think I'd really benefit from watching it!

Sorry Haydn looks like I lost the link to the video, but it's somewhere under the php courses talking about using php mailers and using smtp from GitHub. You might be able to find it in the library.

Grrrr my form is still not working I'm getting the same error. I will have to wait till tomorrow to contact my host this is so frustrating.... :(

I found the error log of my host and now I know what the problem is:

Class 'PHPMailer\PHPMailer\Exception' not found in /home/platinumhavoc/public_html/Sitespressions.com/PHPMailer.php on line 3419

How would I go about fixing this error?

For a reference in the php mailer file here is line 3419:

if (!$this->smtpConnect($this->SMTPOptions)) {

        throw new Exception($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);

    }
Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Looks to be looking for the class 'PHPMailer\PHPMailer\Exception'. Because it is trying to throw and exception. You don't need the file if it works. You said that it worked for you on your local server, so I am guessing it is some issue with external smtp on your host.

You could try to install all of PHPMailer and use their autoloader. This would hopefully give you more errors to work with. You could use composer or just download the PHPMailer files and add them to your site.

Alena Holligan
Alena Holligan
Treehouse Teacher

Or you can just grab the Exception file and require("Exception.php");

When I add and require the Exception.php file and try to submit the form it gives these errors:

2017-10-02 21:24:26 SMTP ERROR: Failed to connect to server: Connection refused (111) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

This is the right file right: https://github.com/PHPMailer/PHPMailer/blob/master/src/Exception.php

Here is the error from my error log in the c panel console:

[29-Sep-2017 09:22:17 America/Phoenix] PHP Notice: Undefined index: OS in /home/platinumhavoc/public_html/mail_test.php on line 3525

Alena Holligan
Alena Holligan
Treehouse Teacher

Yes, this is saying that the connection is refused. It is unable to connect to SMTP. You said it was working on Dev? Did you check with your hosting company? They may need to allow access.

Here is the code that I used it worked fine on workspace, but not on real host godaddy...

require 'Exception.php'; require 'PHPMailer.php'; require 'SMTP.php';

2017-10-03 18:29:17 SMTP ERROR: Failed to connect to server: Connection refused (111) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

and in my error log:

Class 'PHPMailer\PHPMailer\Exception' not found in /home/platinumhavoc/public_html/Sitespressions.com/PHPMailer.php on line 3419

Line3419 is: if (!$this->smtpConnect($this->SMTPOptions)) {

        throw new Exception($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);

    }

Why is it saying that it can't find Exception when I already added the file and required it?

"Yes, this is saying that the connection is refused. It is unable to connect to SMTP. You said it was working on Dev? Did you check with your hosting company? They may need to allow access."

Yes I talked to them and they tested the smpt of my site they say its working fine. They sent me an email using smtp with there default form they have. If I contact them again what exactly should I ask them about this error?

Alena Holligan
Alena Holligan
Treehouse Teacher

You could try changing your port to 587

Or ask that they verify

telnet smtp.mail.yahoo.com 465

WOW!! The person I just talked to on the phone from godaddy hosting tech support just told me completely different then the other people I talked to there yesterday. He said that I can't use smtp and that I must use standard form mail. Is there a video to set up the mail not using smtp?

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Basically you just remove everything about smtp, all the lines between $mail->SMTPDebug and $mail->port

You can check out https://teamtreehouse.com/library/build-a-basic-php-website/adding-a-basic-form/using-a-thirdparty-library-2

Haydn that link that Alena just posted is what your looking for.

When I take out the smtp and only require phpmailer I get no errors in workspace or on the real site. It also directs to the thankyou message and everything looks good. The only problem is that I didn't receive any emails? I waited about 30 minutes. Do you know why I'm not receiving the emails Alena?

Alena Holligan
Alena Holligan
Treehouse Teacher

if you do NOT use SMTP in workspaces, you cannot send out mail, you can only send internally to the special treehouse@localhost email (which doesn't validate as a real email by the way). Check out this video https://teamtreehouse.com/library/build-a-basic-php-website/adding-a-basic-form/using-the-console-to-check-email

Haydn Morris
Haydn Morris
9,187 Points

Thanks bud, I wrestled with it for so long. It turns out my problem was also coming from the setup of my web hosting so anyone having this problem that'd be one of the first things I did, check the conditions of the mail server of your hosting company. I was getting some really weird errors so I got in contact with the hosting company and we worked through it together. Can honestly say though in all the problems I've faced programming this was the most frustrating to fix haha.

Alena I don't care about workspace why isn't it sending on the real site? I took off the smpt and there are no more errors it redirects to the thankyou status page, but I'm not receiving the email to the yahoo email?

Should I try to make a form using this and not php mailer?

https://ph.godaddy.com/help/using-our-php-form-mailers-on-web-and-classic-hosting-8376

I've got it working. Without the smtp yahoo was bouncing the email back. So I created a new email on the local host to receive the email from the form. It's sort of a work around and not a real fix, but at least I will stop bothering you Alena haha thanks for your help.