Skip to main content

Using Gmail API to Send Rich Text Emails Easy Guide!

The new HTML Mail app involves Using Gmail API to Send Rich Text Emails, to send rich-text emails to anyone on your behalf. You can sign up via OAuth by using your Google or Gmail account, and then you’ll see an HTML5 form where you can send emails. The new Gmail API only needs permissions to compose and send messages and does not need access to your Gmail messages or folders, in contrast to Apps Script-based solutions that need full access to your Gmail account. Thus, Using Gmail API to Send Rich Text Emails is easy and quick as well as safe.

Although the app can be easily converted to JavaScript, Python, or Java, it currently uses the Google PHP library to connect to the Gmail API. TinyMCE powers the text editor in the HTML Mail application. Here is the code given and you just need to copy and paste it for Using Gmail API to Send Rich Text Emails.

Code for Using Gmail API to Send Rich Text Emails:

<?php

session_start();

require_once 'Google/Client.php';
require_once 'Google/Service/Gmail.php';

// Replace this with your Google Client ID
$client_id     = 'apps.googleusercontent.com';
$client_secret = 'Your Google Client Secret';
$redirect_uri  = 'http://ctrlq.org/'; // Change this

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

// We only need permissions to compose and send emails
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$service = new Google_Service_Gmail($client);

// Redirect the URL after OAuth
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

// If Access Toket is not set, show the OAuth URL
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

if ($client->getAccessToken() && isset($_POST['message'])) {

  $_SESSION['access_token'] = $client->getAccessToken();

  // Prepare the message in message/rfc822
  try {

      // The message needs to be encoded in Base64URL
      $mime = rtrim(strtr(base64_encode($_POST["message"]), '+/', '-_'), '=');
      $msg = new Google_Service_Gmail_Message();
      $msg->setRaw($mime);
      $service->users_messages->send("me", $msg);

  } catch (Exception $e) {
      print($e->getMessage());
      unset($_SESSION['access_token']);
  }

} ?>

<? if ( isset ( $authUrl ) ) { ?>
<a href="<?= $authUrl; ?>"><img src="google.png" title="Sign-in with Google" /></a>
<? } else { ?>
<form method="POST" action="">
  <textarea name="message" required></textarea>
  <input type="email" required name="to">
  <input type="text"  required name="subject">
  <a href="#" onclick="document.forms.htmlmail.submit();return false;">Send Mail</a>
</form>
<? } ?>

Also see Extract Text from PDF files with Google Apps Script Easy Guide!



from thetechxp https://ift.tt/f4HCxwo
via IFTTT

Comments

Popular posts from this blog

किस महानगर में मिल रहा सबसे सस्ता पेट्रोल? जानें तेल का नया भाव https://ift.tt/zKAj5k6

रोज की तरह आज यानि 9 दिसंबर को भी तेल के नए रेट जारी कर दिए गए हैं. जिसके अनुसार, पेट्रोल-डीजल की कीमतों में कोई बदलाव नहीं किया गया है. सभी राज्यों में वाहन ईंधन की कीमतें जस की तस हैं. आइये जानते हैं, किस महानगर में सबसे कम कीमत पर पेट्रोल मिल रहा है और देश के किस इलाके में सबसे सस्ता और सबसे महंगा पेट्रोल मिल रहा है. from आज तक https://ift.tt/o9nAWyY via IFTTT

Best Website For Finding Government Jobs

  Online Sarkari Job Find   is a reliable and comprehensive online platform dedicated to providing the latest information on job opportunities. Our mission is to assist job seekers in finding their ideal positions by offering timely alerts and notifications on the latest job openings, Sarkari Results, Sarkari Exams, Sarkari jobs, Sarkari networks, and Sarkari Naukri. Our team of dedicated professionals is committed to keeping you informed about the most recent developments in the employment sector. With our user-friendly interface and up-to-date content, we strive to ensure that you have access to accurate and reliable information regarding various government job vacancies. Website Link -  Online Sarkari Job Find

IPL: कप्तान बाहर, दिग्गज भी निकले... रिटेंशन ने कैसे बदल दिया टीमों का गणित https://ift.tt/L7Adk8F

आईपीएल के अगले सीजन के लिए 23 दिसंबर को कोच्चि में मिनी ऑक्शन का आयोजन होना है. इस नीलामी से पहले सभी 10 टीमों की ओर से रिटेंशन लिस्ट जारी कर दी गई. from आज तक https://ift.tt/LK4D2kX via IFTTT