MyBB Community Forums

Full Version: Understanding the EMAIL code...need some help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a support question regarding the following code:

	if($mybb->user['uid'] == 0)
	{
		if(!preg_match("/^(.+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/si", $mybb->input['fromemail']))
		{
			error($lang->error_invalidemail);
		}
		if(!$mybb->input['fromname'])
		{
			error($lang->error_incompletefields);
		}
		$from = $mybb->input['fromname'] . " <" . $mybb->input['fromemail'] . ">";
	}
	else
	{
		$from = $mybb->user['username'] . " <" . $mybb->user['email'] . ">";

My question is this. First you will see the 'fromemail' .... If I wanted to dictate the value of that field or find out where it comes from or how it's pulled, how could I go about doing that?.

As well, in the last line it says $from = $mybb->user['username']
My question on that is, is user a table and 'username' is a column or field? I don't quite understand this portion of the code.

Thanks!
Greetings,

I am trying to understaind in php, specifically the string $mybb->input['fromemail'] that is found in members.php that is responsible for sending emails FROM the someone on the forums.

[Image: wiimail_user.jpg]

Attached is what my functionality looks like. As you can see, I am displaying a FROM field because I want the person to visually see that the email is coming from their wiimail address.

Now, in the existing mybb code, it's utilizing the 'fromemail' and 'fromname'. How is this populated? Is there a way to control what is used and displayed in the 'fromemail' ?

Please assist if possible. I am sooooo close to figuring this one out.

Thanks,
Mike
Maven4Champ Wrote:I have a support question regarding the following code:

My question is this. First you will see the 'fromemail' .... If I wanted to dictate the value of that field or find out where it comes from or how it's pulled, how could I go about doing that?.
Anything in $mybb->input comes from the $_GET or $_POST superglobals, so it was from the form that was submitted. $mybb->input['fromemail'] is equivalent to calling $_POST['fromemail'].

fromemail is the name of the input tag like where it says <input type="text" name="fromemail" />, from there, the value of $mybb->input['fromemail'] is populated once the form is submitted.

Maven4Champ Wrote:As well, in the last line it says $from = $mybb->user['username']
My question on that is, is user a table and 'username' is a column or field? I don't quite understand this portion of the code.
Thanks!

It just so happens that $mybb->user contains the information about the current user (the data is from the database table mybb_users). Yes 'username' is a column in that table, and so it is included in the array. You'll find that most, if not all, of the current user's information and settings are stored in $mybb->user. So to find the user ID of the current user you can use $mybb->user['uid'] or to find the email of the current user you can use $mybb->user['email'].
DennisTT Wrote:
Maven4Champ Wrote:I have a support question regarding the following code:

My question is this. First you will see the 'fromemail' .... If I wanted to dictate the value of that field or find out where it comes from or how it's pulled, how could I go about doing that?.
Anything in $mybb->input comes from the $_GET or $_POST superglobals, so it was from the form that was submitted. $mybb->input['fromemail'] is equivalent to calling $_POST['fromemail'].

fromemail is the name of the input tag like where it says <input type="text" name="fromemail" />, from there, the value of $mybb->input['fromemail'] is populated once the form is submitted.

Maven4Champ Wrote:As well, in the last line it says $from = $mybb->user['username']
My question on that is, is user a table and 'username' is a column or field? I don't quite understand this portion of the code.
Thanks!

It just so happens that $mybb->user contains the information about the current user (the data is from the database table mybb_users). Yes 'username' is a column in that table, and so it is included in the array. You'll find that most, if not all, of the current user's information and settings are stored in $mybb->user. So to find the user ID of the current user you can use $mybb->user['uid'] or to find the email of the current user you can use $mybb->user['email'].

Thank DennisTT,

I think I understand how this is working a little better now. As far as populating the fromemail with a field from the userfields table instead of from the user's registered email address, which file(s) do I modify?
In the code you've posted in the first post, you can modify it to suit your purposes

First you'll have to find the profile field ID that you want to get the information from. You can find this in the "ID" column when you edit the Custom Profile Fields in your Admin CP.

Instead of:
$from = $mybb->input['fromname'] . " <" . $mybb->input['fromemail'] . ">";
You can use:
$from = $mybb->input['fromname'] . " <" . $mybb->user['fidX'] . ">";
Replace the X in fidX with the profile ID you found above.

You can do the same modification with the next part:
$from = $mybb->user['username'] . " <" . $mybb->user['email'] . ">"; 
Modify it into:
$from = $mybb->user['username'] . " <" . $mybb->user['fidX'] . ">"; 
Thanks sooo much Dennis.

One final question.

If the field I am wanting to display is located in another table and not the user table, how would I point to that.

For example:
$from = $mybb->user['username'] . " <" . $mybb->user['fidX'] . ">"; 

Changed to:
$from = $mybb->user['username'] . " <" . $mybb->wiimail['wiimail'] . ">"; 

If you notice, I have changed $mybb->user to $mybb->wiimail (is that acceptable) and finally I changed fixX to wiimail as that is the column name I want to display.

Thanks Dennis!
No, that will not work. You will need run a query on the table. Something like this:
$query = $db->simple_select(TABLE_PREFIX."change_to_table_name", "*", "uid='{$mybb->user['uid']}'");
$wiimail = $db->fetch_array($query);

Keep in mind I don't know your database structure so you might have to play around with the where part(ie. "uid='{$mybb->user['uid']}'"). Then use $wiimail['wiimail'] for the email address. Also, for information on the simple_select method, see ths wiki. [Wiki: Simple_Select] (Broken link, head over to docs.mybb.com instead)
OK I think I have this in working order now. What I did was add a column to the users table and called it wiimail and this will be populated upon registration via a TRIGGER.

Now, if I want to actually display the FROM email address in a textbox, what would be the proper syntax in my template.

Here is my php code mod:
// <----- Begin WiiXchange Mod ----->
elseif($mybb->input['action'] == "wiimailuser")
{
	$plugins->run_hooks("member_wiimailuser_start");

	if($mybb->usergroup['cansendemail'] == "no")
	{
		error_no_permission();
	}
	if($mybb->input['uid'])
	{
		$query = $db->simple_select(TABLE_PREFIX."users", "wiimail, hideemail", "uid='".intval($mybb->input['uid'])."'");
		$emailto = $db->fetch_array($query);
		if(!$emailto['wiimail'])
		{
			error($lang->error_invalidpmrecipient);
		}
		if($emailto['hideemail'] != "no")
		{
			error($lang->error_hideemail);
		}
	}
	if($mybb->user['uid'] == 0)
	{
		eval("\$guestfields = \"".$templates->get("member_wiimailuser_guest")."\";");
	}

	$plugins->run_hooks("member_wiimailuser_end");

	eval("\$emailuser = \"".$templates->get("member_wiimailuser")."\";");
	output_page($emailuser);
}
// <----- End WiiXchange Mod ----->
// <----- Begin WiiXchange Mod ----->
elseif($mybb->input['action'] == "do_wiimailuser" && $mybb->request_method == "post")
{
	$plugins->run_hooks("member_do_wiimailuser_start");

	if($mybb->usergroup['cansendemail'] == "no")
	{
		error_no_permission();
	}
	$query = $db->simple_select(TABLE_PREFIX."users", "uid, wiimail, hideemail", "wiimail='".$db->escape_string($mybb->input['touser'])."'");
	$emailto = $db->fetch_array($query);
	if(!$emailto['wiimail'])
	{
		error($lang->error_invalidpmrecipient);
	}
	if($emailto['hideemail'] != "no")
	{
		error($lang->error_hideemail);
	}
	if(!$mybb->input['subject'] || !$mybb->input['message'])
	{
		error($lang->error_incompletefields);
	}
	if($mybb->user['uid'] == 0)
	{
		if(!preg_match("/^(.+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/si", $mybb->input['wiimail']))
		{
			error($lang->error_invalidemail);
		}
		if(!$mybb->input['wiimail'])
		{
			error($lang->error_incompletefields);
		}
		$from = $mybb->input['wiimail'] . " <" . $mybb->input['wiimail'] . ">";
	}
	else
	{
		$from = $mybb->user['wiimail'] . " <" . $mybb->user['username'] . ">";
	}
	my_mail($emailto['wiimail'], $parser->parse_badwords($mybb->input['subject']), $parser->parse_badwords($mybb->input['message']), $from);

	$plugins->run_hooks("member_do_wiimailuser_end");

	redirect("member.php?action=profile&uid=$emailto[uid]", $lang->redirect_emailsent);
}
// <----- End WiiXchange Mod ----->

and here is my member_wiimailuser html template:

<td width="40%" class="trow1"><strong>Wii Mail From Address:</strong></td>
<td width="60%" class="trow1">
<input type="text" class="textbox" size="39" name="wiimail" value="{$from['wiimail']}" style="color: #000000; font-style: font-weight: bold; background-color: #808080" /></td>

Does all look in order? The one thing is that it doesn't populate the from address known as wiimail in the user table when hitting WiiMail in the memberlist and viewing the wiimail user page.

Am I just calling the wrong thing in my template?
BUMP...

I think I have this really close now I just need a little more assistance. Can someone look at the code I posted just above this and see if there is anything wrong why it wouldn't populate the field from the usertable and into the text field on my form in the html template?

Thanks!