You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

email.rake 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. namespace :redmine do
  18. namespace :email do
  19. desc <<-END_DESC
  20. Read an email from standard input.
  21. General options:
  22. unknown_user=ACTION how to handle emails from an unknown user
  23. ACTION can be one of the following values:
  24. ignore: email is ignored (default)
  25. accept: accept as anonymous user
  26. create: create a user account
  27. no_permission_check=1 disable permission checking when receiving
  28. the email
  29. Issue attributes control options:
  30. project=PROJECT identifier of the target project
  31. status=STATUS name of the target status
  32. tracker=TRACKER name of the target tracker
  33. category=CATEGORY name of the target category
  34. priority=PRIORITY name of the target priority
  35. allow_override=ATTRS allow email content to override attributes
  36. specified by previous options
  37. ATTRS is a comma separated list of attributes
  38. Examples:
  39. # No project specified. Emails MUST contain the 'Project' keyword:
  40. rake redmine:email:read RAILS_ENV="production" < raw_email
  41. # Fixed project and default tracker specified, but emails can override
  42. # both tracker and priority attributes:
  43. rake redmine:email:read RAILS_ENV="production" \\
  44. project=foo \\
  45. tracker=bug \\
  46. allow_override=tracker,priority < raw_email
  47. END_DESC
  48. task :read => :environment do
  49. options = { :issue => {} }
  50. %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
  51. options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
  52. options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
  53. options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
  54. MailHandler.receive(STDIN.read, options)
  55. end
  56. desc <<-END_DESC
  57. Read emails from an IMAP server.
  58. General options:
  59. unknown_user=ACTION how to handle emails from an unknown user
  60. ACTION can be one of the following values:
  61. ignore: email is ignored (default)
  62. accept: accept as anonymous user
  63. create: create a user account
  64. no_permission_check=1 disable permission checking when receiving
  65. the email
  66. Available IMAP options:
  67. host=HOST IMAP server host (default: 127.0.0.1)
  68. port=PORT IMAP server port (default: 143)
  69. ssl=SSL Use SSL? (default: false)
  70. username=USERNAME IMAP account
  71. password=PASSWORD IMAP password
  72. folder=FOLDER IMAP folder to read (default: INBOX)
  73. Issue attributes control options:
  74. project=PROJECT identifier of the target project
  75. status=STATUS name of the target status
  76. tracker=TRACKER name of the target tracker
  77. category=CATEGORY name of the target category
  78. priority=PRIORITY name of the target priority
  79. allow_override=ATTRS allow email content to override attributes
  80. specified by previous options
  81. ATTRS is a comma separated list of attributes
  82. Processed emails control options:
  83. move_on_success=MAILBOX move emails that were successfully received
  84. to MAILBOX instead of deleting them
  85. move_on_failure=MAILBOX move emails that were ignored to MAILBOX
  86. Examples:
  87. # No project specified. Emails MUST contain the 'Project' keyword:
  88. rake redmine:email:receive_imap RAILS_ENV="production" \\
  89. host=imap.foo.bar username=redmine@example.net password=xxx
  90. # Fixed project and default tracker specified, but emails can override
  91. # both tracker and priority attributes:
  92. rake redmine:email:receive_imap RAILS_ENV="production" \\
  93. host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
  94. project=foo \\
  95. tracker=bug \\
  96. allow_override=tracker,priority
  97. END_DESC
  98. task :receive_imap => :environment do
  99. imap_options = {:host => ENV['host'],
  100. :port => ENV['port'],
  101. :ssl => ENV['ssl'],
  102. :username => ENV['username'],
  103. :password => ENV['password'],
  104. :folder => ENV['folder'],
  105. :move_on_success => ENV['move_on_success'],
  106. :move_on_failure => ENV['move_on_failure']}
  107. options = { :issue => {} }
  108. %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
  109. options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
  110. options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
  111. options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
  112. Redmine::IMAP.check(imap_options, options)
  113. end
  114. desc <<-END_DESC
  115. Read emails from an POP3 server.
  116. Available POP3 options:
  117. host=HOST POP3 server host (default: 127.0.0.1)
  118. port=PORT POP3 server port (default: 110)
  119. username=USERNAME POP3 account
  120. password=PASSWORD POP3 password
  121. apop=1 use APOP authentication (default: false)
  122. delete_unprocessed=1 delete messages that could not be processed
  123. successfully from the server (default
  124. behaviour is to leave them on the server)
  125. See redmine:email:receive_imap for more options and examples.
  126. END_DESC
  127. task :receive_pop3 => :environment do
  128. pop_options = {:host => ENV['host'],
  129. :port => ENV['port'],
  130. :apop => ENV['apop'],
  131. :username => ENV['username'],
  132. :password => ENV['password'],
  133. :delete_unprocessed => ENV['delete_unprocessed']}
  134. options = { :issue => {} }
  135. %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
  136. options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
  137. options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
  138. options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
  139. Redmine::POP3.check(pop_options, options)
  140. end
  141. desc "Send a test email to the user with the provided login name"
  142. task :test, [:login] => :environment do |task, args|
  143. include Redmine::I18n
  144. abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
  145. user = User.find_by_login(args[:login])
  146. abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
  147. ActionMailer::Base.raise_delivery_errors = true
  148. begin
  149. Mailer.with_synched_deliveries do
  150. Mailer.test_email(user).deliver
  151. end
  152. puts l(:notice_email_sent, user.mail)
  153. rescue Exception => e
  154. abort l(:notice_email_error, e.message)
  155. end
  156. end
  157. end
  158. end