Trying to get O365 workaround working but don't know php

This plugin allows you to report an issue in MantisBT by sending an email to a particular mail account

Moderators: Developer, Contributor

Post Reply
mushu
Posts: 349
Joined: 04 Jan 2017, 17:41

Trying to get O365 workaround working but don't know php

Post by mushu »

Windows, Mantis 2.25.2, EmailReporting 0.10.1

So I'm trying to trick the bug_report_mail.php script into thinking it is connecting to a mail server and reading a new message from the inbox. I commented out the code in private function process_pop3_mailbox() and added the following code instead:

Code: Select all

echo "** \$argv[1] = $argv[1]\r\n";
$t_msg = file_get_contents($argv[1],FALSE); # slurp in entire file passed on command line to this script
$t_emailresult = $this->process_single_email( $t_msg );	# changed param from $t_Msg to $t_msg
And then in private function process_single_email( $p_i, $p_overwrite_project_id = FALSE ) I commented out the top section of code and added this:

Code: Select all

$this->show_memory_usage( 'Single email retrieved from mailbox' );
$t_email = $this->parse_content( $t_msg );
unset( $t_msg );
$this->show_memory_usage( 'Parsed single email' );
And left the rest of the code from // Only continue if we have a valid Reporter to work with on in the function as it was.

Then to test this I go into the Scripts directory and call it manually, with the complete text of an email message in a text file:

Code: Select all

php bug_report_mail.php lastEmail-0.txt
But of course it doesn't work. I get the error:

Code: Select all

SYSTEM WARNING: 'file_get_contents(): Filename cannot be empty' in 'C:\Mantis\plugins\EmailReporting\core\mail_api.php' line 336
The argv[1] variable is empty at that point, and I don't know why. Any ideas?
SL-Gundam
Posts: 722
Joined: 06 Jul 2011, 14:17

Re: Trying to get O365 workaround working but don't know php

Post by SL-Gundam »

In process_single_email you should not comment out anything. except make changes to

Code: Select all

$t_msg = $this->getMsg( $p_i );
Make it work by filling the necessary variables as needed.
Make it skip the process_pop3_mailbox function.

Maybe even easiest is to replace the function call for process_pop3_mailbox with process_single_email. The call for process_single_email should contain the path/filename that you want to process. The line earlier with the call for getMsg should be replaced with file_get_contents().

It's better to modify as little as possible and make it work with that.
mushu
Posts: 349
Joined: 04 Jan 2017, 17:41

Re: Trying to get O365 workaround working but don't know php

Post by mushu »

Thanks so much! Will do this this morning and post the results...or the errors lol.

EDIT: however, do you know why the command-line parameter $argv[1] is blank? it is there up higher in the script, so something in PHP or the script code is clearing that out. I don't know PHP to know what to look for.
NEVER MIND FIGURED IT OUT...........................

EDIT2: the problem is that the original code uses msg_id which I don't have since I'm passing in the entire email itself, headers and all, including mime attachments if any, and I don't know what to do about assigning the msg_id it wants.
mushu
Posts: 349
Joined: 04 Jan 2017, 17:41

Re: Trying to get O365 workaround working but don't know php

Post by mushu »

So, my problem with losing the command-line parameter is stupid and I'm blind. I am calling the bug_report_mail.php script but EDITING THE CODE in mail_api.php which means that the script I'm calling isn't passing the arguments on to the one I'm editing. Duh.

One problem solved...

EDIT: so in bug_report_mail.php at the very top I added this line of code:

Code: Select all

file_put_contents("EmailFileName.txt", $argv[1]); # save argument to known filename in current directory
And then in mail_api.php I added this line of code at the top of process_pop3_mailbox (haven't yet made your suggested code changes yet):

Code: Select all

$_argv1 = file_get_contents("EmailFileName.txt",FALSE); # grab filename of email message to be slurped in
AND it now runs without any errors! Don't' know if it is creating tickets etc etc etc but getting closer!
mushu
Posts: 349
Joined: 04 Jan 2017, 17:41

Re: Trying to get O365 workaround working but don't know php

Post by mushu »

I believe I've got it all working! Code changes I made ended up being simple:

In mail_api.php

Code: Select all

 	private function process_pop3_mailbox()
	{
	# added these following three lines of code
	$_argv1 = file_get_contents( "EmailFileName.txt", FALSE ); # grab filename of email message text
	$t_msg = file_get_contents( $_argv1, FALSE ); # slurp in entire file passed on command line
	$t_emailresult = $this->process_single_email( $t_msg );	# changed param from $t_Msg to $t_msg
# commented out the rest of function code...
/*
		$this->_mailserver = new Net_POP3();
...
And also...

Code: Select all

	private function process_single_email( $p_i, $p_overwrite_project_id = FALSE )
	{
		$this->show_memory_usage( 'Start process single email' ); # keep this line of code
		$t_msg = $p_i; # added this line of code
# 		$t_msg = $this->getMsg( $p_i );  # commented this out and replaced with line above
# rest of code is left alone...
		if ( empty( $t_msg ) )
...
In bug_report_mail.php

Code: Select all

# added one line of code at very top of script:
	file_put_contents("EmailFileName.txt", $argv[1]); # save argument to known filename in current directory
Now when the scheduled task gets called it runs a separate console app that does the OAuth2 login to our email account, grabs the inbox messages, writes them to separate sequential text files, and exits. Then it launches php passing it bug_report_mail.php and FileNameWithEmailMessageText.txt in a loop, so every email will be processed by the bug_report_mail script.

Of course all of this wouldn't be necessary if we could get the OAuth authentication method for email added to both Mantis and the EmailReporting plugin...I'm just happy that now we don't need to switch ticketing systems right away! :-)
SL-Gundam
Posts: 722
Joined: 06 Jul 2011, 14:17

Re: Trying to get O365 workaround working but don't know php

Post by SL-Gundam »

Good to hear it works.
Nice job.
Post Reply