Mail script not working

The double quote is part of this: “.$_POST[‘Name’].”

Like this:

$mail->send();
echo 'Dear ' . htmlspecialchars($_POST['Name']) . ', 
    <br>Thank you for your message. 
    <br>I will respond as soon as possible.';

That works. Thanks a lot, James, appreciate it.

So to make it clear…
the double quote isnt part of that.

. joins two parts of something in PHP. Usually different things.

So to take James’ version…

the . is joining a string 'Dear ' and a function result htmlspecialchars($_POST['Name'])

A string begins with either a single quote ' or a double quote ", and ends with the same symbol as it started with. That’s so you can put the other kind of quote in without confusing PHP.

' This is all part of the string '
" This is all part of the string "
' This started with a single quote, so i can put "double quotes" inside it without problem.'

Thanks, m_hutley.
As I had mentioned before, I’m not a savvy PHP guy, more like an HTML one. :ghost:

One thing I haven’t figured out yet is this:

$mail->AltBody = "Name: ".$_POST['Name']." 
                             | Phone: ".$_POST['Phone']." 
                             | Comments: ".$_POST['Comments']."
                             | Email: ".$_POST['E-mail'];

It looks like there are a bunch of double quotes missing.
What is it supposed to do?

a string begins and ends with the quote - that includes line breaks. Maybe if i get the colors out, it will help visualize…

$mail->AltBody = "Name: ".$_POST['Name']." 
                             | Phone: ".$_POST['Phone']."  
                             | Comments: ".$_POST['Comments']."
                             | Email: ".$_POST['E-mail'];

I’ve colored all the strings in the line.

Yes, it did help visualize, thanks.
So, what is it supposed to do?

It’s putting together one long string, line breaks and all, to go into the email’s AltBody. The AltBody is what gets presented if the receiving mail client says “I dont/cant accept HTML emails.”

OK, got it, thanks. I learn more about PHP by the minute. :laughing:

Guys, I’m back. I hate to appear stupid, but apparently, I am. As Forrest Gump used to say “Stupid is as stupid does”.
Here’s the situation: i have adapted that script for another website and it doesn’t work again.
Here’s the script:

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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
$email = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL) || "not-a-valid-email@not-a-real-email.com";
$name = filter_var($_POST['Name'], FILTER_SANITIZE_STRING) || "Bad Name";
$textarea = filter_var($_POST['Message'], FILTER_SANITIZE_STRING) || "Message is missing!";

$mail = new PHPMailer(true);
try {
  $mail->setFrom('em@icom-design.com', 'Form Mail Handler');
  $mail->addAddress('em@icom-design.com',);   
  $mail->addReplyTo($_POST['E-mail'], 'Information');
  $mail->isHTML(true);                                  //Set email format to HTML
  $mail->Subject = 'MCCW Website Inquiry';
  $mail->Body = '<ul>
                                   <li>Name:'.$_POST['Name']."</li>
                                   <li>Email: ".$_POST['Email']."</li>
                                   <li>Message: ".$_POST['Message']."</li>
                            </ul>";
  
  $mail->AltBody = "Name: ".$_POST['Name']." 
                             | Message: ".$_POST['Message']."
                             | Email: ".$_POST['E-mail'];
$mail->send();
    echo 'Dear ' . htmlspecialchars($_POST['Name']) . ',
    <br>Thank you for your message. 
    <br>I will respond as soon as possible.
    <br>Ed Miller
    <br>MCCW ';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

Here’s the form:

Inquiries

   If you have any questions regarding a menu, a recipe or about the club in general, please send the form below.

   PRIVACY NOTICE: We will NOT lease or sell your personal information!

Name:  

Email:  

Message:

When I ran it, an error message pops up that the website can’t currently process it. I have tried everything that I learned from you guys last week but to no avail. What did I do wrong this time?

And is it still the HTTP error 500? The error number is the important bit, tells people where to start looking.

Presumably you’ve installed the PHPMailer stuff on the other web site?

Yes, it’s error 500 and, yes, the PHP stuff is installed.

At this point, the PHP Error log should tell you what’s gone wrong.

There’s not going to be much we can tell you from here if the code works in one place and not in the other, if you’ve recreated the site structure exactly as before. The mailer code is in a try-catch, so it’s not getting as far as sending the mail.

squints (this… shouldnt cause an error, but it makes me wince that there’s a dangling comma in there.)

Well, that dangling comma is in there because the previous script has actually 2 dangling commas and it works.

So, should I check with the hosting company re the server error?

If you have error logging enabled on your server, examine the error log to see if it points you in the right direction. If you don’t but can have, enable it and then run again.

If that doesn’t tell you anything useful, I’d probably start putting exit() statements in the code at various locations to try to narrow down how far it gets before the 500 error.

Is the second site running the same version of PHP as the first one?

Here is the latest line of the rror log:

154.12.226.41 - - [05/Sep/2024:07:55:30 -0400] "POST /formhandle.php HTTP/1.1" 500 0 "http://mccwimberley.com" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:114.0) Gecko/20100101 Firefox/114.0"

It does not seem to tell anything.
And, yes, those sites run the same PHP version.

that’s an HTTP log, not a PHP error log.

You may need to ask the hosting company.