destinations.email.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * Functions to handle the email backup destination.
  5. */
  6. /**
  7. * A destination for emailing database backups.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_email extends backup_migrate_destination {
  12. public $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'configure');
  13. /**
  14. * Save to (ie. email the file) to the email destination.
  15. */
  16. public function save_file($file, $settings) {
  17. $size = filesize($file->filepath());
  18. $max = variable_get('backup_migrate_max_email_size', BACKUP_MIGRATE_MAX_EMAIL_SIZE);
  19. if ($size > $max) {
  20. _backup_migrate_message('Could not email the file @file because it is @size and Backup and Migrate only supports emailing files smaller than @max.', array('@file' => $file->filename(), '@size' => format_size($size), '@max' => format_size($max)), 'error');
  21. return FALSE;
  22. }
  23. $attachment = new stdClass();
  24. $attachment->filename = $file->filename();
  25. $attachment->path = $file->filepath();
  26. _backup_migrate_destination_email_mail_backup($attachment, $this->get_location());
  27. return $file;
  28. }
  29. /**
  30. * Get the form for the settings for this filter.
  31. */
  32. public function edit_form() {
  33. $form = parent::edit_form();
  34. $form['location'] = array(
  35. "#type" => "textfield",
  36. "#title" => t("Email Address"),
  37. "#default_value" => $this->get_location(),
  38. "#required" => TRUE,
  39. "#description" => t('Enter the email address to send the backup files to. Make sure the email server can handle large file attachments'),
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Validate the configuration form. Make sure the email address is valid.
  45. */
  46. public function settings_form_validate($values) {
  47. if (!valid_email_address($values['location'])) {
  48. form_set_error('[location]', t('The e-mail address %mail is not valid.', array('%mail' => $form_state['values']['location'])));
  49. }
  50. }
  51. }
  52. /**
  53. * @function
  54. * Temporary mail handler class.
  55. *
  56. * Defines a mail class to send a message with an attachment. Eventually Drupal
  57. * core should provide this functionality, at which time this code will be
  58. * removed.
  59. *
  60. * More info on sending email at <http://php.net/function.mail>.
  61. * This function taken from dba.module.
  62. *
  63. * @param $attachment
  64. * An object which contains two variables "path" the path to the file and
  65. * filename and "filename" which is just the filename.
  66. */
  67. function _backup_migrate_destination_email_mail_backup($attachment, $to) {
  68. // Send mail.
  69. $attach = fread(fopen($attachment->path, "r"), filesize($attachment->path));
  70. $mail = new mime_mail();
  71. $mail->from = variable_get('site_mail', ini_get('sendmail_from'));
  72. $mail->headers = 'Errors-To: [EMAIL=' . $mail->from . ']' . $mail->from . '[/EMAIL]';
  73. $mail->to = $to;
  74. $mail->subject = t('Database backup from !site: !file', array('!site' => variable_get('site_name', 'drupal'), '!file' => $attachment->filename));
  75. $mail->body = t('Database backup attached.') . "\n\n";
  76. $mail->add_attachment($attach, $attachment->filename, "application/octet-stream");
  77. $mail->send();
  78. }
  79. /**
  80. *
  81. */
  82. class mime_mail {
  83. public $parts;
  84. public $to;
  85. public $from;
  86. public $headers;
  87. public $subject;
  88. public $body;
  89. /**
  90. *
  91. */
  92. public function __construct() {
  93. $this->parts = array();
  94. $this->to = "";
  95. $this->from = "";
  96. $this->headers = array();
  97. $this->subject = "";
  98. $this->body = "";
  99. }
  100. /**
  101. *
  102. */
  103. public function add_attachment($message, $name, $ctype) {
  104. $this->parts[] = array(
  105. "message" => $message,
  106. "name" => $name,
  107. "ctype" => $ctype,
  108. );
  109. }
  110. /**
  111. *
  112. */
  113. public function build_message($part) {
  114. $crlf = "\r\n";
  115. // See RFC 2184.
  116. $continuation = $crlf . ' ';
  117. $name = $part['name'];
  118. $len = strlen($name);
  119. // RFC 5322 recommends lines of no longer than 78 chars, which in
  120. // this case comes down to filenames of no longer than 64 chars.
  121. if ($len > 64) {
  122. // We want to preserve the time stamp and extension and such.
  123. $head = substr($name, 0, 28);
  124. $tail = substr($name, ($len - 32));
  125. $name = $head . '___' . $tail;
  126. }
  127. $message = chunk_split(base64_encode($part["message"]), 70, $crlf);
  128. $disposition = $name ? "Content-Disposition: attachment; {$continuation}filename=\"$name\"$crlf" : "";
  129. return "Content-Type: " . $part["ctype"] . ($name ? ";{$continuation}name=\"$name\"" : "") .
  130. "{$crlf}Content-Transfer-Encoding: base64$crlf$disposition$crlf$message";
  131. }
  132. /**
  133. *
  134. */
  135. public function build_multipart($boundary) {
  136. $multipart = "This is a MIME encoded message.\r\n\r\n--$boundary";
  137. for ($i = count($this->parts) - 1; $i >= 0; $i--) {
  138. $multipart .= "\r\n" . $this->build_message($this->parts[$i]) . "--$boundary";
  139. }
  140. return $multipart . "--\r\n";
  141. }
  142. /**
  143. *
  144. */
  145. public function send() {
  146. $headers = array();
  147. if (!empty($this->body)) {
  148. $this->add_attachment($this->body, "", "text/plain");
  149. }
  150. $headers['MIME-Version'] = "1.0";
  151. $boundary = "b" . md5(uniqid(time()));
  152. $headers['Content-Type'] = "multipart/mixed; boundary=\"$boundary\"";
  153. $message = $this->build_multipart($boundary);
  154. $params = array();
  155. $params['body'] = $message;
  156. $params['headers'] = $headers;
  157. $params['subject'] = $this->subject;
  158. drupal_mail('backup_migrate', 'destination_mail', trim($this->to), '', $params, $this->from);
  159. }
  160. }