gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:09 25 4
gpt4 key购买 nike

我想用 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);
?>

最佳答案

在 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();
}

用你的真实 URL 试试这个,看看你会得到什么。如果是 200,则表示交换的 Google Apps 脚本端工作正常。如果不是,响应应该告诉您更多有关故障性质的信息,并帮助您找到解决方案。

关于php - Google Apps 脚本使用 UrlFetchApp 向 phpMailer 发送发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com