I have a list of persons with individual emails (on different kind of mail services, for example gmail and hotmail). I want to send mail from their respective email addresses, like this:
mailService.sendMail {
from "hereMail#some.com"
}
In order to send mail I must set the configuration in Config.groovy. Should I maintain all emails configuration in Config.groovy file? or some other solution exist for this problem?
The configuration only allows the sending from one SMTP server. The account that sends the email is not necessarily the "from" address even though it is being emailed from that account. You should be able to use one account as the SMTP server and change the "from" as needed.
The configuration item sets the "default" from address for outgoing messages. The plugin provides a DSL that is used to specify the components of the message, including a specific From address if you want. If you don't provide a from specification in the message DSL, then it uses the configuration specified value.
Here is a snippet of code that I use in my messaging system to set a user account supplied from address on outgoing messages:
mailMessage = mailService.sendMail {
multipart true
if (toAddresses) { to toAddresses }
if (ccAddresses) { cc ccAddresses }
if (bccAddresses) { bcc bccAddresses }
from messageSpecification.from
subject messageSpecification.subject
if (messageSpecification.plainText) { text messageSpecification.plainText }
if (messageSpecification.htmlText) { html messageSpecification.htmlText }
messageSpecification.attachments.each {
attach(it.filename, it.mediaType, it.data)
}
}
Simply replace the messageSpecification.from reference to your specific from address and you are good to go.
Related
I have implemented a platform using rails, and the goal is to send thousands of emails to customers with one click. The concept is that an email array runs each loop and inside each loop runs send email functionality like below.
#emails = ['abc#gmai.com', 'abc#example.com'] # More than 3 thousands
#emails.each do |email|
aws_email_sender(email, #email_subject, #email_body_html)
end
And the email function is like below:
def aws_email_sender(recipient, subject, htmlbody)
sender = "hello#example.com"
awsregion = "ap-west-1"
# The HTML body of the email.
htmlbodycontent = "#{htmlbody}"
# The email body for recipients with non-HTML email clients.
textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby."
# Specify the text encoding scheme.
encoding = "UTF-8"
# Create a new SES resource and specify a region
ses = Aws::SES::Client.new(region: awsregion)
# Try to send the email.
begin
# Provide the contents of the email.
resp = ses.send_email({
destination: {
to_addresses: [recipient]
},
message: {
body: {
html: {
charset: encoding,
data: htmlbodycontent
},
text: {
charset: encoding,
data: textbody,
},
},
subject: {
charset: encoding,
data: subject,
},
},
source: sender,
});
# If something goes wrong, display an error message.
rescue Aws::SES::Errors::ServiceError => error
puts "Email not sent. Error message: #{error}"
end
end
The email is sending well by AWS but my rails application has gone down like
A timeout occurred, error code 524
I couldn't get the breaking point, why has my application gone down every time?
Thanks in Advance
If 524 is an HTTP status code then it means...
Cloudflare was able to make a TCP connection to the website behind them, but it did not reply with an HTTP response before the connection timed out.
Meaning your Rails app is behind a Cloudflare proxy. Cloudflare received an HTTP request, forwarded it to your app, waited around for your app to respond, but your app never did. A more detailed explanation can be found here.
Probably because it's trying to send emails to 3000 people one-by-one.
There's two strategies to fix this.
Use Bulk Email
Since the content of the email is the same for everyone, use an email template to send bulk email using the #send_bulk_templated_email method.
You can send to up to 50 addresses at a time, so use #each_slice to loop through emails in slices of 50.
This will be more efficient, but your app will still be waiting around for 3000/50 = 60 AWS API calls. At worst it will still time out. At best the user will be waiting around for a form submission.
Use A Background Job
Anytime your app needs to do something that might take a lot of time, like using a service or a large database query, consider putting it into a background job. The Rails app queues up a job to send the emails, and then it can respond to the web request while the mailing is handled in the background. This has other advantages: errors calling the service won't cause an error for the user, and failed jobs due to a temporary service outage can automatically be retried.
In Rails this is done with ActiveJob and you could write a job class to send your mail.
Use ActionMailer
However, Rails also offers a class specifically for sending email in the background: ActionMailer. You can have ActionMailer use AWS with the aws-sdk-rails gem.
config.action_mailer.delivery_method = :ses
I want to send NewsLetter Emails to users.
I have done like this:
public ActionResult SendNewsLetter()
{
_userMailer.NewsLetter().Send();
return View();
}
and in userMailer class:
public virtual MvcMailMessage NewsLetter(string userEmail)
{
//ViewBag.Data = someObject;
return Populate(x =>
{
x.Subject = "NewsLetter";
x.ViewName = "NewsLetter";
x.To.Add("hello#mydomain.mobi");
x.Bcc.Add(userEmail);
});
}
I add submitted newsletter emails to bcc.
but when I send it I encounter this issue:
Bad sequence of commands. The server response was: This mail server
requires authentication when attempting to send to a non-local e-mail
address. Please check your mail client settings or contact your
administrator to verify that the domain or address is defined for this
server.
if remove bbc I can send email normaly because I have provided authentications for hello#mydomain.mobi in web.config.
<system.net>
<mailSettings>
<!-- Method#1: Configure smtp server credentials --><smtp deliveryMethod="Network" from="hello#mydomain.com">
<network host="mydomain.com" port="25" userName="hello#mydomain.com" password="123456" enableSsl="false" />
</smtp>
</mailSettings>
but Im amazed why I cannot send email to other emails?
does somebody have any idea?
The error message that you're encountering isn't related to MVCMailer at all. I'd contact the system administrator of the SMTP server you're attempting to use and see if you're allowed to use the hello#mydomain.com user that you're authenticating with to send email to the value of userEmail. A lot of SMTP servers that web servers use will often have limits or restrictions on email to prevent spamming.
When consuming messages using the plugin, you can access the raw Message and its headers/properties.
When sending messages using the rabbitSend method, it appears from the documentation (http://grails-plugins.github.com/grails-rabbitmq/docs/manual/ref/All%20Classes/rabbitSend.html) that you can only set the exchange name, routing key and message body.
How can these headers/properties be set when sending a message using the rabbitSend method?
At present, it looks like you need to use the underlying rabbitTemplate.convertAndSend() method. The link to the RabbitTemplate Javadoc in the plugin's documentation is broken at the moment, it should point to http://static.springsource.org/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.html
I found an example of setting the message properties using the rabbitTemplate.convertAndSend() method on the Grails JIRA http://jira.grails.org/browse/GPRABBITMQ-7
rabbitTemplate.convertAndSend "amq.direct", "work", payload, ({ Message msg ->
msg.messageProperties.replyTo = new Address("work.reply")
return msg
} as MessagePostProcessor)
I added to Config.groovy default mail body by adding this line:
grails.mail.default.body = "Test body"
and then I am passing adress, subject and body from form by params in a sendMail method:
sendMail {
to params.adress
subject params.title
body params.body
}
It works fine if params.body is not empty, but when user ignores this field the default body is not added to mail - the mail cant be sent without body.
Class
grails.plugin.mail.GrailsMailException
Message
message has no content, use text(), html() or body() methods to set content
What mistake I did here? Is there some special way I need to handle default content of mail body, subject or other fields?
The mail plugin has special support for default.to and default.from but not default.body (or subject, or anything else). You could handle it yourself:
sendMail {
to params.adress
subject params.title
body(params.body ?: grailsApplication.config.grails.mail.default.body)
}
but the plugin won't do it for you.
I am attempting to attach an excel spreadsheet to an email programmatically, and then launch the default blackberry email client with the message as an argument. Unfortunately, I receive the error: "Email service does not support these types of attachments. Change the Send Using field or remove the attachments." The send button is not present, and there is no "Send" option in the menu; this is blocking the ability to send the email.
This error occurs when I load the package onto my physical blackberry phone, as well as in the simulator.
I am able to send the email without a hitch if I use the API instead (the commented transport.send line).
Any and all input would be greatly appreciated, and if I've overlooked some details please let me know.
public Email()
{
try{
message = new Message();
multipart = new Multipart(); //Multi part can hold attachment AND body (and more)
subject = "Service Change Request";
multipart.addBodyPart( new TextBodyPart( multipart, "Hi XXXXXX, \n Here are the details for CLIENT" ) );
byte[] data = null;
InputStream stream = MyAPP.getUiApplication().getClass().getResourceAsStream("/blank_form.xls");
data = IOUtilities.streamToBytes(stream);
stream.close();
multipart.addBodyPart( new SupportedAttachmentPart( multipart, "application/octet-stream", "ServiceUpdate.xls", data ) );
Address recipients[] = new Address[1];
recipients[0]= new Address("*******#gmail.com", "user");
message.setSubject(subject);
message.setContent( multipart );
message.addRecipients(Message.RecipientType.TO, recipients);
//Transport.send(message);
}catch(Exception e){
}
}
public void send(){
Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, new MessageArguments( message ) );
}
EDIT:
The error comes up because the simulator has no email account configured. It should work just fine on any phone that has an email account properly configured.
I hope this helps and I am not too late to lend a hand on this post.
I've worked with attachments before, and they are a pain to work with in Blckberry.
The only issue I can think of is the MIME type you are trying to use.
"Application/octet-stream", try using the MIME corresponding to the extension of the attachment, for example "application/excel" for .xls files. You can find the complete list here , its the longest one I could find.
There are also some issues with the Blackberry email service and attachments that are mentioned on several Knowledge Base Articles on the official Developers page like this one, they sometimes say that the attachments have to be prefixed with "x-rimdevice" in the file name, like "x-rimdevice-serviceupdate.xls". Although I'm not really sure this affects on outgoing email, but I thought it was worth mentioning.
By the way, I'm trying to use your code for an App I'm coding right now, so I'm kind of hoping it works.