Can't get message body of certain emails from inbox using the Zend framework?

Asked
Active3 hr before
Viewed126 times

6 Answers

usingmessage
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,You need to check if the mail is multipart message first, before looping over the parts. If it's not multipart, then the body will be exposed via $mail->getContent() instead of a part. Basic example:,I am trying to read through an email inbox for my application - I'm using the zend framework here. The problem is that I'm unable to retrieve the message body for certain emails. The following is my code as to how I'm doing this:,The problem is that randomly some messages don't return even a textbody or an html body. Even though if I check using a webvmail client those emails have a message body as well. WHat am I missing here?

You need to check if the mail is multipart message first, before looping over the parts. If it's not multipart, then the body will be exposed via $mail->getContent() instead of a part. Basic example:

if ($mail - > isMultiPart()) {
   // Multipart messages handled here
   foreach() {
      // loop over parts
   }
} else {
   // Non multipart messages handled here

   // default text_body
   $one_message - > text_body = $mail - > getContent();

   if (strtok($msg - > contentType, ';') == 'text/html') {
      // HTML email with no plain text alternative, set both values
      $one_message - > body = $mail - > getContent();
      $one_message - > text_body = strip_tags($mail - > getContent());
   }
}
88%

I am trying to read through an email inbox for my application - I'm using the zend framework here. The problem is that I'm unable to retrieve the message body for certain emails. The following is my code as to how I'm doing this:,You should therefore run composer require zendframework/zend-ldap from the base-directory of your project and you should be up and running. By now you get that as a suggestions after the installation.,And that does give me what I'm looking for (169.254.2.2 on the PC I'm using at the moment).,The problem is that randomly some messages don't return even a textbody or an html body. Even though if I check using a webvmail client those emails have a message body as well. WHat am I missing here?

I am trying to read through an email inbox for my application - I'm using the zend framework here. The problem is that I'm unable to retrieve the message body for certain emails. The following is my code as to how I'm doing this:

$mail = new Zend_Mail_Storage_Imap($mail_options);

$all_messages = array();

$page = isset($_GET['page'])?$_GET['page']:1;
$limit = isset($_GET['limit'])?$_GET['limit']:20;

$offset = (($page-1)*$limit)+1;

$end = ($page*$limit)>$c?$c:($page*$limit);
for ($i=$offset;$i<=$end;$i++){

    $h2t = new html2text();
    $h2t->set_allowed_tags('<a>');

    if(!$mail[$i])
        break;
    else{
        $one_message = $mail->getMessage($i);
        $one_message->id = $i;
        $one_message->UID = $mail->getUniqueId($i);

        $one_message->parts = array();
        $one_message->body = '';
        $count = 1;
        foreach (new RecursiveIteratorIterator($mail->getMessage($i)) as $ii=>$part) {

            try {
                $tpart = $part;
                //$tpart->_content = '';
                $one_message->parts[$count] =  $tpart;
                $count++;
                // check for html body
                if (strtok($part->contentType, ';') == 'text/html') {
                    $b = $part->getContent();

                    if($part->contentTransferEncoding == 'quoted-printable')
                        $b = quoted_printable_decode($b);

                    $one_message->html_body = $b;
                    $h2t->set_html($b);
                    $one_message->body = $h2t->get_text();
                }

                //check for text body

                if (strtok($part->contentType, ';') == 'text/plain') {
                    $b = $part->getContent();

                    if($part->contentTransferEncoding == 'quoted-printable')
                        $b = quoted_printable_decode($b);

                    $one_message->text_body = $b;

                    $one_message->body = $b;//$part->getContent();
                }

            } catch (Zend_Mail_Exception $e) {
                // ignore
            }

        }

        $all_messages[] = $one_message;

    }
}
load more v
72%

If you don't have a multipart message, fetch the content via getContent(). Unlike headers, the content is only fetched when needed (aka late-fetch).,fetch the message content,fetch individual parts of multipart messages,To remove a mail, use the method removeMessage(), or rely on array access and use unset():

Basic POP3 example

use Zend\ Mail\ Storage\ Pop3;

$mail = new Pop3([
   'host' => 'localhost',
   'user' => 'test',
   'password' => 'test',
]);

echo $mail - > countMessages().
" messages found\n";
foreach($mail as $message) {
   printf("Mail from '%s': %s\n", $message - > from, $message - > subject);
}
load more v
65%

Next, open Rollbar to see what these errors look like in your account’s item page. The error we just generated should be called "E_Warning: Creating default object from empty value.",If you haven’t already, sign up for a 14-day free trial of Rollbar and let us help you take control of the errors in your Zend application., Engineering Management ,"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

The easiest and most basic way to handle errors is using try, catch, and finally statements. When an error is thrown in the try block, the catch block executes and handles the error. For example, you may want to record the error or present a more friendly error message to the user.

try {
   $value = 5 / 0;
} catch (\Exception $e) {
   echo $e - > getMessage();
}
load more v
75%

If it happens that you are using mentioned version of Zend Framework, PHPMailer or SwiftMailer then you are vulnerable to an attack once malicious crafted input is passed to setFrom() function. In short, if someone crafts an email address like this : “Haxor ” -Param1 -Param2″@test.com ( injecting an extra sequence of ” after the first argument ) it will cause Sendmail to execute with named parameters., If you are an E-commerce development company and you are interested to provide your services under SCANDIWEB brand name you are welcome to submit your application at [email protected] to apply for cooperation with us.,Following are the vulnerable versions of each component.,While none of Magentos are built on Zend Framework they are still utilizing different parts of ZF1 and ZF2 and by no surprise, both are using zend-mail to send emails.

Here is an actual PHP code example which would write message body into file /var/www/cache/phpcode.php:

$email_from = '"attacker" -oQ/tmp/ -X/var/www/cache/phpcode.php  some"@email.com';
$msg_body = "<?php phpinfo(); ?>";
40%

If you receive the following error message, you'll need to configure a default FROM email address (covered in the chapter on CiviMail configuration).,Specify the mail server, username, and password you used when creating the account.,The email should contain headers that resemble the following.,If successful, you will see the message

Sorry.A non - recoverable error has occurred.
The site administrator needs to enter a valid 'FROM Email Address' in
   Administer - > Configure - > Domain Information.
The email address used may need to be a valid mail account with your email service provider.
load more v

Other "using-message" queries related to "Can't get message body of certain emails from inbox using the Zend framework?"