Skip to content
Newer
Older
100644 126 lines (80 sloc) 2.58 KB
cce87f2 @iamcal added flamework's lib_email
authored Nov 2, 2010
1 <?
2 #
3 # $Id$
4 #
5
6 #########################################################################################
7
8 function email_send($args){
9
10 $headers = array();
11
12 if (is_array($args['headers'])){
13
14 $headers = $args['headers'];
15 }
16
17
18 #
19 # set up the from address
20 #
21
22 if ($args['from_name'] && $args['from_email']){
23
24 $from_email = $args['from_email'];
25 $from_name = $args['from_name'];
26
27 }else if ($args['from_email']){
28
29 $from_email = $args[from_email];
30 $from_name = $args[from_email];
31
32 }else if ($args['from_name']){
33
34 $from_email = $GLOBALS['cfg']['email_from_email'];
35 $from_name = $args['from_name'];
36 }else{
37
38 $from_email = $GLOBALS['cfg']['email_from_email'];
39 $from_name = $GLOBALS['cfg']['email_from_name'];
40 }
41
42 $headers['From'] = "\"".email_quoted_printable($from_name)."\" <$from_email>";
43
44
45 #
46 # other headers
47 #
48
49 if (!$headers['To']){
50 $headers['To'] = $args['to_email'];
51 }
52
53 if (!$headers['Reply-To']){
54 $headers['Reply-To'] = $from_email;
55 }
56
57 if (!$headers['Content-Type']){
58
59 $headers['Content-Type'] = 'text/plain; charset=utf-8';
60 }
61
62
63 #
64 # subject and message come from a smarty template
65 #
66
67 $message = trim($GLOBALS['smarty']->fetch($args['template']));
68 $subject = trim($GLOBALS['smarty']->get_template_vars('email_subject'));
69
70 $message = email_format_body($message);
71 $subject = email_quoted_printable($subject);
72
73
74 #
75 # send via local MTA
76 #
77
78 unset($headers['To']);
79
80 mail($args['to_email'], $subject, $message, email_format_headers($headers), $GLOBALS['cfg']['auto_email_args']);
81 }
82
83 #########################################################################################
84
85 function email_format_body($message){
86
87 $message = str_replace("\r", "", $message);
88 $message = wordwrap($message, 72);
89
90 return $message;
91 }
92
93 #########################################################################################
94
95 function email_quoted_printable($subject){
96
97 if (preg_match('/[^a-z: ]/i', $subject)){
98 $subject = preg_replace_callback('/([^a-z ])/i', 'email_quoted_printable_encode', $subject);
99 $subject = str_replace(' ', '_', $subject);
100 return "=?utf-8?Q?$subject?=";
101 }
102
103 return $subject;
104 }
105
106 function email_quoted_printable_encode($m){
107
108 return sprintf('=%02x', StripSlashes(ord($m[1])));
109 }
110
111 #########################################################################################
112
113 function email_format_headers(&$headers){
114
115 $h2 = array();
116
117 foreach ($headers as $h => $v){
118 $h2[] = "$h: $v";
119 }
120
121 return implode("\r\n", $h2);
122 }
123
124 #########################################################################################
125
126 ?>
Something went wrong with that request. Please try again.