如何使用PHP(imap函数)获取电子邮件正文的特定部分?

时间:2017-10-05 04:10:13

标签: php email gmail gmail-imap

我的电子邮件正文包含我想要提取字段并存储在数据库中的电子邮件和电话号码字段我该怎么办? 这是我的PHP代码......

<?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 个答案:

答案 0 :(得分: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];