Skip to content

Instantly share code, notes, and snippets.

@eluleci
Created March 22, 2016 15:13
Show Gist options
  • Save eluleci/9d60bbf603e8f6e51de1 to your computer and use it in GitHub Desktop.
Save eluleci/9d60bbf603e8f6e51de1 to your computer and use it in GitHub Desktop.
package ical
import "time"
type Calendar struct {
Prodid, Method string
Events []Event
}
func (c Calendar) String() string {
s := "BEGIN:VCALENDAR\n"
// supported version is 2.0
s += "VERSION:2.0\n"
// GREGORIAN is the only available value in version 2.0 (https://tools.ietf.org/html/rfc2445#section-4.7.1)
s += "CALSCALE:GREGORIAN\n"
if c.Prodid != "" {
s += "PRODID:" + c.Prodid + "\n"
}
if c.Method != "" {
s += "METHOD:" + c.Method + "\n"
}
if c.Events != nil && len(c.Events) > 0 {
for _, event := range c.Events {
s += event.String()
}
}
s += "END:VCALENDAR"
return s
}
type Event struct {
Uid, Summary, Description, Location string
Dtstamp, Dtstart, Dtend time.Time
Organizer Person
Attendees []Person
}
func (e Event) String() string {
s := "BEGIN:VEVENT\n"
s += "UID:" + e.Uid + "\n"
s += "ORGANIZER;" + e.Organizer.String()
s += "DTSTAMP:" + formatTime(e.Dtstamp) + "\n"
s += "DTSTART:" + formatTime(e.Dtstart) + "\n"
s += "DTEND:" + formatTime(e.Dtend) + "\n"
s += "SUMMARY:" + e.Summary + "\n"
s += "DESCRIPTION:" + e.Description + "\n"
s += "LOCATION:" + e.Location + "\n"
for _, attendee := range e.Attendees {
s += "ATTENDEE;" + attendee.String()
}
s += "END:VEVENT\n"
return s
}
type Person struct {
Cn, Mailto string
}
func (o Person) String() string {
return "CN=" + o.Cn + ":MAILTO:" + o.Mailto + "\n"
}
func formatTime(t time.Time) string {
return t.Format("20060102T150405Z")
}
package main
import (
"github.com/scorredoira/email"
"github.com/eluleci/ical"
"net/smtp"
"fmt"
"bytes"
"time"
)
func main() {
ics := generateIcs()
_ = ics
to := []string{"recipient@company.com"}
cc := []string{"cc@company.com"}
bcc := []string{"bcc@company.com"}
sendEventEmail(to, cc, bcc, ics)
}
func generateIcs() string {
eventId := "uniqueEventId"
createdAt := time.Unix(1458819901, 0)
startDate := time.Unix(1458819901, 0)
endDate := time.Unix(1458822600, 0)
summary := "Event title"
description := "Event description"
location := "Some location"
organizer := ical.Person{"Organizer Person", "organizer@company.com"}
attendee1 := ical.Person{"First Attendee", "first@attendee.com"}
attendee2 := ical.Person{"Second Attendee", "second@attendee.com"}
event := ical.Event{eventId, summary, description, location, createdAt, startDate, endDate, organizer, []ical.Person{attendee1, attendee2}}
ics := ical.Calendar{}
ics.Prodid = "-//Mentornity//NONSGML v1.0//EN"
ics.Events = []ical.Event{event}
fmt.Println(ics)
return ics.String()
}
func sendEventEmail(to, cc, bcc []string, ics string) {
senderName := ""
senderEmail := ""
senderEmailPassword := ""
smtpServer := "smtp.yandex.com.tr"
smtpPort := "587"
message := email.NewMessage("Sample event mail", "This mail should contain an event file.")
message.From = senderEmail
message.To = to
message.Cc = cc
message.Bcc = bcc
var buf bytes.Buffer
buf.Write([]byte(ics))
aErr := message.AttachBuffer("meeting.ics", buf.Bytes(), false)
if aErr != nil {
fmt.Println(aErr)
}
auth := smtp.PlainAuth(senderName, senderEmail, senderEmailPassword, smtpServer)
err := email.Send(smtpServer + ":" + smtpPort, auth, message)
fmt.Println(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment