This topic is locked
[SOLVED]

Need some guidance on extracting an email address from a string.

5/5/2022 8:45:32 PM
PHPRunner General questions
D
DealerModules authorDevClub member

Looking to extract email address from a field that is a string. The field named body contains the body of an email message. I would like to extract the TSMITH@TOMSREPAIR.COM and insert into a field name updated_email_address.
Below is an example string from the body field.

Please fulfill this new order # 100088980 for:

TOM SMITH
TOMS REPAIR CENTER
2051 E MAIN HWY
LARKSVILLE , PA, 19047
United States
T: 215-934-7854
Email: TSMITH@TOMSREPAIR.COM

Order Id:104581

Any direction is greatly appreciated.
I have tried using MySQL regexp without any success.

I have also read in the Email Reader add on:
How can I execute some code when a new email arrives?
Proceed to Editor->Custom files and modify emailreader_custom.php file. You can use this function to send a custom autoreposnder or to parse email and store data in the database.
function processEmail($params) {
// $params["from"] - FROM email address
// $params["subject"] - email subject
// $params["body"] - email body

// your code here

}

Thanks in advance if anyone has some suggestions.

Admin 5/6/2022

The common approach is to use regex (regular expressions) for this kind of tasks. If we Google "regex extract text after word" it will point you to this article on StackOverflow.

Now we can use an online regex tester to find out the correct regex string. We recommend using regex101.com . Paste your text in the TEST STRING window, paste the regex from the article to REGULAR EXPRESSION window, change the prefix to "Email: " and voila, it works.

img alt

Now you can even use their code generator to product the code in PHP. Of course, you need to change the hardcoded value of your email text to a variable. This is pretty much it.

$re = '/(?<=Email: ).*?(?=[\s])/m';
$str = 'TOM SMITH
TOMS REPAIR CENTER
2051 E MAIN HWY
LARKSVILLE , PA, 19047
United States
T: 215-934-7854
Email: TSMITH@TOMSREPAIR.COM

Order Id:104581

Any direction is greatly appreciated.
I have tried using MySQL regexp without any success.

I have also read in the Email Reader add on:
How can I execute some code when a new email arrives?
Proceed to Editor->Custom files and modify emailreader_custom.php file. You can use this function to send a custom autoreposnder or to parse email and store data in the database.';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
D
DealerModules authorDevClub member 5/6/2022

Thanks Admin for the detailed explanation! I appreciate your help.

Paul

Dalkeith 5/9/2022

Hi Paul

Not sure if you are using SQL Server or not but you can generally do this in the database as well. I've had some success where I pull a text body into a text field and then call a user defined function from phprunner pulling the email addresses out.

Here is a blog post I wrote to make sure that I don't forget how to do it which includes the userdefined function.

SQL Azure - TSQL User Defined Funciton to separate multiple emails from nvarchar(max) field.

and here is a link to regex alternate examples for Admin's Response

Email regular expressions some alternatives