Issue processing incoming emails in mailgun - php

I want to process the json (recieved through post request from mailgun of incoming email).
I'm using codeigniter.
Following is a portion the json that I'm dealing with.
},
"log-level": "info",
"timestamp": 1475215276.814206,
"message": {
"headers": {
"to": "helpdesk#example.com",
"message-id": "CALv887xt-=_ijgb1iC0BQcOwJXguOWbiF7j+Licx6kqperh5Zg#mail.gmail.com",
"from": "User Test <user.test#gmail.com>",
"subject": "example email for testing"
},
"attachments": [],
"recipients": [
"53954680dacd5416eedb3f8639ece5f9#email.example.com"
],
I do not have any problem accessing the from value $this->input->post('from') and subject value $this->input->post('subject').
However I'm trying to access the to and recipients with the following code, but all I'm getting is null values.
$to = $this->input->post('to');
$message_header = $this->input->post('message');
$recipients = $message_header->recipients;
$email = $recipients[0];
$email_q = filter_var($email, FILTER_SANITIZE_EMAIL);
Any help would be great.

Got the to address in the following way.
In Codeigniter:
$this->input->post('recipient');
In PHP:
$_POST['recipient'];

Related

How can I serialize WhatsApp message payload to fetch the message body, phone_number_id, contact in Laravel php

{
"field": "messages",
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "16505551111",
"phone_number_id": "123456123"
},
"contacts": [
{
"profile": {
"name": "test user name"
},
"wa_id": "16315551181"
}
],
"messages": [
{
"from": "16315551181",
"id": "ABGGFlA5Fpa",
"timestamp": "1504902988",
"type": "text",
"text": {
"body": "this is a text message"
}
}
]
}
}
The easiest way is to save the response to a variable and get the data from the array as follows
$message = $responce['value']['message'];
$phone_number = $responce['value']['metadata']['phone_number_id'];
$contact = $responce['value']['contacts'];
You can also decode the response first and then you can easily do the following
$res = json_decode($responce);
$message = $res->value->message;
$phone_number= $res->value->metadata->phone_number_id;
$contact = $res->value->contacts;
But if you want to convert the JSON you have to a Laravel object I recommend checking out the following answer.

PHP JSON Decoding String Issue/ Facebook messenger

I have a JSON that looks like this, and I used json_decode(file_get_contents('php://input'),true); to turn it into an array
{
"object": "page",
"entry": [
{
"id": "",
"time":,
"messaging": [
{
"sender": {
"id": ""
},
"recipient": {
"id": ""
},
"timestamp":,
"message": {
"mid": "",
"seq": "",
"text": "STORE, POSTAL CODE"
}
}
]
}
]
}
After the JSON was decoded I wanted to grab the text portion of the JSON, so i did
$message = ($post['entry'][0]['messaging'][0]['message']['text']
$query = explode(',', $message);
I then used $query[0], and $query[1] in some SQL queries.
For some reason, when I hardcode $message to some text like $message = 'store, postal'; it'll work but not when I use explode.
FYI, this json is coming from Facebook as I'm using the messenger api.
Check that $post['entry'][0]['messaging'][0]['message']['text']
gives you "STORE, POSTAL CODE".
As I see json contains uppercase strings and your example is lowercase. Maybe this is important in your code further.

DreamFactory problems sending email

Okay so I setup my own locally running copy of DreamFactory ... all this works on the hosted version ... but my problem when i try to send a test email using say this:
{
"to": [
{
"name": "Demo",
"email": "demo#acme.com"
}
],
"subject": "Testing SMTP Email Service",
"body_text": "I am sending a Test email from my DSP."
}
I get this response:
{
"error": [
{
"context": null,
"message": "Failed to send to the following addresses:Array\n(\n [0] => demo#acme.com\n)\n",
"code": 500
}
]
}
Seems to me it's trying to use all of that for the email address instead of the address itself and I can not figure out why.
Please try upgrading to the newest version of the DSP (v. 1.7.8)
I was able to use this json on my localhost and all was okay:
{
"to": [{
"name": "Demo",
"email": "demo#acme.com"
}],
"subject": "Testing SMTP Email Service",
"body_text": "I am sending a Test email from my DSP."
}
If it gives another issue after you upgrade, please let me know.
Thanks,
Mark

Mandrill only recognize a few of my merge tags?

I am using the Mandrill PHP-API Class to send bulk mail to my clients. And was wondering why dose Mandrill only recognize a few of my 'recipient_metadata' Merge Tags?
The Mandrill Api Request is :
{
"message": {
"from_email": "me#example.com",
"from_name": "Bengwela Stream",
"headers": {
"Reply-To": "me#example.com"
},
"important": false,
"track_opens": true,
"track_clicks": true,
"auto_text": null,
"auto_html": null,
"inline_css": null,
"url_strip_qs": null,
"preserve_recipients": false,
"tracking_domain": null,
"signing_domain": null,
"tags": [
"CODE"
],
"merge": true,
"metadata": {
"sendtype": "demorun"
},
"to": [
{
"email": "to#example.com",
"name": "Bob "
}
],
"subject": "CODE L1",
"recipient_metadata": [
{
"rcpt": "bob#example.com",
"vars": [
{
"name": "A",
"content": 3
},
{
"name": "email",
"content": "bob#example.com"
},
{
"name": "invoice",
"content": "123456"
},
{
"name": "SITE",
"content": "http://example.com"
}
]
}
],
"html": " <!DOCTYPE html PUBLIC \" ....Complex.... *|A|* *|email|* *|invoice|* *|SITE|* ",
"text": " *|A|* *|email|* *|invoice|* *|SITE|* ",
},
"async": true,
"ip_pool": null,
"send_at": null,
"key": "123456456645"
}
But in both any TEXT and EMAIL Live send, only |A| |email| are replaced.
I'm most probably messed some thing up here. But I can't see what. :)
Thank you for any help.
To populate merge tags (the *|SOMETHING|*, you actually want to use the merge_vars and global_merge_vars parameters instead of the metadata parameters. Metadata allows you to add information to the message, but it's only for your tracking/analytics (more on metadata here). Nothing from the metadata parameters is used to populate merge tags or content in the message. The *|EMAIL|* merge tag is special - if you don't provide a value, we'll automatically fill in the recipient's email address by default.
So, in your API call, you should just be able to replace recipient_metadata with merge_vars and have those merge tags get populated as expected.
For more on working with merge tags, check out the KB article here: http://help.mandrill.com/entries/21678522
And if you're still running in to issues, you can contact support to look at the actual API call(s) you're making. Just click the 'Help' link in the footer when logged in to your account.

How do I fetch a contact's email address?

I am using the contact example from the Google OAuth API. I am not getting the contact's email address. I need contact's email address for sending an invitation request. What am I doing wrong?
You need to request access to the scope: https://www.googleapis.com/auth/userinfo.email. Once you have permission, then you send an authorized request to: https://www.googleapis.com/oauth2/v2/userinfo. You will receive back a response with the email address:
{
"email": "<myname>#gmail.com",
"verified_email": true
}
NOTE: If you want to access the rest of the profile, you need to add to the scope with: https://www.googleapis.com/auth/userinfo.profile. Then the same request will return the full profile with email address:
{
"id": "<myID>",
"email": "<myemail>#gmail.com",
"verified_email": true,
"name": "<my name>",
"given_name": "<first>",
"family_name": "<last>",
"link": "https://plus.google.com/<myID>",
"picture": "https://lh4.googleusercontent.com/<pic link>/photo.jpg",
"gender": "male",
"birthday": "<YYYY-MM-DD>",
"locale": "en-US"
}

Categories