MyBB Community Forums

Full Version: Change Default System Mail Address (PHP Mail)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to manually override the default system mailing address.

Emails are not being sent from the email address I have specified in Admin CP --> Config --> Admin Email & Return Email. They are being sent from the system default which uses my hosts domain and fail to deliver (blocked by email providers like Gmail). I made a test email for [email protected] and sent an email to a friend's gmail, worked beautifully. So I figured I would use this email instead!

However, it doesn't work. It still sends from the default system address despite having the [email protected] in Admin Email & Return Email.

I'd like to manually override the email in which PHP mail sends. Found this gem from 1.6 but I can't find this anywhere in functions.php.

Help would be much appreciated!

Literally solved this problem 5 minutes after posting.

For anyone else coming across this later on I found:
function my_mail($to, $subject, $message, $from="", $charset="", $headers="", $keep_alive=false, $format="text", $message_text="", $return_email="")
And added in the email to the $from section, it worked lol:
function my_mail($to, $subject, $message, $from="[email protected]", $charset="", $headers="", $keep_alive=false, $format="text", $message_text="", $return_email="")
Hmm. That shouldn't be necessary. It's fine if you're happy with your solution, but if you're willing to dig into this further, then we can start with this:

What have you got selected under the ACP's Configuration » Mail Settings » Mail handler?
Setting is for PHPMail.

This worked in my situation because my host is missing a PTR record for their main domain. All accounts on their server default to “[email protected]”. This causes Gmail and other providers to completely block anything coming from that address. Privately owned VPS, friend gives me space to work projects.

Previous to the above solution the forum was sending emails with the systems default address (the host domain one) which failed so I had to manually activate and reset passwords. This was evening happening with ACP settings for admin and return email having “[email protected]”. It’s as if the settings weren’t even used.

Manually adding my email into the script above finally made it start sending using my email set and not the account email.
Huh. That's very odd. I don't think it has anything to do with the lack of a PTR record though. The problem seems to be that when my_mail() is supplied with an empty $from parameter - which it almost always is - the correct default value is for some reason not being set. When that parameter is empty, what should happen is that the call to MailHandler::get_from_email()  in MailHandler::build_message() (called by my_mail()) sets the mail handler's from_named property based on your admin email setting. MailHandler::build_message() then goes on to call MailHandler::set_common_headers(), which should use this value for the "From" header, storing the fully-generated headers in the object's headers property. PhpMail::send() then supplies that headers property to the PHP builtin mail() function.

I don't know if you're at all interested in doing your debugging into why this is somehow failing, but that hopefully gives you a bit of a sense as to where you might want to start investigating the problem. Core edits are inadvisable since they are prone to being overwritten on upgrade, but of course you're under no obligation to change your approach.
How much further can I debug if I don’t own the server? I have limited WHM access as I am a reseller for free hosting for a specific niche for MyBB community forums.

Please let me know what debugging process I need to go through. I’m not fluent in a lot of these things so truthfully I’m not even sure if I’d know what I’m looking at lol.

I do hate altering core files, perhaps this is something I could manage with a plugin build instead? Temporarily until whatever debugging you speak of can be used to fix the issue.

Thanks!
Well, i have my own, perhaps idiosyncratic, clunky, method of debugging. it's based on editing PHP files to output crucial information via whatever pragmatic means is most appropriate in the situation - e.g., echo, var_export(), or file_put_contents(..., ..., FILE_APPEND) - the last of which seems most appropriate in your context, a live site.

So, for example, to debug the current problem you're experiencing, you might start by inserting at line #132 of inc/mailhandler.php the following code:

file_put_contents(MYBB_ROOT.'my-debug-output.txt', 'The value of $email just before the return in MailHandler::get_from_email() is: "'.$email.'".', FILE_APPEND);

You would then perform some action on your forum which should send an email, and then open up the my-debug-output.txt file in your board's root directory to see what output was generated.

You would expect the file's contents to be:

The value of $email just before the return in MailHandler::get_from_email() is: "[email protected]".

where, of, course, "[email protected]" is your actual admin email address.

Depending on what you actually saw, you would continue this "output" debugging via a process of narrowing down / elimination by continuing to insert such lines into code to try to figure out why the default email address isn't being set.
I'm going to send you screenshots of the outputs I have on another site that I had not messed with yet so we can start from the beginning.

Screenshots will show details I don't want to share publicly. Smile

(2022-09-19, 11:24 PM)Laird Wrote: [ -> ]
file_put_contents(MYBB_ROOT.'my-debug-output.txt', 'The value of $email just before the return in MailHandler::get_from_email() is: "'.$email.'".', FILE_APPEND);

So this actually showed the proper email address being used for the site in question. Mail delivery failed because it is once again reverting to the cpanel account address and not the one with their domain name in use.

For giggles I did open up webmail and sent a test email from default system account and it sent to my Gmail as the proper email with their domain name, but MyBB using phpMail is sending with the system default (hosts) domain.
Got your PM. Will follow up in turn by PM. We can share an update in this public thread if/when we resolve the bug.