Google Apps脚本使用UrlFetchApp向phpMailer发送发布请求

时间:2014-03-03 10:54:17

标签: php email google-apps-script phpmailer

我想用UrlFetchApp发送数据,但有些东西无效。我不明白问题在哪里不是很有问题,有人可以向我解释如何配置两个文件来发送和接收参数吗?

Google Apps脚本:

function sendEXTmail(name,email,subject,body) {

   var url = "http://www.mysite.com/willy.php";
   var options = {
      "method": "post",
      "payload": {
      "From_Name": "Fix Name",
      "From_Email": "fix_mail@domain.com",
      "To_Name": name,
      "To_Email": email,
      "Subject": subject,
      "Message": body
      }
   };
   var response = UrlFetchApp.fetch(url, options);
   return response;
}

PHP:

require "phpmailer/class.phpmailer.php";


$from_name = (isset($_POST['From_Name'])) ? $_POST['From_Name'] : '';
$from_email = (isset($_POST['From_Email'])) ? $_POST['From_Email'] : '';
$to_name = (isset($_POST['To_Name'])) ? $_POST['To_Name'] : '';
$to_email = (isset($_POST['To_Email'])) ? $_POST['To_Email'] : '';
$subject = (isset($_POST['Subject'])) ? $_POST['Subject'] : '';
$message = (isset($_POST['Message'])) ? $_POST['Message'] : '';

$mail= new PHPmailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->Host = "localhost";
$mail->Port = 25;
$mail->SMTPSecure = "none";
$mail->SMTPAuth   = false;
$mail->addReplyTo($from_email, $from_name);
$mail->From       = $from_email;
$mail->FromName   = $from_name;
$mail->addAddress($to_email, $to_name);
$mail->Subject  = $subject;
$mail->Body = '<html>'.$message.'</html>';


if(!$mail->Send()){ 
  echo $mail->ErrorInfo; 
}else{ 
  echo 'Email inviata correttamente!';
}
$mail->SmtpClose();
unset($mail);
?>

1 个答案:

答案 0 :(得分:0)

在Google Apps脚本方面,您可以使用UrlFetchApp.getRequest()查看要发送的邮件。例如:

   // Log readable JSON object
   Logger.log( 
     JSON.stringify(
       UrlFetchApp.getRequest(url,options),
       null, 2 )); 

以下是它记录的内容:

[15-05-06 22:23:35:144 EDT] {
  "headers": {
    "X-Forwarded-For": "99.224.168.137"
  },
  "useIntranet": false,
  "followRedirects": true,
  "payload": "From_Name=Fix+Name&Subject=subject_param&Message=body&From_Email=fix_mail@domain.com&To_Email=email@example.com&To_Name=name_param",
  "method": "post",
  "contentType": "application/x-www-form-urlencoded",
  "validateHttpsCertificates": true,
  "url": "http://www.example.com/willy.php"
}

基于此,您的函数似乎已准备好与您的PHP匹配的POST请求。

接下来,检查POST本身的结果。为此,首先使用muteHttpExceptions选项,然后使用HttpResponse.getResponseCode()查找并处理函数中的返回码。此外,要从响应中获取可读文本,请使用response.getContentText()。以下是这样的看法:

function sendEXTmail(name,email,subject,body) {
   name = name || "name_param";
   email = email || "email@example.com";
   subject = subject || "subject_param";
   body = body || "body";

   var url = "http://www.example.com/willy.php";
   var options = {
      "muteHttpExceptions": true,
      "method": "post",
      "payload": {
      "From_Name": "Fix Name",
      "From_Email": "fix_mail@domain.com",
      "To_Name": name,
      "To_Email": email,
      "Subject": subject,
      "Message": body
      }
   };

   // Log readable JSON object
   Logger.log( 
     JSON.stringify(
       UrlFetchApp.getRequest(url,options),
       null, 2 )); 

   var response = UrlFetchApp.fetch(url, options);

   var rc = response.getResponseCode();
   if (rc !== 200) {
     // Failure - log it
     Logger.log( "Error %s: %s", rc, response.getContentText())
   }

   return response.getContentText();
}

尝试使用您的真实网址,看看您的回复。如果它是200,那么交易所的Google Apps脚本版本就能正常运行。如果没有,响应应该告诉您更多关于故障性质的信息,并帮助您找到解决方案。