Quick Stellar scripting – Payment Mailer

Just a little script to track your payments without opening the stellar client all the time.
You can adjust this to your needs. All you need is Nodejs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var StellarSdk = require('stellar-sdk')
var nodemailer = require('nodemailer');
// Remove "-testnet" if you wanna work in the real world
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

var accountId = 'your Public Key goes here'; //testnet

//Config Your Smtp-Server
let smtpConfig = {
  host: 'your smtp-server',
  port: 587,
  secure: false, // upgrade later with STARTTLS
  auth: {
    user: 'your smtp user',
    pass: 'your smtp password'
  }
};
let transporter = nodemailer.createTransport(smtpConfig)


// // you can use this code to
// // verify connection configuration
// transporter.verify(function(error, success) {
//   if (error) {
//        console.log(error);
//   } else {
//        console.log('Server is ready to take our messages');
//   }
// });

// Get a message any time a payment occurs.
var es = server.payments()
  .forAccount(accountId)
  .cursor('now')
  .stream({
    onmessage: function (message) {

      var transactionID = message.transaction_hash;
      var memo = "";
      var mailtext = "";
      //only send e-mail if it is a real payment
      if (message.amount > 0) {
        //get the transaction your paymet is in to pull the memo text if any
        server.transactions()
          .transaction(transactionID)
          .call()
          .then(function (transactionResult) {
            memo = transactionResult.memo;

          })
          .then(function () {
            var code = "xlm";
            if (message.asset_type != "native")
              code = message.asset_code;

            //now we got all we need to build the MailBody.
            // Format it as you like it or include other informations
            var mailtext = "you received\n";
            mailtext = mailtext + message.amount + " " + code + "\n";
            mailtext = mailtext + "from " + message.source_account + "\n";
            mailtext = mailtext + "MEMO " + memo; +"\n";
            console.log(mailtext);

            //send email
            var mailOptions = {
              from: 'sender-mail-address',
              to: 'received-mail-address',
              subject: 'you received a payment',
              text: mailtext
            };

            transporter.sendMail(mailOptions, function (error, info) {
              if (error) {
                console.log(error);
              } else {
                console.log('Email sent: ' + info.response);
              }
            });
          })
          .catch(function (err) {
            console.log(err)
          })

      }
    }
  })

Recent Posts

Recent Comments

Archives

abcxyz Written by:

Be First to Comment

Leave a Reply

Your email address will not be published.