trying to get email body text [harder than it seems]

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

trying to get email body text [harder than it seems]

Post by D Oliveira » 2019-10-01 17:14

Good afternoon yall,

the second block of this post is a script working to capture $subject from an email sent to my server email , I was sending a string with the user's email in the subject line to this server email and piping that information to the script. The plan was to disable the user whenever the failed payment email got sent from a wordpress plugin, but this plugin is incapable of adding the user's email in the subject line, it adds to the body of the email, this is what the plugin sends:

Code: Select all

Subscription Cancelled
A subscription belonging to has been cancelled. Their subscription's details are as follows:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Subscription Number: 39010

----------

Subtotal: $0 Total: $0

----------

End of Prepaid Term: 28/09/2019

View Subscription: https://vip.overallteam.com.br/wp-admin/post.php?post=39010&action=edit

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

ENDEREÇO DE COBRANÇA

[email protected]

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

working script with $subject:

ajax-pipe.php

Code: Select all

#!/usr/bin/php -q
<?php 

$fd = fopen("php://stdin", "r");
$email_content = "";
while (!feof($fd)) {
$email_content .= fread($fd, 1024);
}
fclose($fd);

//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $email_content);
 
// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;
 
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
 
// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}


	$currDir = dirname(__FILE__)."/..";
	include("{$currDir}/language.php");
	include("{$currDir}/defaultLang.php");
	include("{$currDir}/lib.php");
	include_once("{$currDir}/header.php");
	 $adminConfig = config('adminConfig');

		
		sql("UPDATE `membership_users` SET `groupID` = '4' WHERE email = '{$subject}'", $eo);

		$sql2 = "SELECT memberID FROM membership_users WHERE email = '{$subject}'";

		$datos2 = sqlValue($sql2);
		

		sql("UPDATE `treinocardio` set activecheck2='0' WHERE  user='{$datos2}'", $eo);
		sql("UPDATE `dietasup` set activecheck2='0' WHERE  user='{$datos2}'", $eo);
		sql("UPDATE `alunos` set active='INATIVO' WHERE  user='{$datos2}'", $eo);
		
        
	
?>


then I tried to use the variables $message and $headers, whenever I tried $message it returns empty value and $headers returns something scrambled without the main body message:

Code: Select all


From [email protected] Mon Sep 30 10:43:23 2019 Received: from mail-oi1-f170.google.com ([209.85.167.170]:40628) by a2plcpnl0513.prod.iad2.secureserver.net with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from <[email protected]>) id 1iEzhe-003ToG-Cx for [email protected]; Mon, 30 Sep 2019 10:43:23 -0700 Received: by mail-oi1-f170.google.com with SMTP id k9so11880448oib.7 for ; Mon, 30 Sep 2019 10:42:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=d6cqq/HGlfjqSts9WSdn1HlBDm5/gdLmsD5sht8BPKg=; b=g2NgcjiAwp2gjcF5cuM2OHC1GlQGG/MWhbpw6Wnft87GPvng/+loMZxYLAziUyKVZz MTUhT5ye7VpPGYtzN8iNv6MD9HxaXAHEwEQSk7jbw0Qw2+61zVn5WqRT0+EF0KM0lBtD mHuHKu06KKfCZa+qBaTB5GN86Xa0b/PVJ2Olr63M3MQyCalNajS9zL7xPcS3wW8wjBAf 7Wn9bdnSlU0XCQGxBN0tHnJjpY6uays33ziYZPSjnmlE79PCjsqnH81EnT7j/f3gRQTX cMXKDg0srNS7ICzOFYGxG6WV9UQUBCwtm9FCELTehCKZqLyxGtgU/RdcVdkSHkEcdXIU Ax2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=d6cqq/HGlfjqSts9WSdn1HlBDm5/gdLmsD5sht8BPKg=; b=nT/X/xDqLch6pxqO5Jwt/dprHpJFoUyozG437A+WMyu10zxbnwlnj/wP+Axt6fX29T vSzMJJ9hgDEFuI4NEWJKzkbHftAIOXwN5KUcjG5Ck5n9E1cigwVqnANrqNK1JNey6jfc FkOJuyIg7zJMRZX3j1P/i4auwT7y/4uYeywY+BvGZ8rqKisN1xed5P6ewbvJhyw3BEAf hnkpFP4hwYPZb1OBcowYvzVb1qfZXEJ24ziA4xWFDfXKxN0Q2cx4ySOKulnSrHxWptZE QkeenEdoykK0ZjXXAWdcXeHJM2TDYMOQsq7s6B4/63L71e4RcegaH6Ko1DQ4qh0NIzfu yrAQ== X-Gm-Message-State: APjAAAU9HPROWmxVlmLDma8KP3sAB5D61sk0/cVkO2xfwHn5PLEhjjRB Nn2lGnTlLq+l7Hs8NBharKJJZ5ydVVWMNWTiqkqGnJrnCVs= X-Google-Smtp-Source: APXvYqzptoHzkItMoB4gk60Fq49UYHAIv5eNeae3nTpDkektpvqQapIiSpDX1hHqpfqQ5tJTVZ2zeRm6M9yocvK1Jw4= X-Received: by 2002:aca:f1a:: with SMTP id 26mr330335oip.136.1569865357031; Mon, 30 Sep 2019 10:42:37 -0700 (PDT) MIME-Version: 1.0 From: David Oliveira <[email protected]> Date: Mon, 30 Sep 2019 13:42:26 -0400 Message-ID: Subject: [email protected] To: [email protected] Content-Type: multipart/alternative; boundary="000000000000f37e910593c8c262" X-Spam-Status: No, score=1.8 X-Spam-Score: 18 X-Spam-Bar: + X-Ham-Report: Spam detection software, running on the system "a2plcpnl0513.prod.iad2.secureserver.net", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see root@localhost for details.




does anyone have any ideas on how to properly get the body text of the email and explode it to fetch just the email? something like:

Code: Select all


        $data1 = explode('COBRANÇA',$message);
        
        
        $vart = strstr($data1[1],"=-",true);    
        
       // if the variable $message was working the output would be just the email: [email protected]


*if you're trying to reproduce this on your server you need a cpanel email, cpanel forwarder set to pipe to a script and change the ajax-pipe.php file permissions to 7 5 5

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: trying to get email body text [harder than it seems]

Post by pbottcher » 2019-10-01 20:58

Hi David,

take a look at the result now. Apparently my mails through the system are not getting send.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: trying to get email body text [harder than it seems]

Post by D Oliveira » 2019-10-01 21:47

pböttcher wrote:
2019-10-01 20:58
Hi David,

take a look at the result now. Apparently my mails through the system are not getting send.
remarkable skills as per usual, i shall share your solution to the community :) ur the man! thanks!

Code: Select all

#!/usr/bin/php -q
<?php 

$fd = fopen("php://stdin", "r");
$email_content = "";
while (!feof($fd)) {
$email_content .= fread($fd, 1024);
}
fclose($fd);

//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $email_content);
 
// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;
$message1 = "";
$check_mail= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
	if ($is_header) {
	// hear information. instead of main message body, all other information are here.
		$headers .= $lines[$i]."\n";
		 
		// Split out the subject portion
		if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
			$subject = $matches[1];
		}
		//Split out the sender information portion
		if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
			$from = $matches[1];
		}
	} 
	else {
	// content/main message body information

	if (strpos($lines[$i],'@') !== FALSE && $check_mail && strpos($lines[$i],'preview') === FALSE) {
		$message1 = $lines[$i];
		$check_mail = false;
	}
		

	$message .= $lines[$i]."\n";
	}
	if (trim($lines[$i])=="") {
	// empty line, header section has ended
		$is_header = false;
	}
}
// teste




	$currDir = dirname(__FILE__)."/..";
	include("{$currDir}/language.php");
	include("{$currDir}/defaultLang.php");
	include("{$currDir}/lib.php");
	include_once("{$currDir}/header.php");
	 $adminConfig = config('adminConfig');
		//$nowtime = time();
		 //sql("INSERT INTO `treinocardio` set user='{$nowtime}', user_name='{$subject}'", $eo);
		//$nowtime = time()+1;
		//sql("INSERT INTO `treinocardio` set user='{$nowtime}',  user_name='{$subject}'", $eo);
		
		
//        $data1 = explode('COBRANÇA',$message);

		$email = trim($message1);
        $vart = strstr($email,"@",true);
		
		sql("UPDATE `membership_users` SET `groupID` = '3' WHERE email = '{$email}'", $eo);

		$sql2 = "SELECT memberID FROM membership_users WHERE email = '{$email}'";

		$datos2 = sqlValue($sql2);
		

		sql("UPDATE `treinocardio` set activecheck2='1' WHERE  user='{$datos2}'", $eo);
		sql("UPDATE `dietasup` set activecheck2='1' WHERE  user='{$datos2}'", $eo);
		sql("UPDATE `alunos` set active='ATIVO' WHERE  user='{$datos2}'", $eo);
		
        
    


	
?>

Post Reply