我有一个PHP网站,我从该网站向我们的客户发送通知电子邮件。 电子邮件是通过html格式发送的,我们从数据库中生成正文,收件人等。
此刻,使用以下代码通过PHP邮件功能发送电子邮件:
function SEND_MAIL($RECIPIENT,$EMAILSUBJECT,$EMAILBODY){
$emailfrom = 'mail@blabla.com';
$fromname = 'From name';
$headers =
'Return-Path: ' . $emailfrom . "\r\n" .
'From: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'X-Priority: 3' . "\r\n" .
'X-Mailer: PHP ' . phpversion() . "\r\n" .
'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: 8bit' . "\r\n" .
'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$params = '-f ' . $emailfrom;
$sendit = mail($RECIPIENT, $EMAILSUBJECT, $EMAILBODY, $headers, $params);
}
SEND_MAIL($CONTACTS_EMAIL,$EMAILSUBJECT_PERSON_A,$EMAILBODY_PERSON_A);
我们想使用GMAIL API替换此功能,因为我们希望跟踪GMAIL帐户中的已发送邮件。我们使用Bluehost作为提供程序,因此无法使用到GMAIL服务器的SMTP。 我们在许多地方都使用了此函数,因此我们只希望在可能的情况下修改这段代码。 我花了几天的时间试图使它生效,最终我得到了一些结果。
到目前为止,我已经在google开发人员控制台中配置了所有内容,并且能够运行quickstart.php(如in this tutorial所述)。 使用本教程,我们上传了JSON文件,并且在设置了我们从gmail复制/粘贴的令牌后,可以访问GMAIL帐户的文件夹(标签)。
然后我能够使用在网上找到的代码示例发送电子邮件
session_start();
require __DIR__ . '/vendor/autoload.php';
// Replace this with your Google Client ID
$client_id = 'blabla.apps.googleusercontent.com';
$client_secret = 'secret';
$redirect_uri = 'https://www.redirecturl';
$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
$emailad=$_POST['emailad'];
$username=$_POST['username'];
$emailbody=$_POST['emailbody'];
$strSubject = $_POST['subject'];
$strRawMessage = "From: fromname <fromemail>\r\n";
$strRawMessage .= "To: $username <$emailad>\r\n";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= "MIME-Version: 1.0\r\n";
$strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= "$emailbody\r\n";
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$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="emailbody" required></textarea>
<input type="email" required name="emailad">
<input type="text" required name="subject">
<input type="text" required name="username">
<input type="submit" value="submit">
</form>
<? } ?>
这最后一种方法还需要来自Google的令牌,并要求访问帐户。
因此,我们想使用这两种方法之一来替换我们的SEND_MAIL函数。 我还想知道是否有可能永远不会使访问令牌过期,以便一旦我们允许访问就永远不必经历Gmail接受程序。
感谢您的帮助!
答案 0 :(得分:2)
无法获取不会过期的访问令牌,但是可以获取刷新token。刷新令牌永不过期,并且可以使用,直到您取消对应用程序的授权。
我使用了您提供的代码,并且进行了一些细微的调整和更改,因此能够使用Google Gmail API发送电子邮件。唯一的缺点是刷新令牌和原始访问令牌需要存储在数据库或文件中,以便Google PHP API可以查看令牌,但这限制了必须重新登录并重新授权应用程序的需要。
身份验证文件-该文件只能使用一次。它是在数据库中设置令牌。设置令牌后,您可以从服务器中删除该文件。
<?php
require '/var/www/html/vendor/autoload.php'; // For Google Client Composer
// Replace this with your Google Client ID
$client_id = 'blabla.apps.googleusercontent.com';
$client_secret = 'secret';
$redirect_uri = 'https://www.redirecturl';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline'); //Added for Refresh Token
$client->setApprovalPrompt('force'); //Added for Refresh Token
// We only need permissions to compose and send emails
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
// Redirect the URL after OAuth
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
}
// If Access Token is not set, show the OAuth URL
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$display = "display: none";
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
$access_token = $client->getAccessToken();
echo '<a href="'.$authUrl.'" style="'.$display.'">Authorize ME PLZ</a>';
if ($access_token != null) {
echo "You're In!";
}
// Saves Access Token Into Database to be Used In SEND_MAIL function
$mysqli = new mysqli('servername', 'username', 'password', 'database');
$stmt = $mysqli->prepare("INSERT INTO refresh_token (refresh, original) VALUES (?, ?)");
$stmt->bind_param("ss", json_encode($access_token), json_encode($access_token)); // Updated to remove warning
$stmt->execute();
$stmt->close();
?>
SEND_MAIL功能-从数据库获取值以认证Gmail API:
<?php
require '/var/www/html/vendor/autoload.php';
function SEND_MAIL($RECIPIENT,$EMAILSUBJECT,$EMAILBODY){
//Get Refresh Token From Database set when running Authentication File
$conn = new mysqli("servername", "username", "password", 'database');
$sql = "SELECT * FROM refresh_token";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$token = $row['original'];
$refresh_token = $row['refresh'];
}
}
$conn->close();
// Replace this with your Google Client ID
$client_id = 'blabla.apps.googleusercontent.com';
$client_secret = 'secret';
$redirect_uri = 'https://www.redirecturl';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setAccessToken($token);
if ($client->isAccessTokenExpired()) {
$client->refreshToken($refresh_token);
$newtoken = $client->getAccessToken();
$client->setAccessToken($newtoken);
}
$service = new Google_Service_Gmail($client);
$fromemail = "<the-email-you-want-to-send-from>@gmail.com";
$strRawMessage = "From: Email <$fromemail> \r\n";
$strRawMessage .= "To: <$RECIPIENT>\r\n";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($EMAILSUBJECT) . "?=\r\n";
$strRawMessage .= "MIME-Version: 1.0\r\n";
$strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= "$EMAILBODY\r\n";
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$service->users_messages->send("me", $msg);
}
SEND_MAIL('test@me.com', 'Test', 'Hey!');
?>