001.
public
function
AddAttachment(
$path
,
$name
=
''
,
$encoding
=
'base64'
,
$type
=
'application/octet-stream'
) {
002.
try {
003.
if
( !@
is_file
(
$path
) )
004.
{
005.
throw
new
phpmailerException(
$this
->Lang(
'file_access'
) .
$path
, self::STOP_CONTINUE);
006.
}
007.
$filename
=
basename
(
$path
);
008.
if
(
$name
==
''
)
009.
{
010.
$name
=
$filename
;
011.
}
012.
013.
$this
->attachment[] =
array
(
014.
0 =>
$path
,
015.
1 =>
$filename
,
016.
2 =>
$name
,
017.
3 =>
$encoding
,
018.
4 =>
$type
,
019.
5 => false,
020.
6 =>
'attachment'
,
021.
7 => 0
022.
);
023.
024.
} catch (phpmailerException
$e
) {
025.
$this
->SetError(
$e
->getMessage());
026.
if
(
$this
->exceptions) {
027.
throw
$e
;
028.
}
029.
echo
$e
->getMessage().
"\n"
;
030.
if
(
$e
->getCode() == self::STOP_CRITICAL ) {
031.
return
false;
032.
}
033.
}
034.
return
true;
035.
}
036.
037.
038.
039.
040.
041.
public
function
GetAttachments() {
042.
return
$this
->attachment;
043.
}
044.
045.
046.
047.
048.
049.
050.
051.
private
function
AttachAll() {
052.
053.
$mime
=
array
();
054.
$cidUniq
=
array
();
055.
$incl
=
array
();
056.
057.
058.
foreach
(
$this
->attachment
as
$attachment
) {
059.
060.
$bString
=
$attachment
[5];
061.
if
(
$bString
) {
062.
$string
=
$attachment
[0];
063.
}
else
{
064.
$path
=
$attachment
[0];
065.
}
066.
067.
if
(in_array(
$attachment
[0],
$incl
)) {
continue
; }
068.
$filename
=
$attachment
[1];
069.
$name
=
$attachment
[2];
070.
$encoding
=
$attachment
[3];
071.
$type
=
$attachment
[4];
072.
$disposition
=
$attachment
[6];
073.
$cid
=
$attachment
[7];
074.
$incl
[] =
$attachment
[0];
075.
if
(
$disposition
==
'inline'
&& isset(
$cidUniq
[
$cid
]) ) {
continue
; }
076.
$cidUniq
[
$cid
] = true;
077.
078.
$mime
[] = sprintf(
"--%s%s"
,
$this
->boundary[1],
$this
->LE);
079.
$mime
[] = sprintf(
"Content-Type: %s; name=\"%s\"%s"
,
$type
,
$this
->EncodeHeader(
$this
->SecureHeader(
$name
)),
$this
->LE);
080.
$mime
[] = sprintf(
"Content-Transfer-Encoding: %s%s"
,
$encoding
,
$this
->LE);
081.
082.
if
(
$disposition
==
'inline'
) {
083.
$mime
[] = sprintf(
"Content-ID: <%s>%s"
,
$cid
,
$this
->LE);
084.
}
085.
086.
$mime
[] = sprintf(
"Content-Disposition: %s; filename=\"%s\"%s"
,
$disposition
,
$this
->EncodeHeader(
$this
->SecureHeader(
$name
)),
$this
->LE.
$this
->LE);
087.
088.
089.
if
(
$bString
) {
090.
$mime
[] =
$this
->EncodeString(
$string
,
$encoding
);
091.
if
(
$this
->IsError()) {
092.
return
''
;
093.
}
094.
$mime
[] =
$this
->LE.
$this
->LE;
095.
}
else
{
096.
$mime
[] =
$this
->EncodeFile(
$path
,
$encoding
);
097.
if
(
$this
->IsError()) {
098.
return
''
;
099.
}
100.
$mime
[] =
$this
->LE.
$this
->LE;
101.
}
102.
}
103.
104.
$mime
[] = sprintf(
"--%s--%s"
,
$this
->boundary[1],
$this
->LE);
105.
106.
return
join(
''
,
$mime
);
107.
}
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
private
function
EncodeFile(
$path
,
$encoding
=
'base64'
) {
119.
try {
120.
if
(!
is_readable
(
$path
)) {
121.
throw
new
phpmailerException(
$this
->Lang(
'file_open'
) .
$path
, self::STOP_CONTINUE);
122.
}
123.
if
(function_exists(
'get_magic_quotes'
)) {
124.
function
get_magic_quotes() {
125.
return
false;
126.
}
127.
}
128.
if
(PHP_VERSION < 6) {
129.
$magic_quotes
= get_magic_quotes_runtime();
130.
set_magic_quotes_runtime(0);
131.
}
132.
$file_buffer
=
file_get_contents
(
$path
);
133.
$file_buffer
=
$this
->EncodeString(
$file_buffer
,
$encoding
);
134.
if
(PHP_VERSION < 6) { set_magic_quotes_runtime(
$magic_quotes
); }
135.
return
$file_buffer
;
136.
} catch (Exception
$e
) {
137.
$this
->SetError(
$e
->getMessage());
138.
return
''
;
139.
}
140.
}
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
public
function
EncodeString (
$str
,
$encoding
=
'base64'
) {
151.
$encoded
=
''
;
152.
switch
(
strtolower
(
$encoding
)) {
153.
case
'base64'
:
154.
$encoded
=
chunk_split
(
base64_encode
(
$str
), 76,
$this
->LE);
155.
break
;
156.
case
'7bit'
:
157.
case
'8bit'
:
158.
$encoded
=
$this
->FixEOL(
$str
);
159.
160.
if
(
substr
(
$encoded
, -(
strlen
(
$this
->LE))) !=
$this
->LE)
161.
$encoded
.=
$this
->LE;
162.
break
;
163.
case
'binary'
:
164.
$encoded
=
$str
;
165.
break
;
166.
case
'quoted-printable'
:
167.
$encoded
=
$this
->EncodeQP(
$str
);
168.
break
;
169.
default
:
170.
$this
->SetError(
$this
->Lang(
'encoding'
) .
$encoding
);
171.
break
;
172.
}
173.
return
$encoded
;
174.
}
175.
176.
177.
178.
179.
180.
181.
public
function
EncodeHeader(
$str
,
$position
=
'text'
) {
182.
$x
= 0;
183.
184.
switch
(
strtolower
(
$position
)) {
185.
case
'phrase'
:
186.
if
(!preg_match(
'/[\200-\377]/'
,
$str
)) {
187.
188.
$encoded
=
addcslashes
(
$str
,
"\0..\37\177\\\""
);
189.
if
((
$str
==
$encoded
) && !preg_match(
'/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/'
,
$str
)) {
190.
return
(
$encoded
);
191.
}
else
{
192.
return
(
"\"$encoded\""
);
193.
}
194.
}
195.
$x
= preg_match_all(
'/[^\040\041\043-\133\135-\176]/'
,
$str
,
$matches
);
196.
break
;
197.
case
'comment'
:
198.
$x
= preg_match_all(
'/[()"]/'
,
$str
,
$matches
);
199.
200.
case
'text'
:
201.
default
:
202.
$x
+= preg_match_all(
'/[\000-\010\013\014\016-\037\177-\377]/'
,
$str
,
$matches
);
203.
break
;
204.
}
205.
206.
if
(
$x
== 0) {
207.
return
(
$str
);
208.
}
209.
210.
$maxlen
= 75 - 7 -
strlen
(
$this
->CharSet);
211.
212.
if
(
strlen
(
$str
)/3 <
$x
) {
213.
$encoding
=
'B'
;
214.
if
(function_exists(
'mb_strlen'
) &&
$this
->HasMultiBytes(
$str
)) {
215.
216.
217.
$encoded
=
$this
->Base64EncodeWrapMB(
$str
);
218.
}
else
{
219.
$encoded
=
base64_encode
(
$str
);
220.
$maxlen
-=
$maxlen
% 4;
221.
$encoded
= trim(
chunk_split
(
$encoded
,
$maxlen
,
"\n"
));
222.
}
223.
}
else
{
224.
$encoding
=
'Q'
;
225.
$encoded
=
$this
->EncodeQ(
$str
,
$position
);
226.
$encoded
=
$this
->WrapText(
$encoded
,
$maxlen
, true);
227.
$encoded
=
str_replace
(
'='
.
$this
->LE,
"\n"
, trim(
$encoded
));
228.
}
229.
230.
$encoded
= preg_replace(
'/^(.*)$/m'
,
" =?"
.
$this
->CharSet.
"?$encoding?\\1?="
,
$encoded
);
231.
$encoded
= trim(
str_replace
(
"\n"
,
$this
->LE,
$encoded
));
232.
233.
return
$encoded
;
234.
}