PHP Classes

File: test_smtp_gmail_with_oauth_token_file.php

Recommend this page to a friend!
  Classes of Manuel Lemos   MIME E-mail message sending   test_smtp_gmail_with_oauth_token_file.php   Download  
File: test_smtp_gmail_with_oauth_token_file.php
Role: Example script
Content type: text/plain
Description: Example script that provides the gmail_smtp_mail function to send messages via the Gmail SMTP server using a file to store OAuth token data
Class: MIME E-mail message sending
PHP mailer to compose and send MIME messages
Author: By
Last change:
Date: 1 year ago
Size: 3,854 bytes
 

Contents

Class file image Download
<?php
/*
 * test_smtp_gmail.php
 *
 * @(#) $Id: test_smtp_gmail_with_oauth_token_file.php,v 1.1 2022/10/28 09:18:22 mlemos Exp $
 *
 *
 */

   
require("smtp_gmail.php");

   
/*
     * Get the http.php file from http://www.phpclasses.org/httpclient
     */
   
require('http.php');

   
/*
     * Get the oauth_client.php and file_oauth_client.php file from http://www.phpclasses.org/oauth-api
     */
   
require('oauth_client.php');
    require(
'file_oauth_client.php');


   
$message_object->smtp_debug = 1;
   
$message_object->smtp_html_debug = 0;
   
$message_object->smtp_user = ''; $smtp_user_line = __LINE__;
   
$message_object->smtp_password = ''; $smtp_password_line = __LINE__;
   
$message_object->smtp_token = ''; $smtp_token_line = __LINE__;
   
$message_object->smtp_authentication_mechanism = ''; $smtp_authentication_mechanism_line = __LINE__;
   
$message_object->smtp_token = ''; $smtp_token_line = __LINE__;
   
$message_object->smtp_token_file = ''; $smtp_token_file_line = __LINE__;
   
$message_object->smtp_authentication_server = ''; $smtp_authentication_server_line = __LINE__;
   
$message_object->smtp_authentication_redirect_uri = ''; $smtp_authentication_redirect_uri_line = __LINE__;
   
$message_object->smtp_authentication_client_id = ''; $smtp_authentication_client_id_line = __LINE__;
   
$message_object->smtp_authentication_client_secret = ''; $smtp_authentication_client_secret_line = __LINE__;
   
$message_object->smtp_authentication_scope = ''; $smtp_authentication_client_scope = __LINE__;

    if(
$message_object->smtp_user === '')
        exit(
'Please set the Gmail account email address in the line '.$smtp_user_line."\n");
    if(
$message_object->smtp_password === '')
    {
        if(
$message_object->smtp_authentication_mechanism === 'XOAUTH2')
        {
            if(
$message_object->smtp_token === '')
            {
                if(
$message_object->smtp_token_file === '')
                {
                    exit(
'Please set the Gmail OAuth application token in the line '.$smtp_token_line.' or the application token file in '.$smtp_token_file_line."\n");
                }
            }
            if(
$message_object->smtp_token_file === '')
            {
                if(
$message_object->smtp_token_file === '')
                {
                    exit(
'Please set the Gmail OAuth application token in the line '.$smtp_token_line.' or the application token file in '.$smtp_token_file_line."\n");
                }
            }
            if(
$message_object->smtp_authentication_server === '')
            {
                exit(
'Please set the Gmail OAuth application server to Google in the line '.$smtp_authentication_server_line."\n");
            }
            if(
$message_object->smtp_authentication_redirect_uri === '')
            {
                exit(
'Please set the Gmail OAuth application redirect_uri in the line '.$smtp_authentication_redirect_uri_line."\n");
            }
            if(
$message_object->smtp_authentication_client_id === '')
            {
                exit(
'Please set the Gmail OAuth application client_id in the line '.$smtp_authentication_client_id_line."\n");
            }
            if(
$message_object->smtp_authentication_client_secret === '')
            {
                exit(
'Please set the Gmail OAuth application client_secret in the line '.$smtp_authentication_client_secret_line."\n");
            }
            if(
$message_object->smtp_authentication_scope === '')
            {
                exit(
'Please set the Gmail OAuth application scope in the line '.$smtp_authentication_scope_line."\n");
            }
        }
        else
            exit(
'Please set the Gmail account password in the line '.$smtp_password_line."\n");
    }

   
/*
     * Change these variables to specify your test sender and recipient addresses
     */
   
$from = $message_object->smtp_user;
   
$to = "mlemos@acm.org";

   
$subject = "Testing smtp_mail function";
   
$message = "Hello,\n\nThis message is just to let you know that the smtp_mail() function is working fine as expected.\n\n$from";
   
$additional_headers = "From: $from";
   
$additional_parameters = "-f ".$from;
    if(
gmail_smtp_mail($to,$subject,$message,$additional_headers,$additional_parameters))
        echo
"Ok.";
    else
        echo
"Error: ".$message_object->error."\n";

?>