【问题标题】:Setup mailgun with parse-server on Heroku在 Heroku 上使用解析服务器设置 mailgun
【发布时间】:2017-06-22 08:47:44
【问题描述】:

我正在尝试设置 mailgun 以与 Heroku 上的解析服务器应用程序一起使用。

推荐的API是

https://github.com/ParsePlatform/parse-server-simple-mailgun-adapter

但没有关于如何实际实现这一点的说明。

--------- 编辑 ------------

按照指令和服务器推送到 Heroku。虽然我仍然没有收到电子邮件。我只是在使用 mailgun 沙箱,并且已经授权并激活了收件人。

我不确定要指定模板的路径,或者:

fromAddress: process.env.EMAIL_FROM 

这不是 mailgun 提供的,所以我刚刚输入了 no-reply@myservername.heroku

这显然是无效的?

index.js 代码:

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var S3Adapter = require('parse-server').S3Adapter;
var path = require('path');

const resolve = require('path').resolve;

var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;

if (!databaseUri) {
    console.log('DATABASE_URI not specified, falling back to localhost.');
}


var api = new ParseServer({
    //**** General Settings ****//

    databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
    cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
    serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
    maxUploadSize: '500mb',

    //**** Security Settings ****//
    // allowClientClassCreation: process.env.CLIENT_CLASS_CREATION || false, 
    appId: process.env.APP_ID || 'myAppId',
    masterKey: process.env.MASTER_KEY || 'myMasterKey', //Add your master key here. Keep it secret! 

    //**** Live Query ****//
    // liveQuery: {
    //  classNames: ["TestObject", "Place", "Team", "Player", "ChatMessage"] // List of classes to support for query subscriptions
    // },


      //**** File Storage ****//
      filesAdapter: new S3Adapter({
          accessKey: process.env.S3_ACCESS_KEY || '',
          secretKey: process.env.S3_SECRET_KEY || '',
          bucket: process.env.S3_BUCKET || '',
          directAccess: true
     }),

    //**** Email Verification ****//
    /* Enable email verification */
    // verifyUserEmails: true,
    /* The public URL of your app */
    // This will appear in the link that is used to verify email addresses and reset passwords.


    publicServerURL: process.env.SERVER_URL || '',
    appName: process.env.APP_NAME || '',

     emailAdapter: {
        module: 'parse-server-mailgun',
        options: {
            fromAddress: process.env.EMAIL_FROM || '',
            domain: process.env.MAILGUN_DOMAIN || '',
            apiKey: process.env.MAILGUN_API_KEY  || '',

            templates: {
              passwordResetEmail: {
              subject: 'Reset your password',
              pathPlainText: resolve(__dirname, 'https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/password_reset_email.txt'),
              pathHtml: resolve(__dirname, 'https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/password_reset_email.html'),
              callback: (user) => { return { firstName: user.get('firstName') }}
              // Now you can use {{firstName}} in your templates
              },
              verificationEmail: {
              subject: 'Confirm your account',
              pathPlainText: resolve(__dirname, 'https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/verification_email.txt'),
              pathHtml: resolve(__dirname, 'https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/verification_email.html'),
              callback: (user) => { return { firstName: user.get('firstName') }}
              // Now you can use {{firstName}} in your templates 
              }
            }
        }
     }

});

我发送密码重置的快速代码表示消息已发送,所以我假设 Cliff 告诉我的所有内容都是正确的,并且服务器端设置正常,可能只是我对变量所做的一些愚蠢的事情。

    PFUser.requestPasswordResetForEmail(inBackground: emailAddress!) { (success, error) in
        if error != nil {
            // display error message
            let userMessage: String = error!.localizedDescription
            GeneralFunctions.createAlert(errorTitle: "Oops...", errorMessage: userMessage, className: self )
        } else {
            // display success message
            let userMessage: String = "An new password was sent to \(emailAddress!)"
            GeneralFunctions.createAlert(errorTitle: "Hooray...", errorMessage: userMessage, className: self )
        }
    }

----------- 编辑 ----------

尝试重置密码后的详细日志

email=my-verified-email@email.com.au
2017-02-06T00:44:25.788619+00:00 app[web.1]: [36mverbose[39m: RESPONSE from [POST] /parse/requestPasswordReset: {
2017-02-06T00:44:25.788623+00:00 app[web.1]:   "response": {}
2017-02-06T00:44:25.788625+00:00 app[web.1]: } 
2017-02-06T00:44:25.797455+00:00 app[web.1]: { Error: ENOENT: no such file or directory, open '/app/https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/password_reset_email.txt'
2017-02-06T00:44:25.797458+00:00 app[web.1]:   errno: -2,
2017-02-06T00:44:25.797459+00:00 app[web.1]:   code: 'ENOENT',
2017-02-06T00:44:25.797459+00:00 app[web.1]:   syscall: 'open',
2017-02-06T00:44:25.797462+00:00 app[web.1]:   path: '/app/https:/myparseserveronheroku.herokuapp.com/node_modules/parse-server-mailgun-adapter/test/email-templates/password_reset_email.txt' }
2017-02-06T00:44:25.792191+00:00 heroku[router]: at=info method=POST path="/parse/requestPasswordReset" host=myparseserveronheroku.herokuapp.com request_id=666ff3e0-db4a-4e76-b7b5-6353edc7e15a fwd="111.111.111.11" dyno=web.1 connect=0ms service=81ms status=200 bytes=483

所以看起来它肯定试图从一个不存在的模板发送。我不确定如何在这里指定路径,我以为所有这些目录包括 node_modules/parse-server-mailgun-adapter/test/email-templates 都被推送到 Heroku。

【问题讨论】:

    标签: parse-server mailgun


    【解决方案1】:

    第一:

    npm install --save parse-server-mailgun
    

    然后在你的 index.js 文件中你可以在初始化中进行如下设置:

     publicServerURL: 'http://MY_HEROKU_APP.herokuapp.com/parse', 
     appName: 'MY_APP',
     emailAdapter: {
        module: 'parse-server-mailgun',
        options: {
          fromAddress: 'no-reply@example.com',
          domain: 'example.com',
          apiKey: 'key-XXXXXX',
        }
     }
    

    Parse Server 自带的默认Mailgun 适配器,需要设置一个fromAddres,以及Mailgun 提供的域和apiKey。此外,您还需要配置您要使用的模板。您必须为每个模板至少提供一个纯文本版本。 html 版本是可选的。

    verifyUserEmails: true,
      emailAdapter: {
        module: 'parse-server-mailgun',
        options: {
          // The address that your emails come from 
          fromAddress: 'YourApp <noreply@yourapp.com>',
          // Your domain from mailgun.com 
          domain: 'example.com',
          // Your API key from mailgun.com 
          apiKey: 'key-mykey',
          // The template section 
          templates: {
            passwordResetEmail: {
              subject: 'Reset your password',
              pathPlainText: resolve(__dirname, 'path/to/templates/password_reset_email.txt'),
              pathHtml: resolve(__dirname, 'path/to/templates/password_reset_email.html'),
              callback: (user) => { return { firstName: user.get('firstName') }}
              // Now you can use {{firstName}} in your templates 
            },
            verificationEmail: {
              subject: 'Confirm your account',
              pathPlainText: resolve(__dirname, 'path/to/templates/verification_email.txt'),
              pathHtml: resolve(__dirname, 'path/to/templates/verification_email.html'),
              callback: (user) => { return { firstName: user.get('firstName') }}
              // Now you can use {{firstName}} in your templates 
            },
            customEmailAlert: {
              subject: 'Urgent notification!',
              pathPlainText: resolve(__dirname, 'path/to/templates/custom_alert.txt'),
              pathHtml: resolve(__dirname, 'path/to/templates/custom_alert.html'),
            }
          }
        }
    

    参考 NPM 包:https://www.npmjs.com/package/parse-server-mailgun

    【讨论】:

    • 谢谢。我会告诉你我是怎么处理这个的!
    • 说只是要使用提供的模板,你如何指定这些的路径? /parse-server/node_modules/parse-server-mailgun/test
    • 您的设置是正确的,这很好。路径不正确。 __dirname 是当前模块的目录名。所以看看你的 index.js 在什么路径,然后你从那里构建你的路径。没有 https 或域名。真正的路径
    • 我在没有 https 的情况下尝试过,但我是一个布偶,并且在路径的开头有一个正斜杠。 /node_modules/。非常感谢您的帮助。
    • 所以电子邮件通过了,但是当我单击重置链接时出现错误 Cannot GET /apps/myappid/request_password_reset?token=CqqIJi3PzyHEMouZpVjP421pm&username=test@test.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多