6 * Functions for message compositon: writing a message, attaching files etc.
8 * @author Thijs Kinkhorst <kink at squirrelmail.org>
9 * @copyright 1999-2020 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
17 * Get a new file to write an attachment to.
18 * This function makes sure it doesn't overwrite other attachments,
19 * preventing collisions and race conditions.
21 * @return filename of the tempfile only (not full path)
24 function sq_get_attach_tempfile()
26 global $username, $attachment_dir;
28 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
30 // using PHP >= 4.3.2 we can be truly atomic here
31 $filemods = check_php_version ( 4,3,2 ) ?
'x' : 'w';
33 // give up after 1000 tries
35 for ($try=0; $try<$TMP_MAX; ++
$try) {
37 $localfilename = GenerateRandomString(32, '', 7);
38 $full_localfilename = "$hashed_attachment_dir/$localfilename";
40 // filename collision. try again
41 if ( file_exists($full_localfilename) ) {
45 // try to open for (binary) writing
46 $fp = @fopen
( $full_localfilename, $filemods);
48 if ( $fp !== FALSE ) {
49 // success! make sure it's not readable, close and return filename
50 chmod($full_localfilename, 0600);
52 return $localfilename;
56 // we tried 1000 times but didn't succeed.
57 error_box( _("Could not open temporary file to store attachment. Contact your system administrator to resolve this issue.") );
63 * Send a simple mail message using SquirrelMail's API.
65 * Until SquirrelMail is sufficiently redesigned, this
66 * function is a stand-in for a simple mail delivery
67 * call. Currently, it only sends plaintext messages
68 * (unless the caller uses the $message parameter).
70 * @param string $to The destination recipient.
71 * @param string $subject The message subject.
72 * @param string $body The message body.
73 * @param string $from The sender.
74 * @param string $cc The destination carbon-copy recipient.
75 * (OPTIONAL; default no Cc:)
76 * @param string $bcc The destination blind carbon-copy recipient.
77 * (OPTIONAL; default no Bcc:)
78 * @param object $message If the caller wants to construct a more
79 * complicated message themselves and pass
80 * it here, this function will take care
81 * of the rest - handing it over to SMTP
82 * or Sendmail. If this parameter is non-
83 * empty, all other parameters are ignored.
84 * (OPTIONAL: default is empty)
85 * @param boolean $only_build_message_object When TRUE, only builds the
86 * message object that it
87 * intends to send and returns
88 * it (returned success code
89 * will be -1 and message ID
90 * emtpy) (OPTIONAL; default
93 * @return array A three-element array, the first element being a
94 * boolean value indicating if the message was successfully
95 * sent or not, the second element being the message's
96 * assigned Message-ID, if available (only available as of
97 * SquirrelMail 1.4.14 and 1.5.2), and the third element
98 * being the message object itself.
99 * If $only_build_message_object is TRUE, only the third
100 * element is useful; first two should be ignored - the
101 * message is never sent in this case.
104 function sq_send_mail($to, $subject, $body, $from, $cc='', $bcc='',
105 $message='', $only_build_message_object=FALSE)
108 require_once(SM_PATH
. 'functions/mime.php');
109 require_once(SM_PATH
. 'class/mime.class.php');
113 $message = new Message();
114 $header = new Rfc822Header();
116 $message->setBody($body);
117 $content_type = new ContentType('text/plain');
118 global $special_encoding, $default_charset;
119 if ($special_encoding)
120 $header->encoding
= $special_encoding;
122 $header->encoding
= '8bit';
123 if ($default_charset)
124 $content_type->properties
['charset']=$default_charset;
125 $header->content_type
= $content_type;
127 $header->parseField('To', $to);
128 $header->parseField('Cc', $cc);
129 $header->parseField('Bcc', $bcc);
130 $header->parseField('From', $from);
131 $header->parseField('Subject', $subject);
132 $message->rfc822_header
= $header;
134 //sm_print_r($message);exit;
137 if ($only_build_message_object)
138 return array(-1, '', $message);
144 // ripped from src/compose.php - based on both 1.5.2 and 1.4.14
147 require_once(SM_PATH
. 'class/deliver/Deliver_SMTP.class.php');
148 $deliver = new Deliver_SMTP();
149 global $smtpServerAddress, $smtpPort, $pop_before_smtp,
150 $domain, $pop_before_smtp_host, $smtp_stream_options;
152 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ?
true : false;
153 if (empty($pop_before_smtp_host)) $pop_before_smtp_host = $smtpServerAddress;
156 get_smtp_user($user, $pass);
157 $stream = $deliver->initStream($message,$domain,0,
158 $smtpServerAddress, $smtpPort, $user, $pass, $authPop, $pop_before_smtp_host, $smtp_stream_options);
160 require_once(SM_PATH
. 'class/deliver/Deliver_SendMail.class.php');
161 global $sendmail_path, $sendmail_args;
162 // Check for outdated configuration
163 if (!isset($sendmail_args)) {
164 if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
167 $sendmail_args = '-i -t';
170 $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
171 $stream = $deliver->initStream($message,$sendmail_path);
178 $deliver->mail($message, $stream);
179 if (!empty($message->rfc822_header
->message_id
)) {
180 $message_id = $message->rfc822_header
->message_id
;
183 $success = $deliver->finalizeStream($stream);
186 return array($success, $message_id, $message);