Jump to content
Sign in to follow this  
imgrooot

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)) {
   $db->commit();
 
                $success = 'Email sent.';
 
   } else {
   $db->rollBack();
   }
}
Edited by imgrooot

Share this post


Link to post
Share on other sites

Nothing happens. I don't get any errors. I just never receive the email in the inbox.

Share this post


Link to post
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? 

Share this post


Link to post
Share on other sites

 

	    $mail->Host = '127.0.01'; 							  // Specify main and backup server

 

You're missing a . there. Should be

$mail->Host = '127.0.0.1';
Are you running a local SMTP server?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

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

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

Sign in to follow this  

×
×
  • 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.