Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Why does the email script not work?

I'm triggering my mail functionality using the attached "contact_me.js." After running the validation script, it uses the attached "contact_me.php" code and that's supposed to send me an email.


No errors, but no email and I'm not even sure how to go about troubleshooting it.


If you could look at the attached files and see if you can't sniff out whatever the trouble might be, that would be awesome!


The page in question is http://brucegust.com/cart/contact.phpcontact_me.jscontact_me.php

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

This one does not give 404

http://brucegust.com/cart/assets/mail/contact_me.php

Perhaps that is the one you want?
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@David: #1 is to see if the URL of the email script works at all. OP already has working email forms on the site

So right-click and inspect - look in the network tab

User generated image
Avatar of Bruce Gust

ASKER

Guys, I want to make sure I'm tracking with you...

Michel, you said you were getting a 404. I did as well earlier this morning prior to adjusting the "contact_me.js" to ensure that I was, in fact, triggering the "contact_me.php" page. But I did correct that, so that piece should be good to go.

If you're going out to http://brucegust.com/cart/contact.php and you fill out the form and inspect the process, on the Network tab you see this:

User generated image
...and that page is being "found." So, I'm thinking we're set there.

On the other hand, the code to that page is pretty straight forward, which is why I'm scratching my head...

<?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)) {
  http_response_code(500);
  exit();
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = 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 = "bruce@brucegust.com"; // Add your email address in between the "" replacing yourname@yourdomain.com - This is where the form will send a message to.
$subject = "brucegust.com 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\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: bruce@brucegust.com.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$header .= "Reply-To: $email";   

if(!mail($to, $subject, $body, $header))
  http_response_code(500);
?>

Open in new window

If I run that code all by itself like this by going out to http://brucegust.com/cart/assets/mail/contact_me.php and run it all by itself having made these changes...

<?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)) {
  http_response_code(500);
  exit();
}
*/

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

$name="Bruce Gust";
$email="bruce@brucegust.com";
$phone="800.321.2154";
$message="test";

// Create the email and send the message
$to = "bruce@brucegust.com"; // Add your email address in between the "" replacing yourname@yourdomain.com - This is where the form will send a message to.
$subject = "brucegust.com 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\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: bruce@brucegust.com.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$header .= "Reply-To: $email";   

if(!mail($to, $subject, $body, $header))
  http_response_code(500);
?>


Open in new window

I get a blank screen.

Why?

What do you think?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gentlemen!

First off, as always, thanks for your time and making the effort to explain what's going on under the hood.

In light of the counsel documented above, rather than try to make a dead horse gallop, I'm going with the PHPMailer functionality, since that seems to be the breakfast of champions.

That said, here's what I've got:

I used composer to install PHPMailer on my local host then uploaded the vendor directory to my server which is sitting on a Godaddy box.

Here's my code that I'm using just to get the engine to turn over:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "bruce@brucegust.com";
$mail->FromName = "Bruce Gust";

//To address and name
$mail->addAddress("bruce@brucegust.com", "Bruce Gust");
// $mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("bruce@brucegust.com", "Reply");

//CC and BCC
//$mail->addCC("cc@example.com");
//$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "test";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

Open in new window

You can see this at work by going out to http://brucegust.com/cart/email_test.php

I get a message that says the email got sent and there is a provision for an error, however...

No email.

I did find this info about setting up an email form on GoDaddy, not sure what, if anything, I need to change: https://www.godaddy.com/help/send-form-mail-using-an-smtp-relay-server-953

What do you think?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
David, I'm not sure "mailGun" is going to work, but you tell me. I'm basing my hesitation on this:

Chris, here's where I'm at:

I've got  a big chunk commented out, but I left it in place just so you can see how I attempted to implement your suggestion.

I'm using "Deluxe Linux Hosting with cPanel" which specifies that you use localhost unless you're using "mail..."





User generated image
When I use these settings:

$mail->IsSMTP(); // tell PHPMailer to use SMTP
$mail->SMTPDebug = 2; // turn on debugging
$mail->SMTPAuth = false;
$mail->SMTPSecure = '';
//$mail->Host = ""; //I googled this to find out if there was something unique about GoDaddy's config and they suggested this
$mail->Port = 25;

I get this message:

2022-03-17 21:53:40 SERVER -> CLIENT: 220-p3plcpnl1035.prod.phx3.secureserver.net ESMTP Exim 4.94.2 #2 Thu, 17 Mar 2022 14:53:40 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2022-03-17 21:53:40 CLIENT -> SERVER: EHLO brucegust.com
2022-03-17 21:53:40 SERVER -> CLIENT: 250-p3plcpnl1035.prod.phx3.secureserver.net Hello brucegust.com [127.0.0.1]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250-CHUNKING250-STARTTLS250-SMTPUTF8250 HELP
2022-03-17 21:53:40 CLIENT -> SERVER: STARTTLS
2022-03-17 21:53:40 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
2022-03-17 21:53:40 CLIENT -> SERVER: QUIT
2022-03-17 21:53:40
2022-03-17 21:53:40
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
2022-03-17 21:53:40 SERVER -> CLIENT: 220-p3plcpnl1035.prod.phx3.secureserver.net ESMTP Exim 4.94.2 #2 Thu, 17 Mar 2022 14:53:40 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2022-03-17 21:53:40 CLIENT -> SERVER: EHLO brucegust.com
2022-03-17 21:53:40 SERVER -> CLIENT: 250-p3plcpnl1035.prod.phx3.secureserver.net Hello brucegust.com [127.0.0.1]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250-CHUNKING250-STARTTLS250-SMTPUTF8250 HELP
2022-03-17 21:53:40 CLIENT -> SERVER: STARTTLS
2022-03-17 21:53:40 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
2022-03-17 21:53:40 CLIENT -> SERVER: QUIT
2022-03-17 21:53:40
2022-03-17 21:53:40
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'

Open in new window

When I try this configuration based on what I found here: https://stackoverflow.com/questions/38254105/phpmailer-smtp-connection-failed-godaddy ...
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

/*
//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

$mail->IsSMTP(); // tell PHPMailer to use SMTP
$mail->SMTPDebug = 2; // turn on debugging
$mail->SMTPAuth = false;
$mail->SMTPSecure = '';
//$mail->Host = "relay-hosting.secureserver.net"; 
$mail->Port = 25;

//From email address and name
$mail->From = "bruce@brucegust.com";
$mail->FromName = "Bruce Gust";

//To address and name
$mail->addAddress("bruce@brucegust.com", "Bruce Gust");
// $mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("bruce@brucegust.com", "Reply");

//CC and BCC
//$mail->addCC("cc@example.com");
//$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "test";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
*/
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->ssl = false;
$mail->authentication = false;
$mail->addAddress("bruce@brucegust.com", "Bruce");
$mail->isHTML = true;
$mail->Subject = "New Submission From Bruce";
$mail->Body = "Hey";
// $mail->AltBody = $alt_msg;
try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

Open in new window

I get a message that says the email was successfully sent, but no email...

What do you think?
Hey Bruce,

If you want full diagnosis, make sure you leave SMTPDebug on (preferably at level 2 - the code in your second block above turns it off by setting the level to 0).

Now, in the first code block, you seem to setting the host to relay-hosting.secureserver.net - but the errors suggest you can't send email through that server. The GoDaddy docs suggest you use localhost as the host. In the second code block, you do use localhost, but you've also turned off debug mode so you can't see what's going on. Also, you never set a From address in the second block, so sending the email will fail.

Finally, in your second block, you're not passing true into the ctor of PHPMailer (new PHPMailer(true)), which means your code will never throw exceptions - and this means your catch block will never fire, even if there's a problem

Give this very simple example a go:

try {

  $mail = new PHPMailer(true);
  $mail->SMTPDebug = 2; // leave this on while testing!
  $mail->isSMTP();
  $mail->Host = 'localhost';
  $mail->Port = 25;

  $mail->setFrom("bruce@brucegust.com", "Bruce");
  $mail->addAddress("bruce@brucegust.com", "Bruce");

  $mail->Subject = "Test Message";
  $mail->Body = "Hello World!";

  $mail->send();
  echo "Message has been sent successfully";

} catch (Exception $e) {

    echo "Mailer Error: " . $mail->ErrorInfo;

}

Open in new window

1) You mentioned, "David, I'm not sure "mailGun" is going to work, but you tell me."

Mailgun is just 1x of 100s (maybe 1000s) of Mail Relay services.

You'll either use a Relay Service or setup your own native/local/in-house MTA for delivery.

Said differently, phpMailer only sends mail. You'll require a Relay/MTA if you expect sent mail to be delivered.

2) Your settings point to a local MTA, so to have any chance of delivery, you must configure your MTA.

This is not recommended, as it's not for the faint of heart.

I do this all day, every day + still, there so much to keep in mind sometimes it takes me days to resolve delivery problems.

https://www.experts-exchange.com/questions/29194498/how-to-configure-smtp-in-wamp-server.html provides a bare bones setup checklist, which will start the process of having your messages delivered.

3) The warning messages you're seeing relate to this...

//$mail->Host = "";

Open in new window


Which likely defaults to localhost (see #2).

So back to #1, you must use a working Relay or you must provide a working MTA, for any level of delivery.
@David - you keep talking about setting up MTAs and Relay Services - that's not relevant here. GoDaddy are very particular about sending out emails - you have to use their servers! The docs that Bruce linked to above clearly show the settings that are needed.
Hey, Chris!

Here's what I did:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

try {

  $mail = new PHPMailer(true);
  $mail->SMTPDebug = 2; // leave this on while testing!
  $mail->isSMTP();
  $mail->Host = 'localhost';
  $mail->Port = 25;

  $mail->setFrom("bruce@brucegust.com", "Bruce");
  $mail->addAddress("bruce@brucegust.com", "Bruce");

  $mail->Subject = "Test Message";
  $mail->Body = "Hello World!";

  $mail->send();
  echo "Message has been sent successfully";

} catch (Exception $e) {

    echo "Mailer Error: " . $mail->ErrorInfo;

}?>

Open in new window

Here's what I got:

2022-03-18 16:20:43 SERVER -> CLIENT: 220-p3plcpnl1035.prod.phx3.secureserver.net ESMTP Exim 4.94.2 #2 Fri, 18 Mar 2022 09:20:43 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2022-03-18 16:20:43 CLIENT -> SERVER: EHLO brucegust.com
2022-03-18 16:20:43 SERVER -> CLIENT: 250-p3plcpnl1035.prod.phx3.secureserver.net Hello brucegust.com [127.0.0.1]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250-CHUNKING250-STARTTLS250-SMTPUTF8250 HELP
2022-03-18 16:20:43 CLIENT -> SERVER: STARTTLS
2022-03-18 16:20:43 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
2022-03-18 16:20:43 CLIENT -> SERVER: QUIT
2022-03-18 16:20:43
2022-03-18 16:20:43
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'
Mailer Error: SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`*.prod.phx3.secureserver.net' did not match expected CN=`localhost'

Open in new window

I'm using Deluxe Linux Hosting with cPanel. That being the case, because of the verbiage that suggested I use localhost "...unless you use PHP script and the mail() function," I tried this:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

try {

  $mail = new PHPMailer(true);
  $mail->SMTPDebug = 2; // leave this on while testing!
  $mail->isSMTP();
  $mail->Host = ''; //left this blank
  $mail->Port = 25;

  $mail->setFrom("bruce@brucegust.com", "Bruce");
  $mail->addAddress("bruce@brucegust.com", "Bruce");

  $mail->Subject = "Test Message";
  $mail->Body = "Hello World!";

  $mail->send();
  echo "Message has been sent successfully";

} catch (Exception $e) {

    echo "Mailer Error: " . $mail->ErrorInfo;

}

Open in new window

...and I got this:

Invalid hostentry:
SMTP Error: Could not connect to SMTP host.
Mailer Error: SMTP Error: Could not connect to SMTP host.

I want to believe we're poised on the threshold of great things, but I'm still missing something.

I appreciate your time...!

Ok,

I think we're close. The error looks like it's an encryption issue - it's trying to start a TLS connection, which it shouldn't. Lets explicitly turn that off and see what we get. Include this setting and give it another whirl:

$mail->SMTPAutoTLS = false; 

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Progress!

Here's what I did:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

try {

  $mail = new PHPMailer(true);
  $mail->SMTPDebug = 2; // leave this on while testing!
  $mail->SMTPAutoTLS = false; //here's your most recent suggestion
  $mail->isSMTP();
  $mail->Host = 'localhost'; // I added this back after getting an error
  $mail->Port = 25;

  $mail->setFrom("bruce@brucegust.com", "Bruce");
  $mail->addAddress("bruce@brucegust.com", "Bruce");

  $mail->Subject = "Test Message";
  $mail->Body = "Hello World!";

  $mail->send();
  echo "Message has been sent successfully";

} catch (Exception $e) {

    echo "Mailer Error: " . $mail->ErrorInfo;

}

Open in new window

You can see I added your suggestion. Initially I had "$mail->Host" set to "". That resulted in an error, so I put "localhost" back. This time it fired! I got this:

2022-03-18 16:38:45 SERVER -> CLIENT: 220-p3plcpnl1035.prod.phx3.secureserver.net ESMTP Exim 4.94.2 #2 Fri, 18 Mar 2022 09:38:45 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2022-03-18 16:38:45 CLIENT -> SERVER: EHLO brucegust.com
2022-03-18 16:38:45 SERVER -> CLIENT: 250-p3plcpnl1035.prod.phx3.secureserver.net Hello brucegust.com [127.0.0.1]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPE_CONNECT250-AUTH PLAIN LOGIN250-CHUNKING250-STARTTLS250-SMTPUTF8250 HELP
2022-03-18 16:38:45 CLIENT -> SERVER: MAIL FROM:<bruce@brucegust.com>
2022-03-18 16:38:45 SERVER -> CLIENT: 250 OK
2022-03-18 16:38:45 CLIENT -> SERVER: RCPT TO:<bruce@brucegust.com>
2022-03-18 16:38:45 SERVER -> CLIENT: 250 Accepted
2022-03-18 16:38:45 CLIENT -> SERVER: DATA
2022-03-18 16:38:45 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself
2022-03-18 16:38:45 CLIENT -> SERVER: Date: Fri, 18 Mar 2022 16:38:45 +0000
2022-03-18 16:38:45 CLIENT -> SERVER: To: Bruce <bruce@brucegust.com>
2022-03-18 16:38:45 CLIENT -> SERVER: From: Bruce <bruce@brucegust.com>
2022-03-18 16:38:45 CLIENT -> SERVER: Subject: Test Message
2022-03-18 16:38:45 CLIENT -> SERVER: Message-ID: <43rbPRdfr3Hzl0MCIVLlJG4yPQCyJnfhZMaei4khzs@brucegust.com>
2022-03-18 16:38:45 CLIENT -> SERVER: X-Mailer: PHPMailer 6.6.0 (https://github.com/PHPMailer/PHPMailer)
2022-03-18 16:38:45 CLIENT -> SERVER: MIME-Version: 1.0
2022-03-18 16:38:45 CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
2022-03-18 16:38:45 CLIENT -> SERVER:
2022-03-18 16:38:45 CLIENT -> SERVER: Hello World!
2022-03-18 16:38:45 CLIENT -> SERVER:
2022-03-18 16:38:45 CLIENT -> SERVER: .
2022-03-18 16:38:45 SERVER -> CLIENT: 250 OK id=1nVFcj-00DQTl-VQ
2022-03-18 16:38:45 CLIENT -> SERVER: QUIT
2022-03-18 16:38:45 SERVER -> CLIENT: 221 p3plcpnl1035.prod.phx3.secureserver.net closing connection
Message has been sent successfully

Open in new window

...but no email. I checked my "Junk" file just to be certain, but nothing was actually deliverd.

Do you smell anything wrong?
Hmmm - OK. That now looks like your PHP script is working and doing exactly what it should be doing. You might need to give it a few minutes.

Something that may or may not be related - I've been trying to reset my GoDaddy password all afternoon, and their site keeps telling me that they've sent me a Reset Password email, but I still haven't received any of there emails (my mail account is NOT hosted by GoDaddy), so maybe they've got a problem at their end..
Alight! We'll wait and see! Thanks for helping me navigate this thing!
No worries bud,

It looks like you've got everything right at your end, so if it doesn't resolve in a timely fashion, you may want to speak with GoDaddy tech support. Not sure if they have a 'Network Status' page that may shed light on it ?
Gentlemen, I was able to get the email functionality to work using some older code on the server, so the dragon  has been slayed. As far as the initial quandary, we'll keep these comments archived for the next round of combat. As always, thanks for your help!