Tushar Rmesh Saindane
Tushar Rmesh Saindane

Reputation: 350

How to Fetch Particular part of email body using PHP(imap function)?

My email body contains the Email and Phone Number fields I want to extract that fields and store in the database how can I do that? This is my PHP Code...

<?php

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = "myusername";
$password = "mypassword";

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());


$emails = imap_search($inbox,"ALL");

if($emails) {
$output = '';

foreach($emails as $email_number) {

$headerInfo = imap_headerinfo($inbox,$email_number);
$structure = imap_fetchstructure($inbox, $email_number);

$overview = imap_fetch_overview($inbox,$email_number,0);

$message = imap_qprint(imap_fetchbody($inbox,$email_number,1));


$output .= 'Body: '.$message.'<br />';
$structure = imap_fetchstructure($inbox, $email_number);
print_r($structure);
}
}
imap_close($inbox);

?>

I m providing the screen shot of the my Email Body, In This whatever infront of Email and Mobile Number. I want to extract that. enter image description here

Upvotes: 0

Views: 1120

Answers (1)

Muhammed Hafil
Muhammed Hafil

Reputation: 175

You could try

    $message = "Business Enquiry for Lister --------- Buyer Contact I
nformation:aaaaa Mobile/ Cell Phone: +91--------, Email: Not Available<br> 
Buyer's ment Details: We want to buy Lister Gold UV LED Slim POP 
Panel Lights. Size: 15 Inches Power: 15 Watts Kindly send
me price and other details. d Qty : -";


preg_match('# Mobile/ Cell Phone: (.*?), #', $message, $match);// checks for string between "Mobile/ Cell Phone: " and ","
echo $mobile = $match[1];

preg_match('/Email:(.*?)<br> /', $message, $match);//// checks for string between "Email:" and "<br>"
echo $email = $match[1];

Upvotes: 1

Related Questions