Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

Pat Rick
Pat Rick
2,570 Points

Error sending an email. Everything was going well until I tried to send a message to a gmail.

GOT THIS ERROR "Message could not be sent.Mailer Error: Could not instantiate mail function."

  require("inc/phpmailer/class.phpmailer.php");

            $mail = new PHPMailer;

            if (!$mail->ValidateAddress($email)) {
                echo "Ivalid email address";
                exit;
            }



            $email_body = "";
            $email_body .= "Name " . $name . "\n";
            $email_body .= "Email " . $email . "\n";
            $email_body .= "Telephone " . $tel . "\n";
            $email_body .= "Comments " . $message . "\n";


            $mail->setFrom($email, $name);
            $mail->addAddress('********61@gmail.com', 'Sean');

            $mail->isHTML(false);

            $mail->Subject = 'hdhhdhshshsh from ' . $name;
            $mail->Body = $email_body;

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

            // To Do: Send email

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

          <?php 
          if (isset ($_GET["status"]) && $_GET["status"] == "thanks") {
            echo "<center><p> Thanks for the email! I&rsquo;ll check out your suggestion shortly! </p></center>";
          } else { ?>

Moderator Edit: Added Markdown to code snippet so that it is readable in the Community post --- also obscured personal information within post... placing personal information (i.e. email address) in a publicly accessible forum is not a good idea, as it opens you up to scammers, spammers, and bots.

Victoria Loisi
Victoria Loisi
7,070 Points

Hi there! I would start by inserting everything inside a try/catch statement, like this:

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

Also, have you tried $phpMailer->Send() instead of $phpMailer->send()?

Radulescu Ramona
Radulescu Ramona
3,687 Points

same thing happened to me.. I've tried your method, Victoria Loisi, but it didn't work.