PHPMailer() isn't working - help?

January 12, 2015 4.7k views

I am trying to send outgoing emails from my PHP app. Here is the error I get:

"Fatal error: Class 'PHPMailer' not found in /usr/share/nginx/templates/webapp.php"

And here is my PHP code for sending the mail, with the require commented out:

//require ( 'class.phpmailer.php');
$mail = new PHPMailer();

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = ""; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = ""; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail-> Subject = $emailsubject ;
$mail -> Body = $emailbody ;
if ($mail->Send() == false )
die($mail->ErrInfo) ;

When I include the 'require' statement, I get the following error:

Warning: require(class.phpmailer.php): failed to open stream: No such file or directory in /usr/share/nginx/templates/webapp.php

Help? Thanks!!

  • Your code is showing your include of class.phpmailer.php as commented out. Since the file with the class is not being loaded the script is not able to use the functions provided in that class.

  • Yes -- as I mentioned after the code, I get a different error when I include that line. Any ideas?

3 Answers

Ok, when you uncomment the require function it looks for the file class.phpmailer.php so you either need to put that file in the same directory as webapp.php or you need to specify where the php script is. You can modify your require function to point to the script so it would look somethikng like this

require ('path/to/file/class.phpmailer.php');

Hope this helps.

Good Luck!

  • Thank you for responding! I am still stuck :(

    Is PHPMailer automatically included with a PHP download? I didn't think I would have to download it separately. But I did just in case, and I put it in a special folder and directed the require() statement to look there. Then I got a different Fatal Error, this time about class SMTP. So I added another require() statement, with class.smtp.php. Now my application just takes foreeeeever to load and for some reason cuts out in the middle of the page. Strange.

  • That's weird. are you sure you the path is set correctly? instead of 'require' try changing to 'include' see if that helps

  • Yeah, I am sure. Unfortunately changing it to require doesn't help.

  • Just FYI, phpmailer is not included with php install, it needs to be install separately. Now, let's tale take a step back and make sure everything is setup properly.

    How did you install the LEMP stack?

    Did you install it a MTA such as postfix?

  • I haven't heard of an MTA before you mentioned it but I am looking into it now. I installed LEMP using this tutorial:

    by Justin Ellingwood
    A LEMP stack (Linux, Nginx, MySQL, and PHP) is a powerful set of software that can be configured to serve dynamic sites and web apps from your server. In this guide, we will discuss how to install a LEMP stack on an Ubuntu 14.04 server.

try with the latest PHPMailer example in here >>

and you can check whether MTA is working on your machine(linux) by sending a test email by following command

echo "this is email body"|mail -s "This is email subject"
  • I tried that in putty.exe and it worked for about 5 minutes and then the prompt came back up again. I tried sending to my gmail account. However, I didn't receive any email (not even in my Spam).

    Thanks for commenting

  • Actually, the emails have come through now! (Tho there went to Spam). I am still working on getting regular PHPMailer to work

  • I am getting a 504 Gateway Timeout whenever I try to send with PHPMailer

Have another answer? Share your knowledge.