2017-10-05 94 views
0

我的電子郵件正文包含電子郵件和電話號碼字段我想提取這些字段並存儲在數據庫中,我該如何做? 這是我的PHP代碼...如何使用PHP(imap函數)獲取電子郵件正文的特定部分?

<?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); 

?> 

餘米提供我的電子郵件正文的屏幕截圖,郵箱和手機號碼的這一切盈。我想提取它。 enter image description here

回答

1

你可以嘗試

$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]; 
+0

感謝名單@Muhammed Hafil。我得到一個空數組,但是如果我刪除了兩個連字符,那麼我將'Email:'作爲一個數組元素,你可以詳細說明這個模式嗎? –

+0

你可以請發佈什麼是$消息 –

+0

$ message =「商業查詢爲利斯特--------- 買家聯繫信息:aaaaa 手機/手機:+91 ------- - , 電子郵件:無
買家的需求詳細信息: 我們要採購李斯特黃金UV LED超薄POP面板燈 尺寸:15英寸 功率:15瓦 請給我的價格和其他細節 。必填數量: - 「 –

相關問題