Gmail api php reply and include original attachments - php

I am trying to send a "reply" message using Gmails API and PHP. I can send the message, and even upload documents to send with the message, but I can find ZERO articles, docs, zip, nothing on how to reply to a message using Gmails API and including the original documents. If you have any suggestions, please advise. Here is my current code:
$gMail = new \Google_Service_Gmail($googleClient);
$msg = new \Google_Service_Gmail_Message();
$msg->setThreadId($threadId);
$messageObj = $gMail->users_messages->send("me", $msg);
There is a ton of code as well that I have not included because it doesn't need to be. Best case, if anyone could point me to the docs that explain how to attach original files (without having to download them and re-upload them to the email), this would be great.
Thanks.

Related

How can I save an image received from a Webhook? (WhatsApp API)

So, I'm starting to understand a bit more of WhatsApp API and all the messages that my number received are sent to my server via Webhook. The text messages are fine, but I'm struggling with the media messages.
When the user sends an image for example, Facebook's Webhook only sends me the mime_type and the sha256 of the image.
Can anyone please guide me the steps of what I need to do?
Do I need to convert it to base64, and then write the file in my server? Do I need to use a specific function? Do I need to use another programming language that's not PHP?
I'm totally lost on this one.
The way to do this, as pointed out by #CBroe is to use the media endpoints.
Assuming message is the message from the Webhook
// Get the image information, including the download link
$image_url_handle = fopen("https://graph.facebook.com/v13.0/" . $message->id);
$image_url = "";
// Read all of the data before parsing
while (!feof($image_url_handle)) {
$image_url .= fread($image_url_handle);
}
// Close the stream
fclose(image_url_handle);
// Get the url from the image information
$image_url = json_decode($image_url)->url;
// Start a request to the download URL.
$image_stream = fopen($image_url);
Note: There is no error handling or frameworks, though this should work most of the time

What does this email from SendGrid is refering to in PHP API?

a few days a go we received a email from sendgrid stating they are going to change some behaviors in their API, I've done research but I have not found concretly what they are refering to, currently we are using the PHP API, and atleast for the 2) I think we are OK as we use SendGrid classes to build the emails
Check your code that uses the mail send endpoint and ensure that the
“enable” parameter is included under the filter when applicable.
What do you need to do?
To avoid disruption to your mail send service, please be sure to make
the following actions before August 10, 2021
Check your code that uses the mail send endpoint and ensure that the
“enable” parameter is included under the filter when applicable.
Check your mail send header to ensure that you are using just one
X-SMTPAPI header and address header of the same kind. Remove multiple
headers of the same kind, so you have only 1 header of the same kind.
For example, if you are currently using multiple “from” headers, you
should modify your code so that you have a single “from” header.
Check your code to ensure that you are not applying any
personalization block substitution to your custom argument fields.
But what about the third item? we have substitions of following fashion: $mail->personalization[0]->addSubstitution('%url%', $link);
But I havent found anything like a "block sustitution in custom arguments"
And the first item also worries me, I haven't found anything like that neither, so I'm afraid that perhaps there is something that the PHP API does behind the scenes.
This a example of the code we use.
$sendgrid = new SendGrid(env('SENDGRID_APIKEY'));
$from_m = new SendGrid\Email(null, $from);
$to_m = new SendGrid\Email(null, $from);
$content = new SendGrid\Content("text/html", $body);
$mail = new SendGrid\Mail($from_m, '.', $to_m, $content);
$mail->personalization[0]->addBcc($tos);
$mail->personalization[0]->setSubject($subject);
$mail->personalization[0]->addSubstitution('%MemberName%', $name);
$mail->personalization[0]->addSubstitution('%url%', $hash_url);
$mail->setTemplateId($template_id);
$sendgrid->client->mail()->send()->post($mail);
We are using sendgrid/sendgrid: ~6.2
Twilio SendGrid developer evangelist here.
From my reading of your code and the guidelines you've been sent, you should be ok.
You don't appear to be using anything that needs to be enabled via the API, though you can check all the mail_settings and tracking_settings in the API reference here.
As you are using the library and calling the API you don't appear to be sending any extra headers.
You don't seem to be using any custom arguments, so you aren't applying any substitutions to them.
I do recommend that you upgrade your PHP helper library to version 7 from 6. That will ensure that the underlying library is also using the API in the most up to date manner.

Twilio sending message Using twilio library

Sending twilio message from my api returns this error
Twilio sending message The requested resource /2010-04-01/Accounts//Messages.json was not found
this is how I send a message. anyone incountered a problem?
$this->client = new \Services_Twilio($sid, $token);
return $this->client->account->messages->sendMessage($from ,$to, $message);
this is the documentation I followed
https://www.twilio.com/docs/api/rest/sending-sms
How do I create Messages.json
I used this https://github.com/twilio/twilio-php on laravel 4.2
Another Twilio developer evangelist here. Think I might be able to help.
The error message you got was this:
The requested resource /2010-04-01/Accounts//Messages.json was not found
The key point is the URL, particularly the double slash in the middle. That is where your Account Sid should be, thus leading to the 404 error.
In this case, I would double check how you are setting $sid. Make sure it is assigned before you try to create the Twilio client object.
Hi Twilio developer evangelist here.
Sorry to hear you're having trouble with your code.
You seem to not have posted your complete code, so I don't see where you actually require the library.
I have however written an example which worked for me.
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'AC';
$token = '01';
$client = new \Services_Twilio($sid, $token);
$message = $client->account->messages->sendMessage(
'+44', // From a valid Twilio number
'+44', // Text this number
"Hello monkey!"
);
I have removed some sensitive data on the code, but if you replace that with your token and numbers, you should be able to send a text message correctly.
Also, just in case you have the contents of the 'Services' folder elsewhere, make sure your paths are correct.
You have entered messages when it should be sms_messages.
CHANGE
$this->client->account->messages->sendMessage($from ,$to, $message);
INTO
$this->client->account->sms_messages->create($from ,$to, $message);
Also make sure that you are running this code inside a class otherwise you need to remove the $this-> from your code and use a local varible.

Collect data from email message

I want to collect data from email to mysql database using php.
If some one is sent a mail to their mail account to my mail id. I want that mail information to store in my database. for further operation. It is possible in PHP because I saw this feature in one hosting support application Kayako Fusion which developed by PHP.
So plese give some information to do this task.
If you want to parse with PHP the best way is to use a 3rd party API to revive the email and then send it to your PHP script with a HTTP Post.
I'd recommend using either sendgrid.com or mailgun.com
Both of these services will parse the email and send you the information in a http POST that you can then insert into mysql. API docs for both: MailGun Incoming Parse API / SendGrid Incoming Parse API
You can use the imap functions, for example:
<?php
$imap = imap_open("{server.example.com:143}INBOX" , 'login' , 'password');
if( $imap ) {
//Check no.of.msgs
$num = imap_num_msg($imap);
//if there is a message in your inbox
if( $num >0 ) {
//read that mail recently arrived
$the_message = imap_body($imap, $num);
//Do the stuff with $the_message
}
//close the stream
imap_close($imap);
}
You'll have to setup a cronjob (scheduled task on Windows) that connects to the Exchange or POP server, retrieve's all new emails sinds the last run and insert them into the DB.
In case of a POP mail server(I think POP will be easier), there is a comment here with all functions you need right -> here. If that doesn't work, try Googling for "PHP POP wrapper" or something similar.
In case of a Microsoft Exchange server, you'd have to use a PHP Exchange wrapper, found one here for you.
Goodluck!
You can pipe your email to a php script and then extract each part of email and store it in data base.
check this example
when you get content of email you can do what ever you want

Zend Framework: Some email users get errors when trying to open PDF attachments?

I'm having a strange problem and not sure how to troubleshoot it. I have created a script in one of my Zend Framework controllers that allows an administrator to log in, upload a PDF, and send as an attachment to everyone subscribed to the mailing list. The problem is that some users report that they are unable to open the PDF attachment, that the file is corrupt. I think this is only happening to AOL users, but I'm not positive. Have you encountered this problem before? Or maybe it is not a problem with AOL, but something wrong with my code?
Here's the code that does the work:
Also, I'm using ZF version 1.6.0. Not sure if that is relevant.
//assuming the form is valid:
$table = new Subscribers();
$rowset = $table->fetchAll();
foreach ($rowset as $row) {
$mail = new Zend_Mail();
$mail->setBodyText($form->getElement('body')->getValue())
->setFrom('weekly-update#email.com', 'Weekly Update')
->addTo($row->email)
->setSubject($form->getElement('subject')->getValue());
$fileLocation = $form->getElement('attachment')->getValue();
$fileContents = file_get_contents($fileLocation);
$attachment = $mail->createAttachment($fileContents);
$attachment->filename = str_replace(Zend_Registry::get('config')->downloadsLocation . '/', '', $fileLocation);
$mail->send();
}
It appears (to me) that in this line of code:
$attachment = $mail->createAttachment($fileContents);
you likely need to add the additional header information available in the createAttachment method of the Zend_Mail framework::
$attachment = $mail->createAttachment($fileContents,
Zend_Mime::DISPOSITION_INLINE);
Many larger email providers are sticklers for strict adherence to good email policy (I've found).
Play around with this and I'm sure you'll get it to work.
I also had this problem.
I'd suggest you trace out the file stream info somehow. The issue with my app was that the $fileContents = file_get_contents($fileLocation); call wasn't getting the stream of the file properly, so this is where you might be falling down.
Try this:
$mail = new Zend_Mail();
...
var_dump($mail->send());
You should see a bunch of gibberish where the file stream comes out in the var_dump under the key:
["_content:protected"]=>
string(37129) "%PDF-1.5
etc...

Categories