phpemail-attachmentssparkpost

Sending iCal invite over Sparkpost


I am trying to send a calendar invite as an attachment through email using SparkPost but the invite does not open once the email is received. I use two files "calendarinvite.php" to create the invite and "Sendemail.php" to send the email.

the calendarinvite.php file creates the invite properly because when I run the script I can download the invite and open it. however, once it is sent as an attachment I can't open the invite.

Any help would be appreciated.

Sendemail.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' ];


?>

CalendarInvite.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;

?>

Solution

  • This works for me:

    JSON input to Transmission API

    {
        "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"
                }
            ]
        }
    }
    

    Here is a sample CURL command. You will need to put the proper to/from address and your API key.

    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"
                }
            ]
        }
    }
    '
    

    This is the raw calendar data before 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