本文介绍了在Heroku上使用parse-server设置mailgun的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将mailgun设置为与Heroku上的parse-server应用程序一起使用.

I am trying to set up mailgun for use with parse-server app on Heroku.

推荐的API是

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

但是关于如何实际实现此目标的说明不存在.

But instructions on how to actually achieve this are non existent.

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

--------- EDIT -----------

按照说明进行操作,服务器将其推送至Heroku.虽然我仍然没有收到电子邮件.我只是使用mailgun沙箱,并已授权并激活了收件人.

Followed instructions and server pushes to Heroku. Though i am still not receiving emails. Im just using mailgun sandbox and have authorised and activated a recipient.

我不确定要指定模板的路径,还是:

Im not sure about specifying the path to templates or:

fromAddress: process.env.EMAIL_FROM 

这不是mailgun提供的,所以我刚刚输入了[email protected]

This wasn't supplied by mailgun so i have just entered [email protected]

这显然无效吗?

index.js代码:

index.js code:

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告诉我的所有信息都是正确的,并且服务器端设置正常,可能只是我对变量所做的一些愚蠢的事情.

My swift code to send password reset says that the message has been sent, so i assume that everything Cliff has told me is correct and the server side is set up ok, probably just something stupid that i have done with the variables.

    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 )
        }
    }

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

----------- EDIT ----------

尝试重设密码后查看详细日志

Verbose log after trying reset password

[email protected]
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.

So definitely looks like its trying to send from a template that doesn't exist. Im not sure how to specify path here, i had thought that all these directories including node_modules/parse-server-mailgun-adapter/test/email-templates were pushed to Heroku.

推荐答案

首先:

npm install --save parse-server-mailgun

然后在您的index.js文件中,可以按以下步骤在初始化中对其进行设置:

Then in your index.js file you can set it up as follows in the initialize:

 publicServerURL: 'http://MY_HEROKU_APP.herokuapp.com/parse', 
 appName: 'MY_APP',
 emailAdapter: {
    module: 'parse-server-mailgun',
    options: {
      fromAddress: '[email protected]',
      domain: 'example.com',
      apiKey: 'key-XXXXXX',
    }
 }

解析服务器随附的默认Mailgun适配器,您需要设置fromAddres,以及Mailgun提供的域和apiKey.此外,您还需要配置要使用的模板.您必须为每个模板至少提供纯文本版本. html版本是可选的.

The default Mailgun adapter that comes with the Parse Server, you need to set a fromAddres, and the domain and apiKey provided by Mailgun. In addition, you also need to configure the templates you want to use. You must provide at least a plain-text version for each template. The html versions are optional.

verifyUserEmails: true,
  emailAdapter: {
    module: 'parse-server-mailgun',
    options: {
      // The address that your emails come from 
      fromAddress: 'YourApp <[email protected]>',
      // 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

Reference to the NPM package: https://www.npmjs.com/package/parse-server-mailgun

这篇关于在Heroku上使用parse-server设置mailgun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:10