MyBB Community Forums

Full Version: Send email notification about new thread
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying to make a simple plugin that sends email notification about a new thread to a specified email address. I can get the email sent, but have problems with including the thread URL to the email.

Here's what I have so far

$plugins->add_hook('datahandler_post_insert_thread', 'emailonnewpost_send_thread');

function emailonnewpost_send_thread(&$data)
{	

	global $mybb;
	
	$username = $mybb->user['username'];
	
	if ($username != "Alex") {

		$thread_url = ??;

		$to      = '[email protected]';
		$subject = 'New Thread in My Forum';
		$message = 'There was a new thread in My Forum: '.$thread_url.'';
		$headers = 'From: [email protected]' . "\r\n" .
		'Reply-To: [email protected]' . "\r\n" .
		'X-Mailer: PHP/' . phpversion();

		mail($to, $subject, $message, $headers);
		
	}
	
}


So how do I capture the thread URL?
Unless it is a draft, the tid is not available at that hook. You will need to use the newthread_end hook instead.
Thanks for your reply. I tried with the following code:

$plugins->add_hook('newthread_end', 'emailonnewpost_send_thread');

function emailonnewpost_send_thread(&$thread)
{    

    global $mybb,$thread;
    
    $username = $mybb->user['username'];
    
    if ($username != "Alex") {

        $tid = $thread['tid'];

        $to      = '[email protected]';
        $subject = 'New Thread in My Forum';
        $message = 'There was a new thread in My Forum with ID '.$tid.'';
        $headers = 'From: [email protected]' . "\r\n" .
        'Reply-To: [email protected]' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);
        
    }
    
} 

The email is being sent with newthread_end hook, but there's still no TID in the email. Any suggestions?
It is $thread['tid']
(2014-03-07, 02:40 AM)Omar G. Wrote: [ -> ]It is $thread['tid']

That's what I have in the code in post #3, no?
My bad, newthread_end is for the newthread page, you need to use newthread_do_newthread_end, the global variable being $tid for the thread ID.
Thanks a lot Omar, that worked. Rep added!
Can the plugin you created be downloaded? I'm looking for something that does exactly that!