GithubHelp home page GithubHelp logo

getBody on null about laravel-gmail HOT 16 CLOSED

dacastro4 avatar dacastro4 commented on May 17, 2024
getBody on null

from laravel-gmail.

Comments (16)

valentim04 avatar valentim04 commented on May 17, 2024 2

Hi, here is a simple but effective solution:

$gmail = LaravelGmail::message()->preload()->all();

    $messages = [];
    foreach ($gmail as $key) {
             
        if ($key->hasNoParts() == false ){
            $body = $key->getHtmlBody();
        } else {
            $body0 = $key->payload->getBody();
            $body = $key->getDecodedBody($body0->data);
        }

}

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

After a few hours spent building my own solution to icloud mails I have found additional issues that you might want to look into considering this is a work in progress. If linebreaks are included in a mail sent from icloud they are split into one part per linebreak. So if I was to write the following.
Hi
My
Name
Is
Buck
That would translate into 5 separate parts that all need to be accounted for if one wants to extract the full email.

Update, the issues seems to stem from sending emails from a phone in combination with icloud, might use different ASCII for linebreak than a computer does.

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

can I see the solution you built? I ran into this same problem...

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

`public function hasNoParts(){
if (empty($this->payload->getParts())) {
return true;
} else {
return false;
}
}

public function extractFromBody(){
if ($this->hasNoParts()) {
$type = $this->payload->getMimeType();
$body = $this->payload->getBody();
if ($type == 'text/html' || $type == 'text/plain') {
$this->bodyArr[$type] = $this->getDecodedBody($body->getData());
}
if ($body->getAttachmentId()) {
$this->attachmentData[] = array(
'id' => $body->getAttachmentId(),
'fileName' => $part->getFilename(),
'mimeType' => $type
);
}
} else {
$parts = $this->payload->getParts();
foreach ($parts as $part) {
if (empty($part->getParts())) {
$type = $part->getMimeType();
$body = $part->getBody();
if ($type == 'text/html' || $type == 'text/plain') {
if (isset($this->messageBodyArr[$type])) {
$this->messageBodyArr[$type] .= $this->getDecodedBody($body->getData());
} else {
$this->messageBodyArr[$type] = $this->getDecodedBody($body->getData());
}
}

				if ($body->getAttachmentId()) {
					$this->attachmentData[] = array(
						'id' => $body->getAttachmentId(),
						'fileName'  => $part->getFilename(),
						'mimeType' => $type
					);						
				}				
			} else {
				$subParts = $part->getParts();
				$this->traverseData($subParts);
			}
		}
	}		
}

public function traverseData($parts){
	foreach ($parts as $part) {			
		if (empty($part->getParts())) {
			$type = $part->getMimeType();
			$body = $part->getBody();
			if ($type == 'text/html' || $type == 'text/plain') {
				if (isset($this->messageBodyArr[$type])) {
					$this->messageBodyArr[$type] .= $this->getDecodedBody($body->getData());
				} else {
					$this->messageBodyArr[$type] = $this->getDecodedBody($body->getData());
				}						
			}

			if ($body->getAttachmentId()) {
				$this->attachmentData[] = array(
						'id' => $body->getAttachmentId(),
						'fileName'  => $part->getFilename(),
						'mimeType' => $type
				);

			}
		} else {
			$subParts = $part->getParts();
			$this->traverseData($subParts);
		}
	}
}

public function getDecodedBody( $content ) {
$content = str_replace( '_', '/', str_replace( '-', '+', $content ) );
return base64_decode( $content );
}
`
All the functions called that aren't present in the supplied code are google API functions.

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

New to github, don't know why the supplied code spazzes out half way...

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

use 3 backtics and end with

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

by the way, thank you very much, I really need to get this done tonight, and this was in the way

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

No worries, there's way to little info on the google API for PHP anyway.

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

I mean, thanks. There's no errors, but they're all null

Am I using this right? Call extractfrombody? when looping through messages? Looking over it...

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

you need to have the payload as a class variable to be able to extract data on it. I set the payload from the message as such:
$message = $this->service->users_messages->get( 'me', $messageId );
$this->payload = $message->getPayload();

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

in the constructor of mail.php?

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

aha! got it! Thanks!

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

It's already set in the mail constructor of the dacastro4 mail.php class. As long as you instantiate the class it should work. However, I'm not sure it can be called statically like other methods. The class I made is not the same however, so you might run into issues.

from laravel-gmail.

JonahKlimack avatar JonahKlimack commented on May 17, 2024

some small things, but I implemented it and pushed to production, you saved me man, I had a meeting tomorrow and I was going to tell them this part isn't working, thanks.

from laravel-gmail.

buckfuddey avatar buckfuddey commented on May 17, 2024

no worries, you'll do the same for someone else some day :)

from laravel-gmail.

nunomsh avatar nunomsh commented on May 17, 2024

This solution wont work if the body as "deeper parts", and you wont get attachments if the body is null.
One possible solution is to get the payload and parts

In your controller:

public function decodeBody($body) 
	{
		$rawData = $body;
		$sanitizedData = strtr($rawData,'-_', '+/');
		$decodedMessage = base64_decode($sanitizedData);
		if(!$decodedMessage){
			$decodedMessage = FALSE;
		}
		return $decodedMessage;
	}

In the function you want to get the emails. In my case I want to get all the emails and attachments and store then, but for this example just the general idea

$messages = LaravelGmail::message()->in('INBOX')->unread()->preload()->all()->take(50);

foreach ($messages as $val => $mlist) {

	$body = $messages[$val]->payload->getBody();
	$FOUND_BODY = $this->decodeBody($body['data']);
	$message_id = $mlist->id;
    
	if(!$FOUND_BODY) {
		$parts = $messages[$val]->payload->getParts();
		foreach ($parts  as $part) {
			if($part['body'] && $part['mimeType'] == 'text/html') {
				$FOUND_BODY = $this->decodeBody($part['body']->data);
				break;
			}
		}
	} if(!$FOUND_BODY) {
		foreach ($parts  as $part) {

			if($part['parts'] && !$FOUND_BODY) {
				foreach ($part['parts'] as $p) {
					if($p['mimeType'] === 'text/html' && $p['body']) {
						$FOUND_BODY = $this->decodeBody($p['body']->data);
						break;
					}
				}
			}
			if($FOUND_BODY) {
				break;
			}
		}
	}
  
	
	if ($messages[$val]->hasAttachments()==1) {

		$anexos = $messages[$val]->getAttachments();
		foreach ($anexos as $key => $anexo) {
			$file = $anexos[$key]->getData();
			$fileName= $anexo->filename;
			$anexo->saveAttachmentTo('anexos/', $messages[$val]->getId().'_'.$anexo->getFileName(), 'public');
		}
	}
	
	$assoc = Gmail::updateOrCreate(
		['google_id' =>  $messages[$val]->getId()],
		['name' => $messages[$val]->getFromName(), 
		'subject' => $messages[$val]->getSubject(),
		'body' => $FOUND_BODY,
		'email_date' => $messages[$val]->getDate(),
		'has_attach' =>$messages[$val]->hasAttachments()
	]);
	$assoc->save();
}

from laravel-gmail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.