Jump to content

Is there a reason why Phpmailer doesn't work when called from inside a sub directory?


Recommended Posts

I am using a phpmailer to send emails to users. I noticed that it only sends email if I am calling it from a page in a root directory. Say I am sending an email from contact.php and it's inside a sub folder/directory instead of the root, it won't email out.


Setup is like this.

	    include_once '../phpmailer/class.phpmailer.php';
	    $mail = new PHPMailer;

	    $mail->IsSMTP();                                      // Set mailer to use SMTP
	    $mail->Host = '127.0.01'; 							  // Specify main and backup server

	    $mail->From = 'hello@myemail.com';
	    $mail->FromName = 'Myname';

	    $mail->AddAddress($user_email);             				  // Name is optional

	    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
	    $mail->IsHTML(true);                                  // Set email format to HTML

	    $mail->Subject = 'Your account has been deleted!';
	    $mail->Body    =  "Hello {$username},<br><br>

	    We are letting you know that your account has been deleted";
if(!$mail->Send()) {
              $errors[] = $mail->ErrorInfo;
            } else {
              if(empty($errors)) {
                $success = 'Email sent.';
   } else {
Edited by imgrooot
Link to comment
Share on other sites

Turn on error reporting or logging and check there. It's likely that the path in the "include_once" portion is not correct and it fails there. 


What are the absolute paths to phpmailer/class.phpmailer.php and the script that you're running? 

Link to comment
Share on other sites

You definitely need proper error reporting and handling. Get rid of the ErrorInfo stuff (which you don't seem to care about anyway) and enable exceptions by passing true to the constructor. Then you'll get a meaningful message rather than "nothing" (assuming your PHP configuration is sane).


You should also consider writing a PHPMailer wrapper instead of repeating the code in every script -- and obviously messing it up. Once the wrapper works, it works in every script, and you no longer have to worry about typos.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.