imgrooot Posted June 7, 2017 Share Posted June 7, 2017 (edited) 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 June 7, 2017 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/ Share on other sites More sharing options...
Sepodati Posted June 7, 2017 Share Posted June 7, 2017 What happens when it "doesn't work"? Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/#findComment-1547125 Share on other sites More sharing options...
imgrooot Posted June 7, 2017 Author Share Posted June 7, 2017 Nothing happens. I don't get any errors. I just never receive the email in the inbox. Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/#findComment-1547126 Share on other sites More sharing options...
Sepodati Posted June 7, 2017 Share Posted June 7, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/#findComment-1547127 Share on other sites More sharing options...
kicken Posted June 7, 2017 Share Posted June 7, 2017 $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? Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/#findComment-1547128 Share on other sites More sharing options...
Jacques1 Posted June 7, 2017 Share Posted June 7, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/304085-is-there-a-reason-why-phpmailer-doesnt-work-when-called-from-inside-a-sub-directory/#findComment-1547131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.