0

My PHPMailer script is not working for the form here. I am using Gmail SMTP. The form does not have attachment option so i disabled it. Please note I have replaced SMTP login info, from email and to email with dummy emails only for the purpose of posting on stackoverflow. BTW, could it have anything to do with the absolute url to the autoload.php file?

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require content_url('/phpmailer/vendor/autoload.php');

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 1;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'example@gmail.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('example2@gmail.com', 'example2');
    $mail->addAddress('example3@gmail.com');     // Add a recipient

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Application form submission';
    $mail->Body    = $message;
    $mail->AltBody = strip_tags($message);

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

Can anyone tell me why it isn't working?

I am getting the following errors:-

Notice: Undefined index: income_checkbox in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 495

Warning: require(): https:// wrapper is disabled in the server configuration by allow_url_include=0 in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 5164

Warning: require(https://www.financemi.com.au/wp-content/phpmailer/vendor/autoload.php): failed to open stream: no suitable wrapper could be found in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 5164

Fatal error: require(): Failed opening required 'https://www.financemi.com.au/wp-content/phpmailer/vendor/autoload.php' (include_path='.:/usr/share/pear/php:/usr/share/php:/usr/share/pear') in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 5164

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Grey-lover
  • 635
  • 3
  • 7
  • 21
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jun 21 '18 at 12:01
  • @RiggsFolly I added error reporting and I have mentioned the errors above. Could you check please. – Grey-lover Jun 21 '18 at 12:16
  • 1
    Most of those are Wordpress errors that don't have anything to do with PHPMailer; it's only the fatal error that's relevant. – Synchro Jun 21 '18 at 13:25

1 Answers1

1

You need to require the autoloader locally, not via HTTP/HTTPS. (Because via HTTP you would be requiring script's output, which would be empty, not its code.)

Change:

require 'https://www.financemi.com.au/wp-content/phpmailer/vendor/autoload.php';

to:

require __DIR__.'/../../../wp-content/phpmailer/vendor/autoload.php';

(Updated to reflect that PHP script gets eval()'d inside wp-content/plugins/insert-php-code-snippet/ directory.)

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
  • It didnt work, perhaps because it is wordpress. Any ideas for the path then? – Grey-lover Jun 21 '18 at 12:09
  • Ok I so I corrected the path and got errors, mentioned in question, care to check please? – Grey-lover Jun 21 '18 at 12:17
  • Well you changed it to `content_url()` call, which in turn generates URL with HTTP/HTTPS, so you are right where you started. Are any error messages shown when you use `require` as indicated in my reply? – Jirka Hrazdil Jun 21 '18 at 12:18
  • When I use require as you showed, I got the following errors:- – Grey-lover Jun 21 '18 at 12:20
  • Notice: Undefined index: income_checkbox in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 493 – Grey-lover Jun 21 '18 at 12:20
  • Warning: require(/nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/../wp-content/phpmailer/vendor/autoload.php): failed to open stream: No such file or directory in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 5161 – Grey-lover Jun 21 '18 at 12:20
  • Fatal error: require(): Failed opening required '/nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/../wp-content/phpmailer/vendor/autoload.php' (include_path='.:/usr/share/pear/php:/usr/share/php:/usr/share/pear') in /nas/content/live/financemi/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php(65) : eval()'d code on line 5161 – Grey-lover Jun 21 '18 at 12:21
  • Ok, didn't know that PHP code would be run through the plugin. So the correct command is: `require __DIR__.'/../../../wp-content/phpmailer/vendor/autoload.php';` – Jirka Hrazdil Jun 21 '18 at 13:19
  • That's some crazy include path. You should be using one of the Wordpress functions that returns the absolute path to the wp-content folder, will be much more reliable. – Synchro Jun 21 '18 at 13:28
  • @Synchro Feel free to post your own answer using WordPress' functions. I won't do it as I am not proficient enough with WordPress. – Jirka Hrazdil Jun 21 '18 at 13:48
  • There's this great stuff called [documentation](https://developer.wordpress.org/reference/classes/wp_filesystem_base/wp_content_dir/)... – Synchro Jun 21 '18 at 14:52
  • @Synchro I already tried using content_url(). IT returns absolute path and it did not work. – Grey-lover Jun 22 '18 at 04:41
  • @JiriHrazdil Ok so I tried the new path and now the thank you page never opens after the submit button is clicked. So I cant even tell what the errors are. Any suggestions. – Grey-lover Jun 22 '18 at 06:24
  • You've already been told not to use URLs - they won't work. The function in the link I provided gets the absolute local file system path (what you need), not a URL. Also, does `vendor/autoload.php` actually exist? It's not part of the PHPMailer file set but is created when you run composer. You may want to load the PHPMailer files manually instead of using that - see [the readme](https://github.com/PHPMailer/PHPMailer) for instructions on how to do that. – Synchro Jun 22 '18 at 07:41