Comments (16)
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.
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.
can I see the solution you built? I ran into this same problem...
from laravel-gmail.
`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.
New to github, don't know why the supplied code spazzes out half way...
from laravel-gmail.
use 3 backtics and end with
from laravel-gmail.
by the way, thank you very much, I really need to get this done tonight, and this was in the way
from laravel-gmail.
No worries, there's way to little info on the google API for PHP anyway.
from laravel-gmail.
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.
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.
in the constructor of mail.php?
from laravel-gmail.
aha! got it! Thanks!
from laravel-gmail.
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.
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.
no worries, you'll do the same for someone else some day :)
from laravel-gmail.
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)
- production.ERROR: refresh token must be passed in or set as part of setAccessToken {"userId":1,"exception":"[object] (LogicException(code: 0): refresh token must be passed in or set as part of setAccessToken at /home/forge/website/vendor/google/apiclient/src/Client.php:332) HOT 2
- Getting error while replying to email HOT 4
- Send Email BCC not working
- Get signature of the user
- Enable function for application wide access
- I have got issue in php version 8.1 Is it available for PHP version 8.1 or laravel10? HOT 2
- Error 400: redirect_uri_mismatch
- refresh token must be passed in or set as part of setaccesstoken HOT 2
- An address can be an instance of Address or a string ("array" given).
- Not supported for Laravel 10 HOT 3
- Multiple user login not working HOT 3
- Hello HOT 1
- Google\\Service\\Exception 401 Request had invalid authentication credentials.
- Can you provide a method for forwarding the email
- Dacastro4\LaravelGmail\Services\Message\Mail::__construct(): Argument #1 ($message) must be of type ?Google_Service_Gmail_Message, Google\Service\Exception given HOT 6
- bootstrap-3.3.7.min.js: 6 vulnerabilities (highest severity is: 6.1)
- jquery-3.1.1.min.js: 4 vulnerabilities (highest severity is: 6.1) - autoclosed HOT 1
- google/apiclient-v2.2.3: 9 vulnerabilities (highest severity is: 9.1)
- orchestra/testbench-v3.5.5: 8 vulnerabilities (highest severity is: 9.8)
- Error when replying an email HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-gmail.