newdeveloper
newdeveloper

Reputation: 1451

nodejs encoding to base64 string is not working

I am trying to send a calendar event as an attachment (.ics) file in nodejs.

I am using ical-generator library to create an event.

The code below is generating an .ics file and attaching it with the email but file always comes as empty.

I am not sure why the event content is not converting to base64. when I log the converted content it shows the same content (not converting to base64)

const cal = ical({ domain: "github.com", name: "my first iCal" });
// overwrite domain
cal.domain("example.net");

cal.createEvent({
  start: moment(),
  end: moment().add(1, "hour"),
  summary: "Example Event",
  description: "It works ;)",
  location: "my room",
  url: "http://example.net/",
});

 console.log('result :', cal.toString("base64"));
    // result : 
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example.net//ical-generator//EN
NAME:my first iCal
X-WR-CALNAME:my first iCal
BEGIN:VEVENT
UID:[email protected]
SEQUENCE:0
DTSTAMP:20210403T212902Z
DTSTART:20210403T212902Z
DTEND:20210403T222902Z
SUMMARY:Example Event
LOCATION:my room
DESCRIPTION:It works \;)
URL;VALUE=URI:http://example.net/
END:VEVENT
END:VCALENDAR

 var message = {
      html: emailBody,
      subject: "test",
      from_email: "from email",
      from_name: "sender name",
      to: [
        {
          email: "receiver email",
        },
      ],
      tags: ["test"],
      attachments: [
        {
          type: "text/calendar",
          content: cal.toString("base64"),
          name: "fileName.ics",
        },
      ],
    };

I found this encoded to base64 content from the other thread and it works just fine. "QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KUFJPRElEOi0vL01lZXRlci9tZWV0ZXIvL05PTlNHTUwgdjEuMC8vRU4NCkNBTFNDQUxFOkdSRUdPUklBTg0KTUVUSE9EOlJFUVVFU1QNCkJFR0lOOlZFVkVOVA0KRFRTVEFSVDoyMDE0MTAxOFQyMDMwMDBaDQpEVEVORDoyMDE0MTAxOFQyMTAwMDBaDQpVSUQ6MjAxNDEwMTVUMDAyODEzLTIyMzc4ODg2OEBtZWV0ZXIuY29tDQpEVFNUQU1QOjIwMTQxMDE0VDIxMjgxM1oNCk9SR0FOSVpFUjtDTj0ic25hZ2dzQGdtYWlsLmNvbSI7U0VOVC1CWT0iTUFJTFRPOnNvbWVhcHBAZ21haWwuY29tIjtMQU5HVUFHRT1zZTpNQUlMVE86c25hZ2dzQGdtYWlsLmNvbQ0KQVRURU5ERUU7Q1VUWVBFPUlORElWSURVQUw7Uk9MRT1SRVEtUEFSVElDSVBBTlQ7UEFSVFNUQVQ9TkVFRFMtQUNUSU9OO1JTVlA9VFJVRTtDTj1GZXNzeSBNO1gtTlVNLUdVRVNUUz0wOk1BSUxUTzpzbmFnZ3MyQGdtYWlsLmNvbQ0KREVTQ1JJUFRJT046ZGRkZCBtYW5kcmlsbA0KTE9DQVRJT046ZGRkZGRkIG1hbmRyaWxsDQpTVU1NQVJZOkNhbiBJIGxheSBsb3c/IENvb2sgc29tZSB5YXkteW8gMg0KVFJBTlNQOk9QQVFVRQ0KU0VRVUVOQ0U6MA0KU1RBVFVTOkNPTkZJUk1FRA0KRU5EOlZFVkVOVA0KRU5EOlZDQUxFTkRBUg=="

If I try abover encoded string, my calendar event works fine. the file.ics also works fine.. so my guess is problem while converting that event content to base64.

Upvotes: 2

Views: 1373

Answers (1)

cbr
cbr

Reputation: 13612

The ical-generator package's calendar class doesn't provide a toString() which takes the format as the parameter, like Buffer. You'll just need to convert it to Base 64 like you'd convert any other string.

const calStr = cal.toString()
const calB64 = Buffer.from(calStr).toString('base64')
console.log('result :', calB64);

Upvotes: 4

Related Questions