With php-ews is there a possibility to copy a message from an inbox to other user's inbox?
The goal is that a message is created by php-ews in a web interface, saved to a shared inbox and after this save a copy of the message to user's inbox.
My code so far:
$id = $mail_items[$i]->ItemId->Id;
$change_key = $mail_items[$i]->ItemId->ChangeKey;
$request = new EWSType_CopyItemType();
$request->ToFolderId->FolderId->Id = $user_folder_id;
$request->ToFolderId->FolderId->ChangeKey = $user_folder_ckey;
$request->ItemIds->ItemId->Id = $id;
$request->ItemIds->ItemId->ChangeKey = $change_key;
$response = $ews->CopyItem($request);
The message does not appear in the other user's inbox. Thanks!
Thanks to Gareth Parker, my error was found.
I had forgotten to give always the exchange version in my clients.
just specify the same Exchange version in every client you create:
$ews = new ExchangeWebServices($host, $username, $password, ExchangeWebServices::VERSION_2007_SP1);
Related
Hi I just got approved on Twilio to use it's WhatsApp messaging service.
I have no problem when testing it in the sandbox, but I got trouble when I moved it into production environment.
Based on Twilio explanation, I have to start the conversation to WhatsApp customer using one of pre-approved templates. When the customer replied, we got 24 hours of window to send freeform messages.
I already did what's in the https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates but unfortunately the given example is actually for freeform message.
Here's the script:
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACxxxxxxxxxxxxxxxxxxxxx";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("whatsapp:+14155238886", // to
array(
"from" => "whatsapp:+15005550006",
"body" => "Hi Joe! Thanks for placing an order with us. We’ll let you know once your order has been processed and delivered. Your order number is O12235234"
)
);
print($message->sid);
Can anyone please help me with PHP script on how to send the WhatsApp message using this pre-approved template?
Alright, maybe some of you got here trying to ask the similar question and here's what I got after contacting the Twilio Support:
My WhatsApp API works now.
There's nothing wrong with my code nor their code (what's in their documentation https://www.twilio.com/docs/sms/whatsapp/tutorial/send-whatsapp-notification-messages-templates), actually they're using the same code to send either template message or freeform message.
Their Template Submission API to WhatsApp contains bug that creates mismatch between what we actually had in Twilio and what WhatsApp actually received. So that's why the first message I sent (even though I used the pre-approved template) always treated as freeform message thus it undelivered.
Twilio WhatsApp API is still in beta service, means bugs are expected. While it's still in beta, they recommend that we need to create templates as simple as possible and avoid formatting like bold, italics, strikethrough, etc also new lines (\n) being used in templates.
Thats all I can share and I hope you don't have problem just like I did.
Cheers!
Below is our code with predefined templates
$number = "+919XXXXXXXXX";
$to = "whatsapp:" . $number;
$from = "whatsapp:+1YYYYYYYYYY";
$msg = "Un rendez-vous de {{1}} pour {{2}} avec {{3}} et prévu le {{4}} a été créé.";
$accountSid = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
$authToken = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
$twilioClient = new Client($accountSid, $authToken);
$msg_data = array("from" => $from, "body" => $msg);
try {
$message = $twilioClient->messages->create($to, $msg_data);
$response = $message->sid ? $message->sid : '';
error_log("Twilio msg response : " . print_r($response, true));
} catch (TwilioException $e) {
error_log('Could not send whatsapp notification to ' . $number);
error_log('Could not send whatsapp TwilioException' . $e->getMessage());
}
One suggestion check for white space while creating message string. Even for a single white space they reject it.
So I got access to the new o365 v2 api and it's working pretty good so far. I am however having trouble accessing any shared inboxes.
Even worse, there doesn't appear to be any error message being returned:
#odata.context = https://outlook.office.com/api/v2.0/$metadata#Me/Messages(Subject,ReceivedDateTime,SentDateTime,Sender,From,ToRecipients,CcRecipients,BccRecipients,ReplyTo,ConversationId,IsRead,InternetMessageId
[value] =
Has anyone ever tried this?
To clarify, this isn't for exchange, but outlook.com
It seems that you were using the delegate-token to request the message from the specific user for the messages in a shared box.
The Office 365 REST API only support app-level token to get the messages from the organization. The delegate-token only could get the messages of the delegatee user.
You can also consider using the EWS to retrieve the messages of shared box as a workaround.
Here is an example for your reference:
string userName = "";
string password = "";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential(userName, password);
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl(userName, RedirectionUrlValidationCallback);
FolderId SharedMailbox = new FolderId(WellKnownFolderName.Inbox, "sharedmailbox#consoto.onmicrosoft.com");
ItemView itemView = new ItemView(10);
var results = service.FindItems(SharedMailbox, itemView);
foreach (var item in results)
{
Console.WriteLine(item.Subject);
}
And if you want the Office 365 REST API to support this feature, you can also submit the feedback from here.
it is clearly stated in the examples how to send message but, i am not clear with how to receive messages initiated by other client in the server any help is highly appreciated
Here is my code for sending the message
include("xmpphp/xmpp.php");
//username = user without domain, "user" and not "user#server" - home is the resource
$conn = new XMPPHP_XMPP('localhost', 5222, 'robel27', 'yoyene122127', 'kiyos');
// Enables TLS - enabled by default, call before connect()!
$conn->useEncryption(false);
$conn->connect();
// shows the user's status message
$conn->presence();
$conn->processUntil(array('session_start', 'roster_received'));
$conn->processTime(2);
$conn->message('kiyos12#kiyos', $_POST['msg1']);
$conn->disconnect();
To get messages you can use $data = $conn->processUntil('message');
I'm using https://github.com/jamesiarmes/php-ews for Exchange Web Services and I'm having a problem adding a contact for an account.
When I login using my impersonation details:
$ews = new ExchangeWebServices($this->server_address, $this->server_username, $this->server_password);
I want to create a new contact, e.g.:
$request = new EWSType_CreateItemType();
$contact = new EWSType_ContactItemType();
$contact->Initials = $this->relation->initials;
$contact->GivenName = $this->relation->first_name;
$contact->MiddleName = $this->relation->insertion;
$contact->Surname = $this->relation->last_name;
$request->Items->Contact[] = $contact;
$result = $ews->CreateItem($request);
I want to add this contact to a account that exists in my impersonation account list.
I want to avoid directly login in as an exchange user like:
$ews = new ExchangeWebServices($this->server_address, 'some#outlook.account', 'somepassword');
Is this possible? How would I achieve such a thing? Thanks for reading!
I'd suggest you most away from james's php-ews, it's unmaintained and doesn't follow any of the PSR's. I'd suggest you take a look at my own fork, garethp/php-ews. Creating a contact would be rather similar, but impersonation is made easy by my fork (example here) and if that's not working for you, you can always drop me a Github issue and I'll try to help out
I'm using Excel VBA to get a user's oAuth2 token for the mail.google.com scope for a native application. It works fine so I have the the user's Access Token (it refreshes if it expires) and I have the user's email address. I was using Zend SMTP to send the emails but found out there are limits to bulk sending emails this way (currently my account is locked out due to going over quota while trying to figure this out over the weekend). My client may send 2,000 emails at once and locking themselves out of their Gmail account for 24 hours is unacceptable. I think using the Gmail REST API will work, as it has higher quotas, but not finding any detailed PHP examples of how to create and send an HTML email via an HTTP request.
I know I have to create the MIME email and think I can figure that out (using this thread stackoverflow.com/questions/24940984/send-email-using-gmail-api-and-google-api-php-client) or from the Zend emails I was successfully creating. My question is what PHP code do I use to create and send the email?
The Google Dev examples all reference /userId/ (from developers.google.com/gmail/api/v1/reference/) but not sure where to get that if I just have the users token and their email address.
I assume it's something like this:
<?php
require 'GoogleAPI/src/Google/autoload.php';
extract($_POST); // use to get my client token, client's email address, the email to, cc, bcc, subject, body, etc.
//<Build the email $mime message here>
$m = new Google_Service_Gmail_Message();
$data = base64_encode($mime);
$data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
$m->setRaw($data);
$service->users_messages->send('me', $m);
?>
I don't know where to put the user's token. I know 'me' is the person that is authenticated.
I'm open to using an HTTP request as well, something like https://mywebsite.com/sendgmail.php?token=[UsersToken]&UserEmail=joe#test.com&ToEmail=toperson#xyz.com&Subject=HI&Body=Thanks for your help instead of the post method.
Just not sure how to implement that either.
I'm self taught PHP and VBA and new to the google api world so please forgive me if this is easy. I'm also looking into boxspring but trying to use the native APIs and PHP first.
EDIT:
So I've tried this using the examples but getting an error that my token is not in proper JSON format. I just want to send the token as a string. If I can't is there a way to make the string into JSON so oauth2 will accept it?:
<?php
require 'GoogleAPI/src/Google/autoload.php';
/**
* Returns an authorized API client.
* #return Google_Client the authorized client object
*/
$client = new Google_Client();
$client->setAccessToken('ya29.hwFcwk2M73vaPwNObeuwizHGjXT2y6UsAFEcDIvRAoWTM28gu2pJeK4GiMySkfAllTOQvXVMYfffff');
// Get the API client and construct the service object.
//$client = getClient();
$service = new Google_Service_Gmail($client);
// Print the labels in the user's account.
$user = 'me';
$results = $service->users_labels->listUsersLabels($user);
if (count($results->getLabels()) == 0) {
print "No labels found.\n";
} else {
print "Labels:\n";
foreach ($results->getLabels() as $label) {
printf("- %s\n", $label->getName());
}
}
?>
Error:
Fatal error: Uncaught exception 'Google_Auth_Exception' with message
'Could not json decode the token' in
/home/[my domain]/public/GoogleAPI/src/Google/Auth/OAuth2.php:179
Stack trace: #0
/home/[my domain]/public/GoogleAPI/src/Google/Client.php(215):
Google_Auth_OAuth2->setAccessToken('ya29.hwFcwk2M73...') #1
/home/[my domain]/public/labels.php(17):
Google_Client->setAccessToken('ya29.hwFcwk2M73...') #2 {main} thrown
in
/home/[my domain]/public/GoogleAPI/src/Google/Auth/OAuth2.php
on line 179
I'm sure I'm making this more difficult than it should be. I'm using the PHP example of showing a users labels, even though my final goal is to send emails.