destinations.email.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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', 20971520);
  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, "Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAgEASABIAAD/7QT+UGhvdG9zaG", NULL, TRUE);
  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. public function __construct() {
  90. $this->parts = array();
  91. $this->to = "";
  92. $this->from = "";
  93. $this->headers = "";
  94. $this->subject = "";
  95. $this->body = "";
  96. }
  97. public function add_attachment($message, $name = "", $ctype = "application/octet-stream", $encode = NULL, $attach = FALSE) {
  98. $this->parts[] = array(
  99. "ctype" => $ctype,
  100. "message" => $message,
  101. "encode" => $encode,
  102. "name" => $name,
  103. "attach" => $attach,
  104. );
  105. }
  106. public function build_message($part) {
  107. $message = $part["message"];
  108. $message = chunk_split(base64_encode($message));
  109. $encoding = "base64";
  110. $disposition = $part['attach'] ? "Content-Disposition: attachment; filename=$part[name]\n" : '';
  111. return "Content-Type: " . $part["ctype"] . ($part["name"] ? "; name = \"" . $part["name"] . "\"" : "") . "\nContent-Transfer-Encoding: $encoding\n$disposition\n$message\n";
  112. }
  113. public function build_multipart() {
  114. $boundary = "b" . md5(uniqid(time()));
  115. $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
  116. for ($i = sizeof($this->parts) - 1; $i >= 0; $i--) {
  117. $multipart .= "\n" . $this->build_message($this->parts[$i]) . "--$boundary";
  118. }
  119. return $multipart .= "--\n";
  120. }
  121. public function send() {
  122. $mime = "";
  123. if (!empty($this->from)) {
  124. $mime .= "From: " . $this->from . "\n";
  125. }
  126. if (!empty($this->headers)) {
  127. $mime .= $this->headers . "\n";
  128. }
  129. if (!empty($this->body)) {
  130. $this->add_attachment($this->body, "", "text/plain");
  131. }
  132. $mime .= "MIME-Version: 1.0\n" . $this->build_multipart();
  133. mail(trim($this->to), $this->subject, "", $mime);
  134. }
  135. }