package email import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ses" log "github.com/rs/zerolog/log" "os" ) const ( // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. Sender = "creditfaircapital@creditfair.in" // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. Recipient = "creditfaircapital@creditfair.in" //os.Getenv("CFC_EMAIL_GROUP_TO") CCRecipient = "maria@creditfair.in" //os.Getenv("CFC_EMAIL_GROUP_CC") // Specify a configuration set. To use a configuration // set, comment the next line and line 92. //ConfigurationSet = "ConfigSet" // The subject line for the email. //Subject = "You request for investment is under process" // The HTML body for the email. //The email body for recipients with non-HTML email clients. TextBody = "You request for investment is under process" // The character encoding for the email. CharSet = "UTF-8" ) func Email(message string, Subject string) (string, error) { sender := os.Getenv("CFC_EMAIL_GROUP_FROM") CCRecipient := os.Getenv("CFC_EMAIL_GROUP_CC") Recipient := os.Getenv("CFC_EMAIL_GROUP_TO") aws_access_key_id := os.Getenv("AWS_SES_KEY_ID") //os.Getenv("AWS_ACCESS_KEY_ID") aws_secret_access_key := os.Getenv("AWS_SECRET_SES_KEY") //os.Getenv("AWS_SECRET_ACCESS_KEY") creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, "") _, err := creds.Get() if err != nil { log.Print("err.Error()") log.Error().Err(err).Msg(err.Error()) return err.Error(), err } cfg := aws.NewConfig().WithRegion("ap-south-1").WithCredentials(creds) //os.Getenv("AWS_DEFAULT_REGION") // sess, err := session.NewSession(&aws.Config{ // Region:aws.String("ap-south-1")}, // ) // Create an SES session. svc := ses.New(session.New(), cfg) // Assemble the email. input := &ses.SendEmailInput{ Destination: &ses.Destination{ CcAddresses: []*string{ aws.String(CCRecipient), }, ToAddresses: []*string{ aws.String(Recipient), }, }, Message: &ses.Message{ Body: &ses.Body{ Html: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(message), }, Text: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(TextBody), }, }, Subject: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(Subject), }, }, Source: aws.String(sender), // Uncomment to use a configuration set //ConfigurationSetName: aws.String(ConfigurationSet), } // Attempt to send the email. result, err := svc.SendEmail(input) // Display error messages if they occur. if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case ses.ErrCodeMessageRejected: log.Print(ses.ErrCodeMessageRejected, aerr.Error()) case ses.ErrCodeMailFromDomainNotVerifiedException: log.Print(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error()) case ses.ErrCodeConfigurationSetDoesNotExistException: log.Print(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error()) default: log.Print(aerr.Error()) } } else { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. log.Error().Err(err).Msg(err.Error()) } return "", nil } log.Print("Email Sent to address: " + Recipient) log.Print(result) return "", nil } func EmailAttachment(email string, message string, subject string, attachment map[string]string) (string, error) { log.Print(attachment) sender := os.Getenv("CFC_EMAIL_GROUP_FROM") CCRecipient := os.Getenv("CFC_EMAIL_GROUP_CC") Recipient := email //os.Getenv("CFC_EMAIL_GROUP_TO") aws_access_key_id := os.Getenv("AWS_SES_KEY_ID") //os.Getenv("AWS_ACCESS_KEY_ID") aws_secret_access_key := os.Getenv("AWS_SECRET_SES_KEY") //os.Getenv("AWS_SECRET_ACCESS_KEY") creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, "") _, err := creds.Get() if err != nil { log.Print("err.Error()") log.Error().Err(err).Msg(err.Error()) return err.Error(), err } cfg := aws.NewConfig().WithRegion("ap-south-1").WithCredentials(creds) //os.Getenv("AWS_DEFAULT_REGION") // sess, err := session.NewSession(&aws.Config{ // Region:aws.String("ap-south-1")}, // ) // Create an SES session. svc := ses.New(session.New(), cfg) //str := base64.StdEncoding.EncodeToString([]byte(message)) var ses_mail = "From: 'Credit Fair Capital' <" + sender + ">\n" ses_mail += "To: " + Recipient + "\n" ses_mail += "Subject: " + subject + "\n" ses_mail += "MIME-Version: 1.0\n" ses_mail += "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n" ses_mail += "--NextPart\n" ses_mail += "Content-Type: text/html\n\n" ses_mail += message + ".\n\n" ses_mail += "--NextPart\n" ses_mail += "Content-Type: application/octet-stream; name=\"" + attachment["FileName"] + "\"\n" ses_mail += "Content-Transfer-Encoding: base64\n" ses_mail += "Content-Disposition: attachment\n\n" ses_mail += attachment["File"] + "\n\n" ses_mail += "--NextPart--" // Assemble the email. input := &ses.SendRawEmailInput{ Destinations: []*string{ aws.String(Recipient), // Required aws.String(CCRecipient), //aws.String(CCRecipient), // More values... }, RawMessage: &ses.RawMessage{ // Required Data: []byte(ses_mail), // Required }, Source: aws.String(sender), // Uncomment to use a configuration set //ConfigurationSetName: aws.String(ConfigurationSet), } // Attempt to send the email. result, err := svc.SendRawEmail(input) // Display error messages if they occur. if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case ses.ErrCodeMessageRejected: log.Print(ses.ErrCodeMessageRejected, aerr.Error()) case ses.ErrCodeMailFromDomainNotVerifiedException: log.Print(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error()) case ses.ErrCodeConfigurationSetDoesNotExistException: log.Print(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error()) default: log.Print(aerr.Error()) } } else { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. log.Error().Err(err).Msg(err.Error()) } return "", nil } log.Print("Email Sent to address: " + Recipient) log.Print(result) return "", nil }