Jenkins pass the variable value from Groovy script to Email Plugin - email

I am trying to send one variable value from a groovy script to email plugin, so that the value will be part of email body.
I am using EnvInject for this, and my groovy script is as below
import hudson.model.*
def pa = new ParametersAction([
new StringParameterValue("MYVAR", "BAR")
])
build.addAction(pa)
And in my email step in Default content section i am trying to get the value of MYVAR using the syntax ${ENV(var: "MYVAR")}
But in the email i am getting blanks. Please suggest what i am missing.

Once you are really sure you env. variable is set. You can do this:
mail bcc: '', body: "Job name: ${env.JOB_NAME} ", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo#foo.com";
Please, note you can access variable like this:
"${env.VAR_NAME}"

Related

How loopback to provide values for mandril template placeholder

From loopback How to provide values for placeholder for mandril template
In this case we have use something like this in mandril template
<p>You may log in to application with this temporary password: <span mc:edit="newPassword"></span> </p>
In loopback code i.e in remote hook in my case i have defined in routes.js as
User.on('resetPasswordRequest', function (info) {
//my info object has the password set
User.app.models.Email.send({
to: info.email,
from: 'abc',
subject: 'application Password Reset',
template: {
name: "mandril template as defined in mandril",
content: [{
name: "newPassword", //name is placeholder name
content: info.options.password // i have already set my password in info object being passed to this remote hook
}]
}
},

How to send attachments through jenkins pipeline using mail

I have configured my jenkins pipeline to send mail like this
post {
success {
mail bcc: '', body: 'this is the body', cc: '', from: '', replyTo: '', subject: 'This is a test', to: 'xyz#gmail.com'
}
}
Now, I would like to send an attachment along with this, but, could not able to find out any option for that. Does anyone knows how to send attachment with the above syntax. I will be thankful.

Unable to send email with boto3 pinpoint send_messages - need example

I have a validated domain, and sending a direct email via the AWS Pinpoint console works fine. However, I can't get the same to work with Boto3 send_messages.
I get {'DeliveryStatus': 'PERMANENT_FAILURE', 'StatusCode': 400, 'StatusMessage': 'Request must include message email message.'}
But I have a MessageConfiguration Default Message with a simple string for Body, and I've tried BodyOverride as well. No problems sending SMS.
I've been unable to snag an example of send_messages, and I think if I saw a working example of an email send, that'd be all I need.
Snippet:
response = ppClient.send_messages(
ApplicationId=pinpointId,
MessageRequest={
'Addresses': {
'mguard#{validateddomain}.com': {
# 'BodyOverride': 'Hello from Pinpoint!',
'ChannelType': 'EMAIL',
}
},
'MessageConfiguration': {
'DefaultMessage': {
'Body': 'Default Message for EMAIL.',
'Substitutions': {}
}
}
}
)

Amazon SES send_email Text body with spacing/newlines

I am facing an issue where all of my text e-mails are scrunched together and do not have new lines persisting through the sending process.
Here is the code:
def send_ses_message(email_to, subject, body):
ses = init_ses_internal()
if ses:
ses.send_email(
Source='no-reply#domain.com',
Destination={
'ToAddresses': [
email_to,
],
},
Message={
'Subject': {
'Data': subject,
},
'Body': {
'Text': {
'Data': body,
'Charset': 'UTF-8',
},
'Html': {
'Data': body,
'Charset': 'UTF-8',
},
}
},
ReplyToAddresses=[
'mharris#domain.com', # just in case someone replies to a no-reply# address I'll receive them
],
ReturnPath='mharris#domain.com', # bounce backs will come to me also
)
return True
I have most recently tried forcing UTF-8 hoping that would allow the newlines to persist. After that I added \n where a new line should exist.
Here is an example of a email:
def send_reset_email(self, email_to, unique_id):
subject = "Company Password Reset"
body = """
Hello!\n\n
We have received a request to reset your password.\n
Please click the link below to proceed with resetting your password. Note: this link will expire in 1 hour.\n\n
http://staging.domain.com/password/reset/{}\n\n
If you did not request this reset you can safely ignore this e-mail.\n\n
Thank you for choosing Company!\n\n
The Company Team\n
www.company.com\n
""".format(unique_id)
send_ses_message(email_to, subject, body)
Please let me know what I can do to ensure that newlines are persistent across Amazon SES. Thanks!
The default content type in SES seems to be text/html, so using <br> instead of \n worked for me
Edit: I was having a similar issue with outlook 2013 clients. Adding a tab character before the newline worked for me.
Replacing \n with \t\n
Or \t\r\n
How do I format a String in an email so Outlook will print the line breaks?
I faced similar issue in sending a contents of log file (stored in variable) to HTML BODY.
Replacing the new line with "<br />" as below helped solve the problem. Below command replaces all newline characters in the text with "<br />".
mytext = mytext.split("\n").join("<br />")

send an email using a template - grails

I want to send an email using a template. I want to have a GSP file where i could style it, and send the email. Currently the send mail function is as follows:
def sendEmail(){
mailService.sendMail {
to "email","**email**"
from "email"
subject "Hi"
body 'Hi'
}
}
in my config.groovy file
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "email"
password = "pwd"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
I went through another Stack Overflow post on this: Where should i add the mail templates ? is it in the views folder ?
sendMail{
multipart true
to "[hidden email]"
subject "Subject goes here"
html g.render( template: '/emails/mailTemplate')
inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')
}
UPDATE
I TREID ADDING A mailTemplate.gsp UNDER EMAILS/ BUT IT DIDNT WORK.
ERROR I GOT Template not found for name [/emails/mailTemplate] and path [/emails/_mailTemplate.gsp]
You can use groovyPageRenderer.render() to parse your email. Below, an example:
class MailingService {
def groovyPageRenderer
def mailService
def yourFunction(User user) {
def content = groovyPageRenderer.render(view: '/mails/myTemplate')
mailService.sendMail {
to user.email
from "email#test.com"
subject "MySubject"
html(content)
}
}
}
In this case, the template is here: /views/mails/MyTemplateFile.gsp
Hope this helps.
Edit:
And the render could be used with a model. Example:
groovyPageRenderer.render(view:'/mails/myTemplate',model:[user:user])
Edit2:
I forgot to add the mailService in my first reply
well, you can try this code...
mailService.sendMail {
to user.email
from "email#test.com"
subject "MySubject"
body(view:'/emails/mailTemplate', model: [a:A])
}
here mailTemplate.gsp is in view/emails. In body of mail service you can use render syntax.
then add '<%# page contentType="text/html" %>' in top of mailTemplate.gsp
Well looking at your code, everything looks good enough.
html g.render(template : '/path/to/template')
should render your template and it will become the body of your mail message.
Have you made sure that you made your template as _template. Since all the gsp's that start with (_) are only considered as a template.
You should also make all the styling(css) inline so that it gets rendered without errors in all mail providers.