0

Trying to send data from Google App Script to a php site which sends an email from my database. For most of the date it is no problem I send it via the url(example.com/example.php?id=test&id2=test2&id3=test3).

For most of the data it is no problem except for the third parameter, the given value is way longer than the other two and may contain newlines as well. My idea was to send the third(or all values) as JSON object. My Google AppScript code so far:

function  sendData(){
  var options = {'text' : 'A longer example text'}  ;
  try {
    var url = "https://example.com/example.php?id1=test1&id2=test2&id3="+options;
    var httpRequest = UrlFetchApp.fetch(url, options).getContentText();
  }
  catch(e){
    Logger.log(e);
  }
  return;
}

My PHP code:

<?php
require 'includes/functions.php';
include_once 'config.php';
include 'ChromePhp.php';

$email = $_GET["id1"];
$name = $_GET["id2"];
$text = $_GET["id3"];

$json =     json_decode($text, true);
ChromePhp::log($json->text);

if(strpos($text, "\n") !== FALSE) {
    $text = str_replace("\n", "<br>", $text);
} 

$replace_to_standard=[$name, $text];
$replace_standard =['nametochange', 'textochange'];
$delivery_added = str_replace($replace_standard, $replace_to_standard, file_get_contents('standardMail.html'));

echo "" . $delivery_added . "";

$m = new MailSender;
$m-> sendMail($email, $email, $delivery_added, "Mail All");

The chrome logger will log "null"

Any ideas?

1 Answer 1

2

Using an AJAX post as an example, you can use the POST capability of Google apps script to send a post request to your PHP file and process it using the $_POST function of PHP.

Try in you GAS:

var payload = {
    'text' : 'A longer example text',
    'email' : '[email protected]'
  };
  var options = {
    'method' : 'post',
    'payload' : payload
  };
  
   
  //var text = String(jsonData.comment);
  try {
    var url = "https://etadres.nl/login/mailAll.php";
    var httpRequest = UrlFetchApp.fetch(url, options).getContentText();
  }
  catch(e){
    Logger.log(e);
  }

Catch your POST request in PHP and edit it as follows:

<?php
require 'includes/functions.php';
include_once 'config.php';
include 'ChromePhp.php';

 
//Receive the RAW post data via the php://input IO stream.
$content = file_get_contents("php://input");

$email = $_POST["email"];
$text= $_POST["text"];


if(strpos($text, "\n") !== FALSE) {
    $text = str_replace("\n", "<br>", $text);
} 

$replace_to_standard=[$name, $text];
$replace_standard =['nametochange', 'textochange'];
$delivery_added = str_replace($replace_standard, $replace_to_standard, file_get_contents('standardMail.html'));

echo "" . $delivery_added . "";

$m = new MailSender;
$m-> sendMail($email, $email, $delivery_added, "Mail All");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.