summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/Transmission.php
blob: a37a13cf40d0e0884e4e10a6fd3ba0c28a8a200c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace SparkPost;

class Transmission extends Resource
{
    protected $customHeaders = array();

    public function __construct(SparkPost $sparkpost)
    {
        parent::__construct($sparkpost, 'transmissions');
    }

    public function fixBlindCarbonCopy($payload)
    {
        //TODO: Manage recipients. "Vincent Song <vincentsong@sparkpost.com>"
        
        $modifiedPayload = $payload;
        $bccList = &$modifiedPayload['bcc'];
        $recipientsList = &$modifiedPayload['recipients'];
        
        //Format: Original Recipient" <original.recipient@example.com>
        //if a name exists, then do "name" <email>. Otherwise, just do <email>
        if(isset($modifiedPayload['recipients'][0]['name']))
        {
            $originalRecipient = '"' . $modifiedPayload['recipients'][0]['name'] 
                . '" <' . $modifiedPayload['recipients'][0]['address'] . '>';
        } else {
            $originalRecipient = '<' . $modifiedPayload['recipients'][0]['address'] 
                . '>';
        }

        //loop through all BCC recipients
        if(isset($bccList)){
            foreach ($bccList as $bccRecipient) { 
                $newRecipient = [
                        'address' => $bccRecipient['address'],
                        'header_to' => $originalRecipient,
                ];
                array_push($recipientsList, $newRecipient);
            }
        }
        
        //Delete the BCC object/array
        unset($modifiedPayload['bcc']); 

        return $modifiedPayload;
    }

    public function fixCarbonCopy($payload)
    {
        $ccRecipients = array();
        $modifiedPayload = $payload;
        $ccList = &$modifiedPayload['cc'];
        $recipientsList = &$modifiedPayload['recipients'];
        
        //If a name exists, then use format "name" <email>. Otherwise, use format <email>
        if(isset($modifiedPayload['recipients'][0]['name'])) {
            $originalRecipient = '"' . $modifiedPayload['recipients'][0]['name'] 
                . '" <' . $modifiedPayload['recipients'][0]['address'] . '>';
        } else {
            $originalRecipient =  $modifiedPayload['recipients'][0]['address'];
        }
        
        if(isset($ccList)){
             foreach ($ccList as $ccRecipient) {
                $newRecipient = [
                        'address' => $ccRecipient['address'],
                        'header_to' => $originalRecipient,
                ];
                //if name exists, then use "Name" <Email> format. Otherwise, just email will suffice. 
                if(is_array($ccRecipient['address'])) {
                    $ccRecipientData = ' "' . $ccRecipient['address']['name'] . '" ' . '<' . $ccRecipient['address']['email'] . '>';
                    
                } else {
                    $ccRecipientData = $ccRecipient['address'];
                }
                array_push($ccRecipients, $ccRecipientData);
                array_push($recipientsList, $newRecipient);
            }

            if(!empty($ccRecipients)){ //If there are CC'd people
                $this->customHeaders = array("CC" => implode(',', $ccRecipients));
            }
            
            //create new object 'headers' under content 
            $content = &$modifiedPayload['content'];
            $content['headers'] = $this->customHeaders;
            
        }
        
        //delete CC
        unset($modifiedPayload['cc']);
        
        return $modifiedPayload;
    }

    public function post($payload)
    {
        $modifiedPayload = $this->fixBlindCarbonCopy($payload); //Fixes BCCs into payload
        $modifiedPayload = $this->fixCarbonCopy($modifiedPayload); //Fixes CCs into payload
        return parent::post($modifiedPayload, $this->customHeaders);
    }
}

?>