2017-10-05 95 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瓦 请给我的价格和其他细节 。必填数量: - “ –

相关问题