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…




Here are my key files…


    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!";
        echo "Email Failed.";


function callPHPMailer($from, $to, $subject, $body){

    $mail = new PHPMailer;

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

    // Email Message Details
    $mail->From = $from;
    $mail->Subject = $subject;
    $mail->Body = $body;


    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 
(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”…


…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…


PHP treats it like it is here…


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__.


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