Mirror of PeerTube
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.

emailer.ts 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. import { createTransport, Transporter } from 'nodemailer'
  2. import { isTestInstance } from '../helpers/core-utils'
  3. import { bunyanLogger, logger } from '../helpers/logger'
  4. import { CONFIG } from '../initializers/config'
  5. import { UserModel } from '../models/account/user'
  6. import { VideoModel } from '../models/video/video'
  7. import { JobQueue } from './job-queue'
  8. import { EmailPayload } from './job-queue/handlers/email'
  9. import { readFileSync } from 'fs-extra'
  10. import { VideoCommentModel } from '../models/video/video-comment'
  11. import { VideoAbuseModel } from '../models/video/video-abuse'
  12. import { VideoBlacklistModel } from '../models/video/video-blacklist'
  13. import { VideoImportModel } from '../models/video/video-import'
  14. import { ActorFollowModel } from '../models/activitypub/actor-follow'
  15. import { WEBSERVER } from '../initializers/constants'
  16. type SendEmailOptions = {
  17. to: string[]
  18. subject: string
  19. text: string
  20. fromDisplayName?: string
  21. replyTo?: string
  22. }
  23. class Emailer {
  24. private static instance: Emailer
  25. private initialized = false
  26. private transporter: Transporter
  27. private constructor () {}
  28. init () {
  29. // Already initialized
  30. if (this.initialized === true) return
  31. this.initialized = true
  32. if (Emailer.isEnabled()) {
  33. logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
  34. let tls
  35. if (CONFIG.SMTP.CA_FILE) {
  36. tls = {
  37. ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
  38. }
  39. }
  40. let auth
  41. if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
  42. auth = {
  43. user: CONFIG.SMTP.USERNAME,
  44. pass: CONFIG.SMTP.PASSWORD
  45. }
  46. }
  47. this.transporter = createTransport({
  48. host: CONFIG.SMTP.HOSTNAME,
  49. port: CONFIG.SMTP.PORT,
  50. secure: CONFIG.SMTP.TLS,
  51. debug: CONFIG.LOG.LEVEL === 'debug',
  52. logger: bunyanLogger as any,
  53. ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
  54. tls,
  55. auth
  56. })
  57. } else {
  58. if (!isTestInstance()) {
  59. logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
  60. }
  61. }
  62. }
  63. static isEnabled () {
  64. return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
  65. }
  66. async checkConnectionOrDie () {
  67. if (!this.transporter) return
  68. logger.info('Testing SMTP server...')
  69. try {
  70. const success = await this.transporter.verify()
  71. if (success !== true) this.dieOnConnectionFailure()
  72. logger.info('Successfully connected to SMTP server.')
  73. } catch (err) {
  74. this.dieOnConnectionFailure(err)
  75. }
  76. }
  77. addNewVideoFromSubscriberNotification (to: string[], video: VideoModel) {
  78. const channelName = video.VideoChannel.getDisplayName()
  79. const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
  80. const text = `Hi dear user,\n\n` +
  81. `Your subscription ${channelName} just published a new video: ${video.name}` +
  82. `\n\n` +
  83. `You can view it on ${videoUrl} ` +
  84. `\n\n` +
  85. `Cheers,\n` +
  86. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  87. const emailPayload: EmailPayload = {
  88. to,
  89. subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
  90. text
  91. }
  92. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  93. }
  94. addNewFollowNotification (to: string[], actorFollow: ActorFollowModel, followType: 'account' | 'channel') {
  95. const followerName = actorFollow.ActorFollower.Account.getDisplayName()
  96. const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
  97. const text = `Hi dear user,\n\n` +
  98. `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
  99. `\n\n` +
  100. `Cheers,\n` +
  101. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  102. const emailPayload: EmailPayload = {
  103. to,
  104. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
  105. text
  106. }
  107. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  108. }
  109. addNewInstanceFollowerNotification (to: string[], actorFollow: ActorFollowModel) {
  110. const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
  111. const text = `Hi dear admin,\n\n` +
  112. `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
  113. `\n\n` +
  114. `Cheers,\n` +
  115. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  116. const emailPayload: EmailPayload = {
  117. to,
  118. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
  119. text
  120. }
  121. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  122. }
  123. myVideoPublishedNotification (to: string[], video: VideoModel) {
  124. const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
  125. const text = `Hi dear user,\n\n` +
  126. `Your video ${video.name} has been published.` +
  127. `\n\n` +
  128. `You can view it on ${videoUrl} ` +
  129. `\n\n` +
  130. `Cheers,\n` +
  131. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  132. const emailPayload: EmailPayload = {
  133. to,
  134. subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
  135. text
  136. }
  137. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  138. }
  139. myVideoImportSuccessNotification (to: string[], videoImport: VideoImportModel) {
  140. const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
  141. const text = `Hi dear user,\n\n` +
  142. `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
  143. `\n\n` +
  144. `You can view the imported video on ${videoUrl} ` +
  145. `\n\n` +
  146. `Cheers,\n` +
  147. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  148. const emailPayload: EmailPayload = {
  149. to,
  150. subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
  151. text
  152. }
  153. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  154. }
  155. myVideoImportErrorNotification (to: string[], videoImport: VideoImportModel) {
  156. const importUrl = WEBSERVER.URL + '/my-account/video-imports'
  157. const text = `Hi dear user,\n\n` +
  158. `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
  159. `\n\n` +
  160. `See your videos import dashboard for more information: ${importUrl}` +
  161. `\n\n` +
  162. `Cheers,\n` +
  163. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  164. const emailPayload: EmailPayload = {
  165. to,
  166. subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
  167. text
  168. }
  169. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  170. }
  171. addNewCommentOnMyVideoNotification (to: string[], comment: VideoCommentModel) {
  172. const accountName = comment.Account.getDisplayName()
  173. const video = comment.Video
  174. const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
  175. const text = `Hi dear user,\n\n` +
  176. `A new comment has been posted by ${accountName} on your video ${video.name}` +
  177. `\n\n` +
  178. `You can view it on ${commentUrl} ` +
  179. `\n\n` +
  180. `Cheers,\n` +
  181. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  182. const emailPayload: EmailPayload = {
  183. to,
  184. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
  185. text
  186. }
  187. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  188. }
  189. addNewCommentMentionNotification (to: string[], comment: VideoCommentModel) {
  190. const accountName = comment.Account.getDisplayName()
  191. const video = comment.Video
  192. const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
  193. const text = `Hi dear user,\n\n` +
  194. `${accountName} mentioned you on video ${video.name}` +
  195. `\n\n` +
  196. `You can view the comment on ${commentUrl} ` +
  197. `\n\n` +
  198. `Cheers,\n` +
  199. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  200. const emailPayload: EmailPayload = {
  201. to,
  202. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
  203. text
  204. }
  205. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  206. }
  207. addVideoAbuseModeratorsNotification (to: string[], videoAbuse: VideoAbuseModel) {
  208. const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
  209. const text = `Hi,\n\n` +
  210. `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
  211. `Cheers,\n` +
  212. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  213. const emailPayload: EmailPayload = {
  214. to,
  215. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
  216. text
  217. }
  218. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  219. }
  220. addVideoAutoBlacklistModeratorsNotification (to: string[], video: VideoModel) {
  221. const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
  222. const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
  223. const text = `Hi,\n\n` +
  224. `A recently added video was auto-blacklisted and requires moderator review before publishing.` +
  225. `\n\n` +
  226. `You can view it and take appropriate action on ${videoUrl}` +
  227. `\n\n` +
  228. `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
  229. `\n\n` +
  230. `Cheers,\n` +
  231. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  232. const emailPayload: EmailPayload = {
  233. to,
  234. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
  235. text
  236. }
  237. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  238. }
  239. addNewUserRegistrationNotification (to: string[], user: UserModel) {
  240. const text = `Hi,\n\n` +
  241. `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
  242. `Cheers,\n` +
  243. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  244. const emailPayload: EmailPayload = {
  245. to,
  246. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
  247. text
  248. }
  249. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  250. }
  251. addVideoBlacklistNotification (to: string[], videoBlacklist: VideoBlacklistModel) {
  252. const videoName = videoBlacklist.Video.name
  253. const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
  254. const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
  255. const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
  256. const text = 'Hi,\n\n' +
  257. blockedString +
  258. '\n\n' +
  259. 'Cheers,\n' +
  260. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  261. const emailPayload: EmailPayload = {
  262. to,
  263. subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
  264. text
  265. }
  266. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  267. }
  268. addVideoUnblacklistNotification (to: string[], video: VideoModel) {
  269. const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
  270. const text = 'Hi,\n\n' +
  271. `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
  272. '\n\n' +
  273. 'Cheers,\n' +
  274. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  275. const emailPayload: EmailPayload = {
  276. to,
  277. subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
  278. text
  279. }
  280. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  281. }
  282. addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
  283. const text = `Hi dear user,\n\n` +
  284. `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
  285. `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
  286. `If you are not the person who initiated this request, please ignore this email.\n\n` +
  287. `Cheers,\n` +
  288. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  289. const emailPayload: EmailPayload = {
  290. to: [ to ],
  291. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
  292. text
  293. }
  294. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  295. }
  296. addVerifyEmailJob (to: string, verifyEmailUrl: string) {
  297. const text = `Welcome to PeerTube,\n\n` +
  298. `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
  299. `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
  300. `If you are not the person who initiated this request, please ignore this email.\n\n` +
  301. `Cheers,\n` +
  302. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  303. const emailPayload: EmailPayload = {
  304. to: [ to ],
  305. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
  306. text
  307. }
  308. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  309. }
  310. addUserBlockJob (user: UserModel, blocked: boolean, reason?: string) {
  311. const reasonString = reason ? ` for the following reason: ${reason}` : ''
  312. const blockedWord = blocked ? 'blocked' : 'unblocked'
  313. const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
  314. const text = 'Hi,\n\n' +
  315. blockedString +
  316. '\n\n' +
  317. 'Cheers,\n' +
  318. `${CONFIG.EMAIL.BODY.SIGNATURE}`
  319. const to = user.email
  320. const emailPayload: EmailPayload = {
  321. to: [ to ],
  322. subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
  323. text
  324. }
  325. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  326. }
  327. addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
  328. const text = 'Hello dear admin,\n\n' +
  329. fromName + ' sent you a message' +
  330. '\n\n---------------------------------------\n\n' +
  331. body +
  332. '\n\n---------------------------------------\n\n' +
  333. 'Cheers,\n' +
  334. 'PeerTube.'
  335. const emailPayload: EmailPayload = {
  336. fromDisplayName: fromEmail,
  337. replyTo: fromEmail,
  338. to: [ CONFIG.ADMIN.EMAIL ],
  339. subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
  340. text
  341. }
  342. return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
  343. }
  344. sendMail (options: EmailPayload) {
  345. if (!Emailer.isEnabled()) {
  346. throw new Error('Cannot send mail because SMTP is not configured.')
  347. }
  348. const fromDisplayName = options.fromDisplayName
  349. ? options.fromDisplayName
  350. : WEBSERVER.HOST
  351. return this.transporter.sendMail({
  352. from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
  353. replyTo: options.replyTo,
  354. to: options.to.join(','),
  355. subject: options.subject,
  356. text: options.text
  357. })
  358. }
  359. private dieOnConnectionFailure (err?: Error) {
  360. logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
  361. process.exit(-1)
  362. }
  363. static get Instance () {
  364. return this.instance || (this.instance = new this())
  365. }
  366. }
  367. // ---------------------------------------------------------------------------
  368. export {
  369. Emailer,
  370. SendEmailOptions
  371. }