我正在尝试使用 SparkPost 通过电子邮件将日历邀请作为附件发送,但收到电子邮件后邀请不会打开。我使用两个文件"calendarinvite.php"来创建邀请,并使用"Sendemail.php"发送电子邮件。
日历邀请.php文件可以正确创建邀请,因为当我运行脚本时,我可以下载邀请并打开它。 但是,一旦作为附件发送,我就无法打开邀请。
任何帮助将不胜感激。
发送电子邮件.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// include substr($_SERVER["DOCUMENT_ROOT"], 0, strrpos($_SERVER["DOCUMENT_ROOT"], '\')) . '/thingzine/sparkpost/CalendarInvit.php';
// echo $ical;
// Function to Access SparkPost API:
function sparkpost($method, $uri, $payload = [], $headers = [])
{
$defaultHeaders = [ 'Content-Type: application/json' ];
$curl = curl_init();
$method = strtoupper($method);
$finalHeaders = array_merge($defaultHeaders, $headers);
$url = 'https://api.sparkpost.com:443/api/v1/'.$uri;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($method !== 'GET') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $finalHeaders);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// Variables for Attachment:
$filePath = dirname(__FILE__).'/';
$fileName = 'calendar.ics';
// $fileName = 'test.jpg';
// ====== Sample Code: mime_content_type not a function in our version of php:
// mime_content_type not a function in our version of php:
// $attFile = $filePath.$fileName;
// $fileType = mime_content_type($attFile);
// ==================
$fileData = base64_encode($filePath.$fileName);
// ========= Vairable to get template for email Body:
$fileNameHTLM = 'TemplateTest.html';
$htmltemplate = $filePath.$fileNameHTLM;
$html = file_get_contents($htmltemplate);
// ===============================
// Payload to send Email:
$payload = [
'options' => ['sandbox' => false,],
'content' => [
'from' => [
'name' => 'Team 3',
'email' => 'XXXXX@XXXX.com',
],
'subject' => 'Sending an email with SparkPost',
'html' => "$html",
'attachments' => [
[
'name' => $fileName,
'type' => 'application/ics',
'data' => $fileData,
],
],
],
'recipients' => [
['address' => 'RRRDF@gmail.com', 'name' => 'Testing'],
],
];
$headers = [ 'Authorization: XXXXXXXXXXXXX' ];
?>
日历邀请.php:
<?php
$domain = "XXX.com";
$sentDate = gmdate('Ymd') . 'T' . gmdate('His');
$message = "";
$fromName = "ZZZZ";
$fromEmail = "xxx@xxxx.com";
$toName = "George";
$toEmail = "XXXX@XXXXX.com";
$subject = gmdate('His') . " - test calen invite";
$description = "SPARKPOST: This is a meeting to eat steak. Come hungry.";
$startTime = "20190130T053000";
$endTime = "20190130T073000";
$location = "123 oak ave, CA 07642";
$organizerName = "YYYY O.";
$organizerEmail = "XXX@yahoo.com";
$eventTitle = "Meeting Calendar Event";
$msgID = md5(uniqid(mt_rand(), true)) . ".nariel.com";
//Create ICAL Content (Google rfc 2445 for details and examples of usage)
$ical = 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
LOCATION:' . $location . '
DESCRIPTION: ' . $description . '
DTEND:' . $endTime . 'Z
DTSTAMP:' . $sentDate . 'Z
DTSTART:' . $startTime . 'Z
ORGANIZER;CN=' . $organizerName . ':mailto:' . $fromEmail . '
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:' . $eventTitle . '
TRANSP:OPAQUE
UID:' . $msgID . '
X-ALT-DESC;FMTTYPE=text/html:
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR';
// $message .= $ical;
// $mimeMessage = $headers . $message;
// echo $mimeMessage . "<br><br><br><br>"
// header('Content-Type: text/calendar; method=REQUEST; charset="UTF-8"n');
// header('Content-Disposition: inline; filename=Invit.ics');
header('Content-Type: text/Calendar; charset="UTF-8"; method=REQUEST');
header("Content-Disposition: inline; filename=calendar.ics");
echo $ical;
?>
这对
我有用:
传输到 API 的 JSON 输入
{
"options": {
"open_tracking": true,
"click_tracking": true,
"transactional": false,
"sandbox": false,
"inline_css": false
},
"campaign_id": "test",
"recipients": [
{
"address": {
"email": "test@example.com",
"name": "test recipient"
},
"tags": [],
"substitution_data": {
"firstname": "John"
}
}
],
"content": {
"from": {
"email": "test@test.example.com",
"name": "Sparkpost Demo System"
},
"subject": "My Cal Invite",
"reply_to": "test@example.com",
"text": "Sample Calendar Invite n ",
"html": "<p>Here is a sample calendar invite</p>",
"attachments": [
{
"name": "invite.ics",
"type": "application/ics; name="invite.ics"",
"data": "QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KQ0FMU0NBTEU6R1JFR09SSUFODQpNRVRIT0Q6UkVRVUVTVA0KQkVHSU46VkVWRU5UDQpEVFNUQVJUOjIwMTkwMTA5VDIyMDAwMFoNCkRURU5EOjIwMTkwMTA5VDIzMDAwMFoNCkRUU1RBTVA6MjAxOTAxMDlUMjEyNDQxWg0KT1JHQU5JWkVSO0NOPUpvaG4gRG9lOm1haWx0bzpqb2huQGV4YW1wbGUuY29tDQpVSUQ6MjA0QTNDQTYtOENDNS00MzJFLTk3NzgtMzQxOENBNDY3QUIzDQpBVFRFTkRFRTtDVVRZUEU9SU5ESVZJRFVBTDtST0xFPVJFUS1QQVJUSUNJUEFOVDtQQVJUU1RBVD1BQ0NFUFRFRDtSU1ZQPVRSVUUNCiA7Q049SmFuZSBEb2U7WC1OVU0tR1VFU1RTPTA6bWFpbHRvOmphbmVAZXhhbXBsZS5jb20NCkFUVEVOREVFO0NVVFlQRT1JTkRJVklEVUFMO1JPTEU9UkVRLVBBUlRJQ0lQQU5UO1BBUlRTVEFUPU5FRURTLUFDVElPTjtSU1ZQPQ0KIFRSVUU7Q049am9obkBleGFtcGxlLmNvbTtYLU5VTS1HVUVTVFM9MDptYWlsdG86am9obkBleGFtcGxlLmNvbQ0KQ1JFQVRFRDoyMDE5MDEwOVQyMTI0MDJaDQpERVNDUklQVElPTjpZb3VyIGV2ZW50IGNvbnRlbnQNCkxBU1QtTU9ESUZJRUQ6MjAxOTAxMDlUMjEyNDQwWg0KTE9DQVRJT046DQpTRVFVRU5DRTowDQpTVEFUVVM6Q09ORklSTUVEDQpTVU1NQVJZOlRlc3QNClRSQU5TUDpPUEFRVUUNClgtQVBQTEUtVFJBVkVMLUFEVklTT1JZLUJFSEFWSU9SOkFVVE9NQVRJQw0KRU5EOlZFVkVOVA0KRU5EOlZDQUxFTkRBUg0K"
}
]
}
}
下面是一个示例 CURL 命令。您需要输入正确的收件人/发件人地址和您的 API 密钥。
curl -X POST
https://api.sparkpost.com/api/v1/transmissions
-H 'Authorization: [YOUR_TOKEN_HERE]'
-H 'cache-control: no-cache'
-d '{
"options": {
"open_tracking": true,
"click_tracking": true,
"transactional": false,
"sandbox": false,
"inline_css": false
},
"campaign_id": "test",
"recipients": [
{
"address": {
"email": "test@example.com",
"name": "test recipient"
},
"tags": [],
"substitution_data": {
"firstname": "John"
}
}
],
"content": {
"from": {
"email": "test@test.example.com",
"name": "Sparkpost Demo System"
},
"subject": "My Cal Invite",
"reply_to": "test@example.com",
"text": "Sample Calendar Invite n ",
"html": "<p>Here is a sample calendar invite</p>",
"attachments": [
{
"name": "invite.ics",
"type": "application/ics; name="invite.ics"",
"data": "QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KQ0FMU0NBTEU6R1JFR09SSUFODQpNRVRIT0Q6UkVRVUVTVA0KQkVHSU46VkVWRU5UDQpEVFNUQVJUOjIwMTkwMTA5VDIyMDAwMFoNCkRURU5EOjIwMTkwMTA5VDIzMDAwMFoNCkRUU1RBTVA6MjAxOTAxMDlUMjEyNDQxWg0KT1JHQU5JWkVSO0NOPUpvaG4gRG9lOm1haWx0bzpqb2huQGV4YW1wbGUuY29tDQpVSUQ6MjA0QTNDQTYtOENDNS00MzJFLTk3NzgtMzQxOENBNDY3QUIzDQpBVFRFTkRFRTtDVVRZUEU9SU5ESVZJRFVBTDtST0xFPVJFUS1QQVJUSUNJUEFOVDtQQVJUU1RBVD1BQ0NFUFRFRDtSU1ZQPVRSVUUNCiA7Q049SmFuZSBEb2U7WC1OVU0tR1VFU1RTPTA6bWFpbHRvOmphbmVAZXhhbXBsZS5jb20NCkFUVEVOREVFO0NVVFlQRT1JTkRJVklEVUFMO1JPTEU9UkVRLVBBUlRJQ0lQQU5UO1BBUlRTVEFUPU5FRURTLUFDVElPTjtSU1ZQPQ0KIFRSVUU7Q049am9obkBleGFtcGxlLmNvbTtYLU5VTS1HVUVTVFM9MDptYWlsdG86am9obkBleGFtcGxlLmNvbQ0KQ1JFQVRFRDoyMDE5MDEwOVQyMTI0MDJaDQpERVNDUklQVElPTjpZb3VyIGV2ZW50IGNvbnRlbnQNCkxBU1QtTU9ESUZJRUQ6MjAxOTAxMDlUMjEyNDQwWg0KTE9DQVRJT046DQpTRVFVRU5DRTowDQpTVEFUVVM6Q09ORklSTUVEDQpTVU1NQVJZOlRlc3QNClRSQU5TUDpPUEFRVUUNClgtQVBQTEUtVFJBVkVMLUFEVklTT1JZLUJFSEFWSU9SOkFVVE9NQVRJQw0KRU5EOlZFVkVOVA0KRU5EOlZDQUxFTkRBUg0K"
}
]
}
}
'
以下是 Base64 之前的原始日历数据:
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20190109T220000Z
DTEND:20190109T230000Z
DTSTAMP:20190109T212441Z
ORGANIZER;CN=John Doe:mailto:john@example.com
UID:204A3CA6-8CC5-432E-9778-3418CA467AB3
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE
;CN=Jane Doe;X-NUM-GUESTS=0:mailto:jane@example.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
TRUE;CN=john@example.com;X-NUM-GUESTS=0:mailto:john@example.com
CREATED:20190109T212402Z
DESCRIPTION:Your event content
LAST-MODIFIED:20190109T212440Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Test
TRANSP:OPAQUE
X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
END:VEVENT
END:VCALENDAR