Re: [go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Amandeep Kaur

Hi, Jakob Borg

Thanks for ypur reply to the post. The solution you have provided can not 
be implemented on my system. I have provided a small sample of my code 
structure. Please take a look

type EmailTemplate struct{
BookingDetails string
}

type EmailRequest struct{
EmailTo  string
EmailBody  string
}

// get saved html with tokens from database
notificationTemplate, errVal := merchantDb.GetNotificationTemplate()
request := EmailRequest{
"t...@example.com", 
notificationTemplate.Content,
}
templateData.BookingDetails += 
"Industry"+industry.IndustryName+""

request.EmailSend(templateData)


func (request *EmailRequest) EmailSend(notificationTemplateData 
interface{}) (bool, error) {
body, errParse := ParseTemplate(request.EmailBody, 
notificationTemplateData)
//email sending code here 
}

func ParseTemplate(templateHtml string, data interface{}) (string, 
error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens.
When I use ParseTemplate function to parse tokens as string values then it 
works fine. 
But if I have to parse html in one of my tokens then it parses html as 
string and in email displays html as string.

How ever If I try to make assertion on it from interface to template.HTML, 
it gives me error: 
cannot convert notificationTemplateData (type interface {}) to type 
"html/template".HTML: need type assertion
On Friday, May 4, 2018 at 2:18:51 PM UTC+5:30, Jakob Borg wrote:
>
> Hi, 
>
> Your post is a bit confusing and I think you may be using the word "parse" 
> in the opposite of it its common meaning. However, if you want to pass a 
> HTML fragment through a HTML template and have it not be escaped, look at 
> the template.HTML type: https://golang.org/pkg/html/template/#HTML
>
> //jb
>
> On 4 May 2018, at 10:42, Amandeep Kaur <amandeep...@gmail.com 
> > wrote:
>
> Hello,
>
> I am working on a SAAS based project for which I need to send emails to 
> different clients on different events. 
>
> I am using email templates which use tokens (in format {{.TOKENNAME}}) 
> that are made dynamic while sending emails. Now these token are parsed by 
> using "html/template" package. 
>
> following is the custom function that I have made to parse these tokens 
> into email body.
>
> func ParseTemplate(templateHtml string, data interface{}) (string, error) {
> var body string
> t, err := template.New("my_template").Parse(templateHtml)
> if err != nil {
> return body, err
> }
> buf := new(bytes.Buffer)
> 
> if err = t.Execute(buf, data); err != nil {
> return body, err
> }
> body = buf.String()
> return body, nil
> }
>
> Where templateHtml is the email body with tokens and data is the interface 
> holding dynamic values for these tokens. When I use ParseTemplate function 
> to parse tokens as string values then it works fine. But if I have to parse 
> html in one of my tokens then it parses html as string and in email 
> displays html as string.
>
> Can anybody tell me what should I do to parse html in ParseTemplate 
> function??
>
> Thanks!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts...@googlegroups.com .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Amandeep Kaur
Hello,

I am working on a SAAS based project for which I need to send emails to 
different clients on different events. 

I am using email templates which use tokens (in format {{.TOKENNAME}}) that 
are made dynamic while sending emails. Now these token are parsed by using 
"html/template" package. 

following is the custom function that I have made to parse these tokens 
into email body.

func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens. When I use ParseTemplate function 
to parse tokens as string values then it works fine. But if I have to parse 
html in one of my tokens then it parses html as string and in email 
displays html as string.

Can anybody tell me what should I do to parse html in ParseTemplate 
function??

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Email sent via command line is not delivered in Golang

2018-01-24 Thread Amandeep Kaur




I am sending emails in one of my projects through command line. The project 
back end is written in Golang. I am using following code to send emails:



package utils


import(
"bytes"
"html/template"
"os/exec"
"fmt"
)

type EmailRequest struct{
EmailTo  string
EmailSubject string
EmailBodystring
}

func (request *EmailRequest) EmailSend(notificationTemplateData 
interface{}) (bool, error) {
subject, errParse := ParseTemplate(request.EmailSubject, 
notificationTemplateData)
body, errParse:= ParseTemplate(request.EmailBody, 
notificationTemplateData)
if errParse != nil{
return false, errParse
}
err := ExecuteMailCommand("mail -s \"$(echo \" "+subject+" \nMIME-version: 
1.0;\nContent-Type: text/html;charset=\"UTF-8\";\n\")\"  "+request.EmailTo, 
body)
if err != nil {
return false, err
}

return true, nil
}


func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("test").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

   func ExecuteMailCommand(command string, body string) error {
cmd := exec.Command("sh", "-c", command)
cmd.Stdin = bytes.NewBufferString(body)

stdout, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%v\n", err)
return err
}

fmt.Printf("%s\n", stdout)
return nil
}


But while sending email through this code, strange thing is happening. 
Sometimes the email is delivered immediately. But sometimes it is not 
delivered even upto somehours. The server is running on AWS.

I am not getting the reason why it is behaving like this. Do I need to set 
some server mail configurations or there is some problem in the code ?/

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Update Mongodb fields with omitempty flag in Golang structure

2017-11-10 Thread Amandeep Kaur
Thanks @Diego Medina  It worked for me.

On Thursday, November 9, 2017 at 12:19:14 AM UTC+5:30, Diego Medina wrote:
>
> replied on the mgo list 
>
> https://groups.google.com/d/topic/mgo-users/F3LbWdyPGDc/discussion
>
> (pasted here to save a click)
>
> Hi,
>
> > the checkbox is unchecked and form is submitted to save. Now as I have 
> applied "omitempty" 
>
> if the checkbox is unchecked, your client side code should send "false" to 
> the go server, so omitempty is not applied because it is not empty, it is 
> false, so saving it to mongo will actually update the field.
>
> for truly optional fields, what I do is, declare the fields as pointers to 
> the type, so, you would have
>
> type Coupon struct {
> Id   *int`json:"id,omitempty" bson:"_id,omitempty"`
> Name *string `json:"name,omitempty" bson:"name,omitempty"`
> Code *string `json:"code,omitempty" bson:"code,omitempty"`
> Description  *string `json:"description,omitempty" 
> bson:"description,omitempty"`
> Status   *bool   `json:"status" bson:"status"`// I assume this 
> is also optional
> MaxUsageLimit*int`json:"max_usage_limit,omitempty" 
> bson:"max_usage_limit,omitempty"`
> SingleUsePerUser *bool   `json:"single_use_per_user,omitempty" 
> bson:"single_use_per_user,omitempty"`
> }
>
>
> the bson/json Un/marshaller will then omit the values that are nil 
> pointers, so now yo ucan tell the difference between a false checkbox, and 
> a REST POST that did not include the field at all
>
> Hope this helps.
>
> Diego
>
> On Wednesday, November 8, 2017 at 12:46:39 AM UTC-5, Amandeep Kaur wrote:
>>
>>
>> Hi
>>
>> I am working on a Coupon form in which I have some optional fields. 
>>
>> *Introduction:*
>>
>> All the form field values are received as JSON and mapped into a Golang 
>> structure. In the structure, I have added an "omitempty" flag with every 
>> field. So only those form values are mapped which have some appropriate 
>> value, rest of the values like 0, " ", false are ignored by the structure.
>>
>> Here is the Golang structure
>>
>> type Coupon struct {
>> Id   int`json:"id,omitempty" bson:"_id,omitempty"`
>> Name string `json:"name,omitempty" bson:"name,omitempty"`
>> Code string `json:"code,omitempty" bson:"code,omitempty"`
>> Description  string `json:"description,omitempty" 
>> bson:"description,omitempty"`
>> Status   bool   `json:"status" bson:"status"`
>> MaxUsageLimitint`json:"max_usage_limit,omitempty" 
>> bson:"max_usage_limit,omitempty"`
>> SingleUsePerUser bool   `json:"single_use_per_user,omitempty" 
>> bson:"single_use_per_user,omitempty"`}
>>
>> *Problem:*
>>
>>1. 
>>
>>When I save this form for the very first time, the form values that 
>>are appropriate are saved into the Mongodb.
>>2. 
>>
>>Now I want to update that form and suppose there is a check box, 
>>which was checked at the time of saving data. While updating form, the 
>>checkbox is unchecked and form is submitted to save. Now as I have 
>> applied 
>>"omitempty" flag in the structure, so its not mapping the empty value to 
>>the checkbox field. Since the value is not mapped into the structure, its 
>>not getting saved into the Database. 
>>3. 
>>
>>When a user edits the form for the second time, it sees the same 
>>check box as checked. (But practically, the value should be updated to 
>> the 
>>DB and the check box should be displayed as unchecked.)
>>4. 
>>
>>I am using the same form data (in JSON format) in a REST API. In API, 
>>while updating form data, if I mention only those values which are 
>> required 
>>and don't pass the values which I don't want to update, then MongoDB is 
>>overriding the whole document with the provided required values(Even 
>> those 
>>values are also being overridden which I don't want to update as well as 
>>don't pass in the API).
>>
>> *Requirement:*
>>
>> In future, I want to expose the REST API, So I don't want this thing to 
>> be happened there. That is why I don't want to remove "omitempty" flag from 
>> the structure fields. 
>>
>> Is there any way to save the empty form values or API data fields to the 
>> DB while using omitempty flag in the structure?
>>
>> Thanks!
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Update Mongodb fields with omitempty flag in Golang structure

2017-11-07 Thread Amandeep Kaur


Hi

I am working on a Coupon form in which I have some optional fields. 

*Introduction:*

All the form field values are received as JSON and mapped into a Golang 
structure. In the structure, I have added an "omitempty" flag with every 
field. So only those form values are mapped which have some appropriate 
value, rest of the values like 0, " ", false are ignored by the structure.

Here is the Golang structure

type Coupon struct {
Id   int`json:"id,omitempty" bson:"_id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
Code string `json:"code,omitempty" bson:"code,omitempty"`
Description  string `json:"description,omitempty" 
bson:"description,omitempty"`
Status   bool   `json:"status" bson:"status"`
MaxUsageLimitint`json:"max_usage_limit,omitempty" 
bson:"max_usage_limit,omitempty"`
SingleUsePerUser bool   `json:"single_use_per_user,omitempty" 
bson:"single_use_per_user,omitempty"`}

*Problem:*

   1. 
   
   When I save this form for the very first time, the form values that are 
   appropriate are saved into the Mongodb.
   2. 
   
   Now I want to update that form and suppose there is a check box, which 
   was checked at the time of saving data. While updating form, the checkbox 
   is unchecked and form is submitted to save. Now as I have applied 
   "omitempty" flag in the structure, so its not mapping the empty value to 
   the checkbox field. Since the value is not mapped into the structure, its 
   not getting saved into the Database. 
   3. 
   
   When a user edits the form for the second time, it sees the same check 
   box as checked. (But practically, the value should be updated to the DB and 
   the check box should be displayed as unchecked.)
   4. 
   
   I am using the same form data (in JSON format) in a REST API. In API, 
   while updating form data, if I mention only those values which are required 
   and don't pass the values which I don't want to update, then MongoDB is 
   overriding the whole document with the provided required values(Even those 
   values are also being overridden which I don't want to update as well as 
   don't pass in the API).
   
*Requirement:*

In future, I want to expose the REST API, So I don't want this thing to be 
happened there. That is why I don't want to remove "omitempty" flag from 
the structure fields. 

Is there any way to save the empty form values or API data fields to the DB 
while using omitempty flag in the structure?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.