Problems with paths and phpMailer

I can’t get phpMailer to work, and am really frustrated right now… :anguished:

Here is my directory/file structure…

home_directory
    public_html
        contact-us.php


        utilities
            functions.php

    outside_root
        PHPMailerAutoload.php
        class.phpmailer.php
        class.smtp.php

Here are my key files…

contact-us.php

    require_once(WEB_ROOT . 'utilities/functions.php');

    // Code to sanitize and process submitted form.

    // Call phpMailer
    $emailResults = callPHPMailer($inquiryEmail, $to, $subject, $body);
//    mail($to, $subject, $body, $headers);

    if ($emailResults){
        echo "Email was Successful!";
        exit();
                    
    }else{
        echo "Email Failed.";
        exit();
    }

functions.php

function callPHPMailer($from, $to, $subject, $body){
    require_once('../../outside_root/PHPMailerAutoload.php');

    $mail = new PHPMailer;

    // Email Server Log-in Credentials
    $mail->isSMTP();
    $mail->Host = 'xx.yy.zz.aa';
    $mail->SMTPAuth = true;
    $mail->Username = 'support@domain.com';
    $mail->Password = 'pass';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    // Email Message Details
    $mail->From = $from;
    $mail->addAddress($to);
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $body;

    $mail->send();

    return $results;

}//End of callPHPMailer

Right now I get this error…

Warning: require_once(../../outside_root/PHPMailerAutoload.php): failed to open stream: No such file or directory...

Fatal error: require_once(): Failed opening required 
'../../outside_root/PHPMailerAutoload.php' 
(include_path='.:/Applications/MAMP/bin/php/php5.4.10/lib/php') in...

When I had just …/outside_root… I kept seeing “Waiting on local.mysite” in the bottom of the browser window.

So then I tried to fix the path and now it errors out.

I’m not sure what is going on, and not knowing object-oriented programming doesn’t help when it comes to phpMailer.

Can someone please help me figure out what I am doing wrong?

Here is where I was going wrong…

While it would seem logical to use this path to point FROM “functions.php” TO “PHPMailerAutload.php”…

require_once('../../outside_root/PHPMailerAutoload.php');

…thing don’t work that way in PHP.

Because I INCLUDED “functions.php” in “contact-us.php”, PHP treats “functions.php” like it is at the same level as the including file.

So while it is physically here…

/public_html/utilities/functions.php

PHP treats it like it is here…

/public_html/

That of course messed up my path reference FROM “functions.php” TO “PHPMailerAutload.php”…


On a side note, another mysterious issue I was having at the same time of this one, but which seems to have disappeared was the fact that when I clicked “Submit” the Comment/Question was spinning out and I saw a “Waiting on local.mysite…” in the bottom of my browser.

Things failed several times, and then after supper, thing started working.

Not sure what that was all about.

Anyway, I think I have my first ever use of phpMailer working finally.

Not sure how much better it is than just mail( ), but they say it is?!

Here is one other crucial thing I had to do to make phpMailer work across my client’s site…

Since several different scripts - located in several different part of my directory-tree - INCLUDE “functions.php”, the solution I showed above only works if the parent script is in the Web Root.

To fix this, I created a constant called “HOME” like this…

// Home Directory.
define('HOME', ENVIRONMENT === 'dev'
        ? '/Users/mikey_w/Documents/25-Work/++htdocs/06_ACME/'
        : '/home/acme7/');

Then in my “functions.php” I was able to change things to this…

        // Access Email Classes.
        require_once(HOME . 'outside_root/PHPMailerAutoload.php');

This way, not matter which script INCLUDES “functions.php”, when “functions.php” then INCLUDES “PHPMailerAutoload.php”, things always work.

(Personally, I think it is stupid how this works in PHP w.r.t. INCLUDES, but at least I have a workaround!!)

There may be a more eloquent way to do this - especially in OOP - but it solved my problem, and I figured I would share here!

You’re spot on. Another only slightly different option is to use __DIR__.

require_once(__DIR__.'/../../outside_root/PHPMailerAutoload.php');

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.