I'm trying to develop a PHP script resting on the Gmail API that would make possible to snooze my messages at a specific time, i.e. archive and un-archive a message at a specific datetime.
Everything is in place and works but a detail, that is I cannot keep the "From" headers of the original message. More specifically:
- The ID of the message to snooze is retrieved at a specific time thanks to a CRON job;
- Using the message ID, the message in raw format is retrieved and cloned;
- The original message is deleted and the cloned message is sent (and received) => This ensures that the email is displayed at the top of the inbox.
Problem: the cloned email is a perfect copy of the original one but the "From" headers which display the email address of the authenticated user, i.e. myself (username@gmail.com).
//[...] object $this->message
private function cloneMail() {
// GET RAW message
$this->message->raw = $this->gmail->users_messages->get(
$this->message->user,
$this->message->id,
array('format'=>'raw')
);
try {
// INSERT original message
$inserted = $this->gmail->users_messages->delete(
$this->message->user,
$this->message->id
);
// ONCE DELETED, SEND CLONED EMAIL
if ($deleted->getId()) {
try {
$this->gmail->users_messages->send(
$this->message->user,
$this->message->raw
);
} catch (Exception $e) {
// -- Fallback...
}
}
} catch(Exception $e) {}
}
[Updated] Working solution using messages.insert()
private function cloneMail() {
// GET RAW message
$this->message->raw = $this->gmail->users_messages->get(
$this->message->user,
$this->message->id,
array('format'=>'raw')
);
try {
// DELETE original message
$deleted = $this->gmail->users_messages->delete(
$this->message->user,
$this->message->id
);
// ONCE DELETED, SEND CLONED EMAIL
if ($deleted->getId()) {
try {
$this->gmail->users_messages->insert(
$this->message->user,
$this->message->raw
);
} catch (Exception $e) {
// -- Fallback...
}
}
} catch(Exception $e) {}
}