SendGrid Web API bounced email header response - php

I am sending emails via SendGrid with this inside the x-smtpapi header
$json_string = array(
'unique_args' => array (
'email_id' => 1
)
);
It all seems to send okay, inside SendGrid I can view the "email_id" in the Email activity under Unique Args.
However when I try to use the API to look at this email, I cannot find a way to actually get these unique arguments from the API.
I am using this to try and get the headers returned with the bounced emails.
$request = 'https://api.sendgrid.com/api/bounces.get.json&api_user=username&api_key=password'
All I get is just the email addresses that have bounced, not the header information (the unique arguments)
I want to know, is it possible to get the unique arguments from the API. I have read it multiple times to no avail.
I hope this makes sense.
Thanks

At present there's no way request to lookup specific events by unique_arg with the Web API.
However, the SendGrid Event Webhook will give you granular data on each event, such as a bounce as it happens. The Event Webhook POSTs data to your server every time an action is taken upon an email (e.g. open, click, bounce).
Once you receive it, you're responsible for storing this, although it's not a typical API it gives very specific data on events which you can then compile and rehash however you like.
To get started using the webhook, you'll do something like the following, and have SendGrid POST to the following script:
<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
// Here, you now have each event and can process them how you like
process_event($event);
}
[Taken from the SendGrid Webhook Code Example]

Related

DocuSign API: sending a signature request via e-mail with the Envelope ID

I am trying to do the following with the DocuSign API, but I am not getting very much wiser from their documentation.
The admin creates the envelope in DocuSign just as they normally would, but without sending it
They take the Envelope ID and enter it in our software
We show a button to the end user that sends out the Envelope signature request when clicked (based on the Envelope ID)
The closest I came to finding something like this was https://developers.docusign.com/docs/esign-rest-api/how-to/request-signature-email-remote/ but that doesn't seem to allow me to use and existing envelope.
The API reference doesn't seem to offer any help either (https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopes/)
What I have got so far is the following:
OAuth + generating the JWT + access token (works fine)
generating Recipient View (which is not what I need but needs to be replaced with the right call
$view_request = new \DocuSign\eSign\Model\RecipientViewRequest(['return_url' => $args['ds_return_url']]);
if ($args['starting_view'] == "envelope" && $args['envelope_id']) {
$view_request->setEnvelopeId($args['envelope_id']);
}
# Call the API method
$config = new \DocuSign\eSign\Configuration();
$config->setHost($args['base_uri']);
$config->addDefaultHeader('Authorization', 'Bearer ' . $args['ds_access_token']);
$api_client = new \DocuSign\eSign\Client\ApiClient($config);
$envelope_api = new \DocuSign\eSign\Api\EnvelopesApi($api_client);
$results = $envelope_api->createRecipientView($args['account_id'], $view_request);
$view_url = $results['url'];
return $view_url;
Thanks!
So based on your description it looks like for the first step, you're looking to create an envelope as a draft. Which is basically creating an envelope, filling all the information out, and then not sending it.
This will spit out an envelope Id which you can store in your application.
And when the button you describe is clicked, you can update the status of the envelope to "sent" using this endpoint which will send out the envelope.
If you're looking for something more detailed, you can always reach out to us at DocuSign Developer Support and we can discuss it further.

GMail API - Getting certain message headers or fields

I have successfully connected GMail API via G Suite account and service account. I can get a message list and I can retrieve messages by IDs. I'm working with PHP.
What I'm having problems with is to get for example the FROM or TO headers, SUBJECT or the snippet field.
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from'], 'fields'=>['snippet','labelIds']);
$fullMessage = $service->users_messages->get($user, $id, $optParam);
This will return the snippet, but not the subject or from or the labelIds.
If I use the GMail "Try this API" and use the id of the message and use "snippet" in the "fields" entry, I just get the snippet back as:
{
"snippet": "Short snippet of the message"
}
If I use:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from','to']);
I do get the 3 headers, but I also get a lot more information, including the labels and snippet - about 3K for each message.
I just can't seem to be able to specify a small subset of the data. All I need is to show messages as a list with the subject, date/time, from/to.
I don't care so much about the amount of data, but it takes on average about 3.5 seconds to retrieve the data for just 14 message!
Is there a way to restrict this so I don't get all the "extra" data or speed the retrieval up somehow?
Sending the request would involve specifying the metadata keys as well as the parameter names of the fields you want to obtain. You can use an HTTP GET request with the URI to get the ‘to’, ‘from’, ‘subject’ and ‘snippet’ with https://www.googleapis.com/gmail/v1/users/me/messages/<MESSAGE_ID>?format=metadata&metadataHeaders=to&metadataHeaders=from&metadataHeaders=subject&fields=snippet%2C+payload%2Fheaders, which will also limit the headers you obtain.
In PHP you can use this:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject', 'from', 'to'], 'fields'=>'payload/headers,snippet');
Note that the fields parameter needs to be sent as a string and not an array.
Also be aware there is a known issue with the Gmail API where using the https://www.googleapis.com/auth/gmail.metadata scope will not return the snippet.
You’ll need to use https://www.googleapis.com/auth/gmail.readonly instead.
You can also make a batch of requests in one network call which will help speed up overall execution time as documented here.

How to set Unique open tracking in sendgrid with php

The following code I am using to send mails from php.
<?php
require 'vendor/autoload.php';
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test#example.com", "Example User");
$email->setOpenTracking(true, "--sub--");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$response = $sendgrid->send($email);
?>
Here I am setting Open Tracking for web-hook, When an email is opened the open event trigger and response coming to my given URL. Here I am getting web-hook every time when ever that email is opened by client. But I want Unique Open tracking.
I tried this which not working and may be invalid method:
$email->setUniqueOpenTracking(true, "--sub--");
Is there any setting or parameters That I need to configure.
Thanks in Advance
After call conversation with send grid, they are not providing Unique open tracking.
I have to maintain the opened web-hooks in a table and check every time, whether the web-hook is already received or not.

stripe webhook account.updated is not working

I am trying to send an (simple test) email when I receive an account.updated call for a custom account from Stripe API. My other webhooks to create charges and inform customers about successful or failed charges work like this, but here I get an error 500 (I can see that in the dashboard of the custom account) and the mail is NOT send, so I am not sure what I am doing wrong here. My code looks like this:
<?php
require_once('vendor/autoload.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_XXXX");
// Retrieve the request's body and parse it as JSON
$input = #file_get_contents("php://input");
$event_json = json_decode($input);
// Verify the event by fetching it from Stripe
$event = \Stripe\Event::retrieve($event_json->id);
// Do something with $event
if ($event->type == 'account.updated') {
// The E-Mail message to send to inform about a succeeded charge
$message = 'test';
// Send the E-Mail
mail('test#example.com', 'We need more info about you!', $message);
}
http_response_code(200); // PHP 5.4 or greater
Thank you for your help!
If you look at your web server's error.log (usually accessible from your hosting control panel or in /var/log/) do you see more detail on what's causing the 500?
Could it be $event = \Stripe\Event::retrieve($event_json->id); failing?
If the event is occurring directly on a connected account, you may also need to pass the account id to retrieve it.
See here for a bit more context,
https://stripe.com/docs/connect/webhooks
The code would be more like:
$event = \Stripe\Event::retrieve(
array("id" => $event_json->id),
array("stripe_account" => $event_json->account));
https://stripe.com/docs/connect/authentication#authentication-via-the-stripe-account-header

Sendgrid : get bounce status code with webhooks (php api)

I use Sendgrid to send emails. Emails are sent using their web API and I use an endpoint to get each email events.
Here is the code of this endpoint (that Sendgrid call each time when an event occur, after sending an email).
I would like to get the status code of each event (eg : 5.1.1 for a bounce).
Here is what I tried so far :
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
$sg_message_id = $event['sg_message_id']; //OKAY
$event = $event['event']; //OKAY (eg : "bounce")
$status = $event['status']; //NOT OKAY ("undefined index")
//and if I try :
$status = $event['event']['status']; //I get the first letter of the event (eg : "b" for bounce)
}
the documentation (here : https://sendgrid.com/docs/API_Reference/Webhooks/event.html, Bounce part) says there is a field "status" (eg : 5.1.1), and I don't understand why it doesn't work,
Any idea?
I understood the problem :
I tried to simulate a bounce error using a fake user email like
user#gmail1.com
but Sendgrid didn't process the email (host not reacheable) and returned a "dropped" event.
Instead, if I send an email to:
another-user-unknow#gmail.com
then sendgrid return a process event and then a bounce event, with the bounce status code:)

Categories