HtmlEmailBuilder::getHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 42
rs 9.536
1
<?php
2
3
/**
4
 *  ____  _     _                       _ _
5
 * |  _ \| |__ | |_ ___ _ __ ___   __ _(_) |
6
 * | |_) | '_ \| __/ _ \ '_ ` _ \ / _` | | |
7
 * |  __/| | | | ||  __/ | | | | | (_| | | |
8
 * |_|   |_| |_|\__\___|_| |_| |_|\__,_|_|_|
9
 *
10
 * This file is part of Kristuff\Phtemail.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.2.0
18
 * @copyright  2017-2020 Kristuff
19
 */
20
21
namespace Kristuff\Phtemail;
22
23
/**  
24
 * Main class to build html email
25
 * Extends the HtmlHeadBuilder class that extends the HtmlBaseBuilder
26
 * 
27
 */
28
class HtmlEmailBuilder extends \Kristuff\Phtemail\Core\HtmlHeadBuilder
29
{
30
    /** 
31
     * The email meta title 
32
     * 
33
     * @access private
34
     * @var string  $emailTitle
35
     */
36
    private $emailTitle = '';
37
38
    /** 
39
     * The email body container 
40
     * 
41
     * @access private
42
     * @var EmailBodyContainer      $bodyContainer
43
     */
44
    private $bodyContainer = null;
45
46
    /** 
47
     * The main email header container 
48
     * 
49
     * @access private
50
     * @var EmailHeaderContainer    $headerContainer
51
     */
52
    private $headerContainer = null;
53
54
    /** 
55
     * The main email footer container
56
     * 
57
     * @access private
58
     * @var EmailFooterContainer    $footerContainer
59
     */
60
    private $footerContainer = null;
61
62
    /**
63
     * Constructor
64
     */
65
    public function __construct()
66
    {
67
        $this->headerContainer  = new EmailHeaderContainer($this);
68
        $this->bodyContainer    = new EmailBodyContainer($this);
69
        $this->footerContainer  = new EmailFooterContainer($this);
70
71
        // default styles
72
        $this->headerContainer->setAlign(self::H_ALIGN_CENTER);
73
        $this->footerContainer->setAlign(self::H_ALIGN_CENTER);
74
    }
75
76
    /** 
77
     * Sets the email title 
78
     * 
79
     * @access public 
80
     * @param string    $title      The email title in meta
81
     * 
82
     * @return void
83
     */
84
    public function setTitle(string $title)
85
    {
86
        $this->emailTitle = $title;
87
    }
88
89
    /** 
90
     * Sets dark backside color and background. This doesn't affect
91
     * the default email body color and background  
92
     * 
93
     * @access public 
94
     * @param string    $title      The email title in meta
95
     * 
96
     * @return void
97
     */
98
    public function setBacksideDarkTheme()
99
    {
100
        $this->setBacksideBackgroundColor(self::COLOR_DARKGRAY);
101
        $this->setBacksideColor(self::COLOR_GRAY_500);
102
    }
103
104
    /** 
105
     * Gets the email body container element
106
     * 
107
     * @access public 
108
     * @return EmailBodyContainer  
109
     */
110
    public function body()
111
    {
112
        return $this->bodyContainer;
113
    }
114
    
115
    /** 
116
     * Gets the email header container element
117
     * 
118
     * @access public 
119
     * @return EmailHeaderContainer
120
     */
121
    public function header()
122
    {
123
        return $this->headerContainer;
124
    }
125
126
    /** 
127
     * Gets the email footer container element
128
     * 
129
     * @access public 
130
     * @return EmailFooterContainer  
131
     */
132
    public function footer()
133
    {
134
        return $this->footerContainer;
135
    }
136
137
    /** 
138
     * Gets the html mail as string
139
     * 
140
     * @access public
141
     * @return string 
142
     */
143
    public function getHtml()
144
    {
145
        /** 
146
         * set doctype / open html tags
147
         * @see https://www.campaignmonitor.com/blog/email-marketing/2010/11/correct-doctype-to-use-in-html-email/
148
         */
149
        $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . PHP_EOL;
150
        $html .= '<html xmlns="http://www.w3.org/1999/xhtml">' . PHP_EOL;
151
152
        // head (css styles, meta, title)
153
        $html .= '<head>' . PHP_EOL;
154
        $html .= $this->getMeta();
155
        $html .= $this->getTitle($this->emailTitle);
156
        $html .= $this->getStyles();
157
        $html .= '</head>' . PHP_EOL;
158
159
        // start body (document body, not the email body...), center table
160
        $html .= '<body bgcolor="'. $this->backsideBackgroundColor  .'" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">'. PHP_EOL;
161
        
162
        $html .= $this->getHtmlComment('CENTER THE EMAIL //', '  ');
163
        
164
        $html .= '  <center style="background-color:'. $this->backsideBackgroundColor  .';">'. PHP_EOL;
165
        $html .= '    <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable" style="table-layout: fixed;max-width:100% !important;width: 100% !important;min-width: 100% !important;">'. PHP_EOL;
166
        $html .= '      <tr>'. PHP_EOL;
167
        $html .= '        <td align="center" valign="top" id="bodyCell">'. PHP_EOL;
168
169
        // render child elements collection
170
        $html .= $this->headerContainer->getHtml('          ');    
171
        $html .= $this->bodyContainer->getHtml('          ');    
172
        $html .= $this->footerContainer->getHtml('          ');    
173
174
        // close centered table
175
        $html .= '        </td>'. PHP_EOL;
176
        $html .= '      </tr>'. PHP_EOL;
177
        $html .= '    </table>'. PHP_EOL;
178
        $html .= $this->getHtmlComment('// CENTER THE EMAIL', '  ');
179
        $html .= '  </center>'. PHP_EOL;
180
181
        // close body and html tags and returns html
182
        $html .= '</body>' . PHP_EOL;
183
        $html .= '</html>';
184
        return $html;
185
    }
186
    
187
}