2

I am struggling to get PHPMailer setup and working in my application.

The installation seems simple enough. My directory structure looks like this:

|-[controllers]
|---controller.php
|-[vendor]
|---[PHPMailer]
|------class.phpmailer.php
|------PHPMailerAutoload.php
|-index.php

index.php

<?php
   ...
   require __DIR__ . '/vendor/PHPMailer/PHPMailerAutoload.php';
   ...
?>

controller.php

<?php
    include('vendor/PHPMailer/class.phpmailer.php');
    $mail = new PHPMailer();
    ...
?>

That's pretty much as simple as the example gets, but when I run this, I get the following error:

Parse error: syntax error, unexpected end of file in C:\wamp\www\commway\vendor\PHPMailer\class.phpmailer.php on line 2995

Everything was working fine, I can't see how I've managed to break this. I have even tried moving everything to just the index.php file but I get the same result. As soon as I call $mail = new PHPMailer(); it throws the error.

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Riples
  • 1,167
  • 2
  • 21
  • 54
  • Has `C:\wamp\www\commway\vendor\PHPMailer\class.phpmailer.php` adequate content? – Nick May 20 '16 at 05:34
  • In controller try to do require instead of include. If that gives you error one line above means your file is still not found at path. as include allows your code execution even if file is not found. – Mukesh Ram May 20 '16 at 05:35
  • Also as your structure seems MVC, if you are using composer then try loading phpmailer via composer like "composer require phpmailer/phpmailer" – Mukesh Ram May 20 '16 at 05:38
  • @MukeshRam I had done that originally. However, now I have resorted to downloading the zip file and placing the files manually. – Riples May 20 '16 at 05:38
  • @MukeshRam I have changed my code to `require` and when I click on the file path, it takes me to the correct file. However, I still get the error. In fact, even if I remove the line `$mail = new PHPMailer();` I still get the error. – Riples May 20 '16 at 05:40
  • Please recheck that there is not permission issue, and you have to declare object in your index file – DsRaj May 20 '16 at 05:43
  • Also try opening file C:\wamp\www\commway\vendor\PHPMailer\class.phpmailer.php in text editor as per your system like notepad, gedit and check if any lines are not like {?> or from if it is in last line and any blank space from last lines and try it again – Mukesh Ram May 20 '16 at 05:46
  • It looks like this line is affecting it: `return (boolean)preg_match( '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?.........`. It seems to be closing the PHP scripting. I commented out that code and it now seems to execute further. I now get this error: `Fatal error: Call to undefined method PHPMailer::Send()` – Riples May 20 '16 at 05:58
  • @Riples No, until you have ?> in string it is not parsed so, no use to change this line – Mukesh Ram May 20 '16 at 06:01
  • I noticed the `class.phpmailer.php` that I downloaded from the zip file was version 5.2.13. I replaced it with version 5.2.14 and now I have success. – Riples May 20 '16 at 06:04
  • That is great then – Mukesh Ram May 20 '16 at 06:11
  • Yes, but I really don't understand what the issue was. Unless it was an error in version 13. Apart from that, the only change I made was changing `include` to `require`. But thanks for your help. – Riples May 20 '16 at 06:27
  • I think you just had a corrupt/truncated file. PHPMailer's files do not ship with basic syntax errors in! – Synchro May 20 '16 at 08:02

1 Answers1

2

I have this code running :

//      PHPmailer

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host = SMTP;                         // Specify main and backup SMTP servers/*

$mail->From = EMAIL;
$mail->FromName = SENDER;
$mail->addAddress($Email, $FirstName);      // Add a recipient
$mail->WordWrap = 50;                       // Set word wrap to 50 characters
$mail->isHTML(true);                        // Set email format to HTML

$mail->Subject = $mailSubject;
$mail->Body    = $message;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent to ' . $Email . "<br>";
} 

//      end of PHPmailer

with structure :

PHPMailer-Master
-class.phpmailer.php
-class.pop3.php
-class.smtp.php
-PHPmailerAutoload.php
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
Louis
  • 2,854
  • 2
  • 19
  • 24