[英]How do you know a soap client is using https/ssl? Using php nusoap library
[英]Gmail API PHP Client Library - How do you create a draft email with an attachment using the PHP client library?
我正在使用Google的PHP客戶端庫向Gmail的API發送電話。 使用這些資源,我可以創建草稿電子郵件。 但我不能為我的生活弄清楚如何使用客戶端庫來創建帶附件的草稿電子郵件。 我已經在網上找了兩個星期在網上找到的東西,但我仍然沒有答案。
這似乎是直截了當的。 有人有答案嗎?
謝謝!
編輯:感謝johncorser推動正確的方向。 使用zetacomponents,我能夠正確地構建消息(在使用PHPMailer和Mail_mime失敗了兩周之后)。 這是我的最終代碼:
<?php
session_start();
require 'Google/autoload.php';
require 'Google/Client.php';
require 'Google/Service/Gmail.php';
require 'ezc/Base/base.php';
spl_autoload_register( array( 'ezcBase', 'autoload' ) );
//Get credential
$config = parse_ini_file('helpers/config.ini');
// Setup Google API Client
$client = new Google_Client();
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['redirect_url']);
$client->addScope('https://www.googleapis.com/auth/gmail.modify');
// Create GMail Service
$service = new Google_Service_Gmail($client);
// Check if user is logged out
if(isset($_REQUEST['logout'])){
unset($_SESSION['access_token']);
}
// Check if we have an authorization code; if so, use it
if(isset($_GET['code'])){
$code = $_GET['code'];
$client->authenticate($code);
$_SESSION['access_token'] = $client->getAccessToken();
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($url,FILTER_VALIDATE_URL));
}
// Check if we have an access token in the session; if so, setup input form; if not, create auth URL
if(isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
echo "<form action='' method='post' enctype='multipart/form-data'>
<p>Your First Name: <input type='text' name='first' required></p>
<p>Your Last Name: <input type='text' name='last' required></p>
<p>Body (without any greeting):</p> <textarea rows='20' cols='100' name='body' required></textarea>
<p>Select headshot (JPG, JPEG, PNG, or GIF) to upload: <input type='file' name='headshotToUpload' id='headshotToUpload' required></p>
<p>Select resume PDF to upload: <input type='file' name='resumeToUpload' id='resumeToUpload' required></p>
<p><input type='submit' name='submit' value='Create Drafts'></p></form>";
} else {
$loginUrl = $client->createAuthUrl();
}
//If auth URL is set, create llink for user to login; otherwise, show logout link
if (isset($loginUrl)) {
echo "<a class='login' href='" . $loginUrl . "'>First Click Here to Connect Through Google!</a>";
} else {
echo "<a class='logout' href='?logout'>Logout</a>";
}
//Once submit button is pressed, begin file upload process
if(isset($_POST['submit'])) {
$target_dir = "uploads/";
$target_hsfile = $target_dir . basename($_FILES["headshotToUpload"]["name"]);
$target_cvfile = $target_dir . basename($_FILES["resumeToUpload"]["name"]);
$uploadOk = 1;
//Check if filenames already exist
if (file_exists($target_hsfile)) {
echo "Please rename your headshot before uploading.";
$uploadOk = 0;
}
if (file_exists($target_cvfile)) {
echo "Please rename your resume before uploading.";
$uploadOk = 0;
}
//Check that file types are permitted
$hsFileType = pathinfo($target_hsfile,PATHINFO_EXTENSION);
if($hsFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed for headshots.";
$uploadOk = 0;
}
$cvFileType = pathinfo($target_cvfile,PATHINFO_EXTENSION);
if($cvFileType != "pdf") {
echo "Sorry, resumes must be in PDF format only.";
$uploadOk = 0;
}
//Report on success or failure of upload
if ($uploadOk == 0) {
echo "Sorry, your files were not uploaded. Please correct the noted error(s) and try again";
} else {
// If everything is ok, try to upload file
if ((move_uploaded_file($_FILES["headshotToUpload"]["tmp_name"], $target_hsfile)) && ((move_uploaded_file($_FILES["resumeToUpload"]["tmp_name"], $target_cvfile)))) {
echo "The files ". basename( $_FILES["headshotToUpload"]["name"]). " and ". basename( $_FILES["resumeToUpload"]["name"]). " have been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
//Load data
$agent_data = parse_ini_file('helpers/agents.ini', true);
//Iterate through agencies
foreach ($agent_data as $agency) {
$mail = new ezcMail();
$mail->addTo( new ezcMailAddress($agency['agent_email'], $agency['agent_first']." ".$agency['agent_last'] ));
$mail->subject = "Submission of " . $_POST['first'] . " " . $_POST['last'] . " to " . $agency['agency_name'];
$textpart = new ezcMailText("Dear " . $agency['agent_salut'] . " " . $agency['agent_last'] . "," . "\r\n" . "\r\n" . $_POST['body'] . "\r\n" . "\r\n" . "Sincerely," . "\r\n" . "\r\n" . $_POST['first'] . " " . $_POST['last']);
$fileAttachment1 = new ezcMailFile($target_hsfile);
$fileAttachment2 = new ezcMailFile($target_cvfile);
$mail->body = new ezcMailMultipartMixed( $textpart, $fileAttachment1, $fileAttachment2 );
$message = $mail->generate();
$msgbody = new Google_Service_Gmail_Message();
$msgbody->setRaw(rtrim(strtr(base64_encode($message), '+/', '-_'), '='));
$draft = new Google_Service_Gmail_Draft();
$draft->setMessage($msgbody);
$service->users_drafts->create('me', $draft);
unset($mail);
unset($msgbody);
unset($draft);
}
}
?>
如果您有原始電子郵件,這很容易。 我試試這樣的東西:
public function save_draft(Google_Service_Gmail $gmail, $raw_message, $gmail_thread_id = null)
{
$postBody = new Google_Service_Gmail_Message();
$postBody->setRaw(rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
if ($gmail_thread_id)
$postBody->setThreadId($gmail_thread_id);
$draft = new Google_Service_Gmail_Draft();
$draft->setMessage($postBody);
return $gmail->users_drafts->create('me', $draft);
}
此函數應創建草稿,將該草稿添加到您傳入的任何gmail線程中,並返回結果。 這個奇怪的base64_encode業務的原因是因為gmail api需要一個相當具體的編碼。 您應該能夠將附件包含在原始主體中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.