Tom V Posted February 26, 2020 Share Posted February 26, 2020 When testing the forgotPassword module, the emails are sent from [email protected]. How should I modify the forgotPassword.inc.php file to use the from_email field in the settings table instead? I'm a total newbie on this stuff. Thanks for any help, Tom Quote Link to comment Share on other sites More sharing options...
Eurogangster Posted February 26, 2020 Share Posted February 26, 2020 Find this in the forgotPassword.inc.php if (isset($user["U_id"])) { Under the $body add $headers = "From: [email protected]" ; And you are ready 1 Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted February 26, 2020 Share Posted February 26, 2020 3 hours ago, Eurogangster said: Find this in the forgotPassword.inc.php if (isset($user["U_id"])) { Under the $body add $headers = "From: [email protected]" ; And you are ready Find this in the forgotPassword.inc.php if (isset($user["U_id"])) { Under the $body add $headers = "From: [email protected]" ; And you are ready This method would hard-code the email address. OP wants to use the dynamically-set address in the site settings. I'm not massively familiar with GL, so I can't advise further Quote Link to comment Share on other sites More sharing options...
Eurogangster Posted February 26, 2020 Share Posted February 26, 2020 3 minutes ago, Magictallguy said: This method would hard-code the email address. OP wants to use the dynamically-set address in the site settings. I'm not massively familiar with GL, so I can't advise further He could save in a variable the email from the DB ->Settings Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted February 26, 2020 Share Posted February 26, 2020 4 hours ago, Eurogangster said: He could save in a variable the email from the DB ->Settings ..Yes, he already has done. He's attempting to get that variable from the settings Quote Link to comment Share on other sites More sharing options...
Tom V Posted February 26, 2020 Author Share Posted February 26, 2020 I tried adding this to the allowed methods: 'from_email'=>array('type'=>'get') I added this to constructModule: $this->page->addToTemplate("from_email", $settings->loadSetting("from_email")); Then I added this: $from = $this->methodData->from_email; $headers[]= 'From:'.$from; And I changed this line to this: mail($user["U_email"], "Password Reset", $body, implode("\r\n", $headers)); The result is the email that is sent goes to spam folder, because the from address shows up as (unknown sender). This is where I'm stuck. Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted February 27, 2020 Share Posted February 27, 2020 (edited) public function method_reset() { $this->error("A password reset email has been sent to you!", "success"); $user = $this->db->select("SELECT * FROM users INNER JOIN userStats ON (US_id = U_id) WHERE U_email = :email ORDER BY U_id DESC", array(":email" => $this->methodData->email)); if (isset($user["U_id"])) { $url = $_SERVER["HTTP_ORIGIN"] . $_SERVER["SCRIPT_NAME"] . "?page=forgotPassword&action=resetPassword&auth=" . $user["U_password"] . "&id=" . $user["U_id"] . ""; $settings = new settings(); $from = $settings $headers['From'] = $settings->loadSetting('from_email'); $body = "To reset your password please follow the link below: \r\n " . $url; mail($user["U_email"], "Password Reset", $body, $headers); } } Try that. Edit: I’m on mobile and it’s real difficult to edit my code I pasted but delete the entire line that says $from = $settings Edited February 27, 2020 by KyleMassacre 2 Quote Link to comment Share on other sites More sharing options...
Tom V Posted February 27, 2020 Author Share Posted February 27, 2020 Kyle, That's an effective solution. Thanks. Quote Link to comment Share on other sites More sharing options...
TonyCisseroni Posted February 27, 2020 Share Posted February 27, 2020 if (isset($user["U_id"])) { $url = $_SERVER["HTTP_ORIGIN"] . $_SERVER["SCRIPT_NAME"] . "?page=forgotPassword&action=resetPassword&auth=" . $user["U_password"] . "&id=" . $user["U_id"] . ""; $settings = new settings(); $headers["From"] = "From: "; $headers['From'] .= $settings->loadSetting('from_email'); $body = "To reset your password please follow the link below: \r\n " . $url; mail($user["U_email"], "Password Reset", $body, $headers['From']); } Made a small change so that the array value is formatted correctly. Tested and this solution is working! thank you KyleMassacre Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted February 27, 2020 Share Posted February 27, 2020 (edited) 5 hours ago, TonyCisseroni said: Made a small change so that the array value is formatted correctly. Based off the docs, it is formatted correctly if you are using v7.2 which I should've verified. To make it more universal, it should really be like this: public function method_reset() { $this->error("A password reset email has been sent to you!", "success"); $user = $this->db->select("SELECT * FROM users INNER JOIN userStats ON (US_id = U_id) WHERE U_email = :email ORDER BY U_id DESC", array(":email" => $this->methodData->email)); if (isset($user["U_id"])) { $url = $_SERVER["HTTP_ORIGIN"] . $_SERVER["SCRIPT_NAME"] . "?page=forgotPassword&action=resetPassword&auth=" . $user["U_password"] . "&id=" . $user["U_id"] . ""; $settings = new settings(); $headers = null; if (version_compare(PHP_VERSION, '7.2.0') >= 0) { $headers['From'] = $settings->loadSetting('from_email'); $headers['MIME-Version'] = '1.0'; $headers['Content-type'] = 'text/html; charset=iso-8859-1'; } else { $headers = 'From: '.$settings->loadSetting('from_email').'\r\n'. 'MIME-Version: 1.0 \r\n'. 'Content-type: text/html; charset=iso-8859-1 \r\n'; } $body = "To reset your password please follow the link below: \r\n <a href='{$url}'>Click Here</a>" \r\n; $body .= "If the link is not visible, then follow this url below: \r\n {$url}" mail($user["U_email"], "Password Reset", wordwrap($body, 70, "\r\n"), $headers); } Edited February 27, 2020 by KyleMassacre Updated code for mistake in else statement. PHP < 7.2 does not allow an array for headers Quote Link to comment Share on other sites More sharing options...
TonyCisseroni Posted February 27, 2020 Share Posted February 27, 2020 Sorry for the confusion. I meant to format the value in the array as "From: [email protected]" instead of just the email. The mail function needs it that way. Also if you look at the mail function you have to call $headers['From'] or you will get an error because it's looking for a string value not an array. The code I posted is tested and working. Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted February 27, 2020 Share Posted February 27, 2020 (edited) 56 minutes ago, TonyCisseroni said: Sorry for the confusion. I meant to format the value in the array as "From: [email protected]" instead of just the email. The mail function needs it that way. Also if you look at the mail function you have to call $headers['From'] or you will get an error because it's looking for a string value not an array. The code I posted is tested and working. I did it again, didn’t I lol. It’s not supposed to be an array in the else statement. Updated post Edited February 27, 2020 by KyleMassacre 1 Quote Link to comment 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.