destinations.email.inc

  1. nittany7 modules/contrib/backup_migrate/includes/destinations.email.inc
  2. mooc7 modules/contrib/backup_migrate/includes/destinations.email.inc

Functions to handle the email backup destination.

Functions

Namesort descending Description
_backup_migrate_destination_email_mail_backup @function Temporary mail handler class.

Classes

Namesort descending Description
backup_migrate_destination_email A destination for emailing database backups.
mime_mail

File

modules/contrib/backup_migrate/includes/destinations.email.inc
View source
  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. var $supported_ops = array('scheduled backup', 'manual backup', 'configure');
  13. /**
  14. * Save to (ie. email the file) to the email destination.
  15. */
  16. 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. 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 sever 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. 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. class mime_mail {
  80. var $parts;
  81. var $to;
  82. var $from;
  83. var $headers;
  84. var $subject;
  85. var $body;
  86. function mime_mail() {
  87. $this->parts = array();
  88. $this->to = "";
  89. $this->from = "";
  90. $this->headers = "";
  91. $this->subject = "";
  92. $this->body = "";
  93. }
  94. function add_attachment($message, $name = "", $ctype = "application/octet-stream", $encode = NULL, $attach = FALSE) {
  95. $this->parts[] = array(
  96. "ctype" => $ctype,
  97. "message" => $message,
  98. "encode" => $encode,
  99. "name" => $name,
  100. "attach" => $attach,
  101. );
  102. }
  103. function build_message($part) {
  104. $message = $part["message"];
  105. $message = chunk_split(base64_encode($message));
  106. $encoding = "base64";
  107. $disposition = $part['attach'] ? "Content-Disposition: attachment; filename=$part[name]\n" : '';
  108. return "Content-Type: ". $part["ctype"] . ($part["name"] ? "; name = \"". $part["name"] ."\"" : "") ."\nContent-Transfer-Encoding: $encoding\n$disposition\n$message\n";
  109. }
  110. function build_multipart() {
  111. $boundary = "b". md5(uniqid(time()));
  112. $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
  113. for ($i = sizeof($this->parts) - 1; $i >= 0; $i--) {
  114. $multipart .= "\n". $this->build_message($this->parts[$i]) ."--$boundary";
  115. }
  116. return $multipart .= "--\n";
  117. }
  118. function send() {
  119. $mime = "";
  120. if (!empty($this->from)) $mime .= "From: ". $this->from ."\n";
  121. if (!empty($this->headers)) $mime .= $this->headers ."\n";
  122. if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain");
  123. $mime .= "MIME-Version: 1.0\n". $this->build_multipart();
  124. mail(trim($this->to), $this->subject, "", $mime);
  125. }
  126. }