forked from JorgenEvens/WebLab_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMail.php
More file actions
157 lines (131 loc) · 5.07 KB
/
Mail.php
File metadata and controls
157 lines (131 loc) · 5.07 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
*
* Mail wrapper
*
* Send mails based on WebLab_Template.
* Joins images into the e-mail for optimal viewing.
*
* @author Jorgen Evens <jorgen@wlab.be>
* @version 0.1
*
*/
class WebLab_Mail extends WebLab_Template
{
// TODO: Set resources directory.
/**
* Receiver of the e-mail.
* @var String The e-mailaddress of the receiver.
*/
protected $_to;
/**
* The sender of the e-mail.
* @var String The e-mailaddress of the sender.
*/
protected $_from;
/**
* The subject of the e-mail.
* @var String The subject of the e-mail.
*/
protected $_subject;
/**
* The boundary splitting multiple content types.
* @var String The boundary used to split content types.
*/
protected $_boundary;
/**
* Defines primary content type
* @var String Defaults to text/html
*/
protected $_content_type = 'text/html';
/**
* Generates a new Mail object.
* @param String $template The template path
* @param String $to The recipients
* @param String $subject The subject of this e-mail. Defaults to 'Automated mail'
* @param String $from From who the receiver gets this message. Defaults to 'admin@server'
*/
public function __construct( $template, $to, $subject='Automated mail', $from='admin@server', $content_type='text/html' ){
parent::__construct($template);
if( !is_array( $to ) )
$to = explode( ',', $to );
$this->_to = $to;
$this->_subject = $subject;
$this->_from = $from;
$this->_content_type = $content_type;
}
/**
* Generates the body of the Mail.
* @param bool $show Output result of this function to the browser. Defaults to false.
* @return String The body of the e-mail.
*/
public function render( $show=false )
{
$code = parent::render();
$code = $this->_parseTemplate($code);
if( $show )
echo $code;
return $code;
}
/**
* Parses the code generate by the template for images.
* @param String $html_code The HTML code to parse.
* @return String The HTML code parsed to include images.
*/
protected function _parseTemplate( $html_code ){
$regExp = "/(background|src)=\"(.*)\.(jpeg|png|gif|jpg)\"/i";
preg_match( $regExp, $html_code, $matches );
$images = array();
foreach( $matches as $match ){
$html_code = str_replace( $match[2], 'cid:img' . count( $images ), $html_code);
$images[count($images)] = $match[2];
}
if( empty( $images ) ) {
return $html_code;
} else {
$this->_boundary = md5( uniqid( time() ) );
$html = '--' . $this->_boundary . "\n";
$html .= 'Content-Type: ' . $this->_content_type . ';' ."\n\n" . $html_code;
foreach( $images as $key => $image )
{
$html .= '--' . $this->_boundary . "\n";
$img = file_get_contents( $image );
$name = basename( $image );
$contentType = array_pop( explode( '.', $name ) );
$html .= 'Content-Type: image/' . $contentType . '; name="' . $name . '"' . "\n" .
'Content-ID: <img' . $key . '>' . "\n" .
'Content-Transfer-Encoding: base64' . "\n" .
'Content-Disposition: inline' . "\n\n";
$html .= chunk_split( base64_encode( $img ), 68, "\n" );
$html .= "\n";
}
$html = trim( $html, "\n" );
$html .= "\n\n" . '--' . $this->_boundary . '--' . "\n";
}
return $html;
}
/**
* Transmits the e-mail to all the receivers.
* @return bool Indicates whether the send operation was successful.
*/
public function send(){
if( !empty( $this->_from ) ) {
$headers = 'FROM:' . $this->_from . "\n";
if( !empty( $this->_boundary ) ) {
$headers .= 'Content-Type: multipart/related; ' .
'boundary=' . $this->_boundary . "\n";
} else {
$headers .= 'Content-Type: ' . $this->_content_type . "\n";
}
}
$content = $this->render();
$error_address = array();
foreach( $this->_to as $email ){
if( !mail( $email, $this->_subject, $content, $headers ) )
$error_address[] = $email;
}
if( count( $error_address ) > 0 )
return $error_address;
return true;
}
}