Question

Getting message from imap_fetchbody function works absolutely fine & when I print the message, it's printing with no issues. The problem is when I apply any substring function, it displays nothing. Ultimately it seems message is not regarded as a string. Here is my code:

$email=imap_search($inbox, 'SUBJECT "Mail delivery failed"', SE_UID);
foreach($email as $email_number) {

$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);    

$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">subject'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="to">'.$overview[0]->to.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';    

$output.= '<div class="body">'.$message.'</div>';
}
function getBetween($message,$start,$end){
$r = explode($start, $message);
if (isset($r[1])){
    $r = explode($end, $r[1]);
    return $r[0];
}
return '';
}
$start = "The following address(es) failed: ";
$end = " No Such User Here";
$newcontent = getBetween($message,$start,$end);
echo $newcontent;

Any idea how to convert $message into a string so that I can apply the functions? And yes, basically I want to have specific part between two strings from email.

EDIT

If someone is suspected about getting the output, then the result forecho $message is below:

**This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its recipients.
This is a permanent error. The following address(es) failed: xxx@example.com
No Such User Here"...**
Was it helpful?

Solution

Your getBetween function does not do what it should do.

function getBetween($message, $start, $end){
    $temp = substr($message, strpos($message, $start) + strlen($start)); // Get the first part, excluding the start text
    return substr($temp, 0, strpos($temp, $end)); // Remove the last part
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top