I´ve got a pdf file as ByteArray and I want to know if there´s a way to attach it without creating the main file on the server.
The code provided by the Play documentation only allows real files to be attached.
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("A pdf document");
attachment.setPath(Play.getFile("rules.pdf").getPath());
I´m using the Playframework Mail module.
Thanks!
Since Play 1.x uses the Apache Commons Email library under the hood, you could use the MultiPartEmail#attach(DataSource ds, String name, String description) method:
import org.apache.commons.mail.*;
// create the mail
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe#somewhere.org", "John Doe");
email.setFrom("me#apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// get your inputstream from your db
InputStream is = new BufferedInputStream(MyUtils.getBlob());
DataSource source = new ByteArrayDataSource(is, "application/pdf");
// add the attachment
email.attach(source, "somefile.pdf", "Description of some file");
// send the email
email.send();
Upcoming Play Version 1.3 will introduce a method attachDataSource(), which can be called from within a Mailer class. This will allow you to attach a ByteArray as an attachment to emails easily without the need to save them to the disk first or without the need to use the Apache Commons Emails. You can then use the "standard" Play way.
Here is the corresponding feature request in the play bugtracker:
http://play.lighthouseapp.com/projects/57987/tickets/1500-adding-maillerattachdatasource-functionality
Related
I have an aspnetboilerplate template, .netcore & angular (free version). I am trying to find a way to attach a word document to an email using the IEmailSender but cant find the proper way of doing so. Have already checked the Email Sending but there is no hint for attaching file to an email.
Does anyone have a sample code that could possibly share with me?
Here is the code snippet to send an email with attachment:
MailMessage mail = new MailMessage
{
Subject = "Subject",
Body = "Message",
IsBodyHtml = true,
To = { "toaddress#gmail.com"},
From = new MailAddress("fromaddress#gmail.com")
};
mail.Attachments.Add(new Attachment(_env.WebRootPath + "\\pp.jpg"));
_emailSender.SendAsync(mail);
You can directly construct a MailMessage and pass it to IEmailSender.SendAsync(mailMessage).
See https://github.com/aspnetboilerplate/aspnetboilerplate/blob/94ebd48fd959cd460d97b809317a959e45c94067/src/Abp/Net/Mail/EmailSenderBase.cs#L66
If you are using MailKit, the underlying implementation will convert the Mail message object into MimeMessage and send it via Mailkit
See https://github.com/aspnetboilerplate/aspnetboilerplate/blob/94ebd48fd959cd460d97b809317a959e45c94067/src/Abp.MailKit/MailKitEmailSender.cs#L42-L50
MimeMessage implementation
https://github.com/jstedfast/MimeKit/blob/bcc7030b61c0c83a10eab7e7a5d689efd923038d/MimeKit/MimeMessage.cs#L3494
I'm not sure if this is possible, but I need to return an email message from a web api controller. Essentially, it would then allow the user the open the file (an eml or msg), make some changes and then send it to the relevant person.
Code wise I have a service that returns a MailMessage.
I have a controller that returns a pdf, using the file's byte array as it's content, but a mail message doesn't seem the easiest thing to convert.
Is this possible? I would rather not write the message to disk first if I can help it, but I could if this is the only solution.
I have just faced the same problem, and I resolved it like this:
var stream = new MemoryStream(); // this has to contain your file content
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(stream.GetBuffer())
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("message/rfc822");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.eml"
};
If you want to know how to generate emails and do not send the but save them to disk instead, you can check the solution proposed here: How to save MailMessage object to disk as *.eml or *.msg file
I'm sending an email using phpmailer. I have web service to generate pdf. This pdf is not uploading or downloading to anywhere.
PDF url is like
http://mywebsite/webservices/report/sales_invoice.php?company=development&sale_id=2
I need to attach this dynamic pdf url to my email.
My email sending service url is like
http://mywebsite/webservices/mailservices/sales_email.php
Below is the code which i am using to attach the pdf.
$pdf_url = "../report/sales_invoice.php?company=development&sale_id=2";
$mail->AddAttachment($pdf_url);
Sending message is working but pdf doesn't attached. It gives below message.
Could not access file: ../report/sales_invoice.php?company=development&sale_id=2
I need some help
To have the answer right here:
As phpmailer would not auto-fetch the remote content, you need to do it yourself.
So you go:
// we can use file_get_contents to fetch binary data from a remote location
$url = 'http://mywebsite/webservices/report/sales_invoice.php?company=development&sale_id=2';
$binary_content = file_get_contents($url);
// You should perform a check to see if the content
// was actually fetched. Use the === (strict) operator to
// check $binary_content for false.
if ($binary_content === false) {
throw new Exception("Could not fetch remote content from: '$url'");
}
// $mail must have been created
$mail->AddStringAttachment($binary_content, "sales_invoice.pdf", $encoding = 'base64', $type = 'application/pdf');
// continue building your mail object...
Some other things to watch out for:
Depending on the server response time, your script might run into timing issues. Also, the fetched data might be pretty large and could cause php to exceed its memory allocation.
in grails-mail plugin you need do define your inline image data in your service, assuming you are using grails mail from a service.
you do this like so in your service.groovy
inline 'header', 'image/jpg', new File('./web-app/images/mailAssets/alert_header_pre.png')
inside your service definition, lets say:
def mailService
def contactUser(userName, email) {
mailService.sendMail {
multipart true
to email
from "marc.heidemann#live.de"
subject "Hello from grails-mail"
text "Hallo from grails-mail multipart text modus"
html view:"/alert/test", model:[name:userName]
inline 'header', 'image/jpg', new File('./web-app/images/mailAssets/alert_header_pre.png')
inline 'footer', 'image/jpg', new File('./web-app/images/mailAssets/suchebottomre.gif')
}
}
for now, the app is rendering the footer and the header image, Ok.
alright, now the plan of the planners of this project is to render profile pictures from database (about 15.000 users) in their emails - can and if then how can this be achieved without declaring every user's profile picture inside the service.groovy? Furthermore those pictures are stored outside of my app at amazon s3. might this be a boundary of mail plugin or is it possible to get this working? What would you offer those planning and creative guys as an alternative if it is not possible to do so? any opinions are welcome.
Loop through your users.
Get the corresponding picture from S3 using the grails-aws-plugin.
Insert picture into email
Send mail using the mail-plugin
That way you don't have to declare it in the service. You can download the pictures from S3 to use them as inline pictures or you could use a url provided by S3.
To access the file for inline usage:
def file = aws.s3().on(bucket).get(name, path)
To get a public url:
def url = aws.s3().on(bucket).url(name, path)
I have a fileUploader widget that I'm using to select an xml file. I then have a button that calls my handler in the viewImpl class when the user submits the selected file. If I understand things correctly, from there I do a submit from the formPanel and the file is on the server.
#UiHandler("calculateComplexityButton")
void onClickCalculateComplexity(ClickEvent e){
formPanel.submit();
//How do I get the inputStream back to here????
presenter.getTask(inputStream);
}
My problem is how do I get the inputStream off the server? I tried using an RPC call for all this, but when I try to get the inputStream I'm not pulling anything off the server. I tried:
inputStream = request.getInputStream();
but it appears to be empty. Any ideas on this?
I dropped the RPC code and used a simple HTTPRequest I found here. That gets me to the servlet, but the request doesn't have the file stream. When I reach this line in the code:
FileItemIterator iter = upload.getItemIterator(request); //Nothing is here in iter.
You can not make an upload via RPC, thats why you have to submit your form to a servlet.
final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.setAction("/upload");
So, when you do form.submit() it will send your file to the Action(Servlet). In the server side you can use the lib form apache (commons-fileupload). You have many different way to get your file, you can save on disk, read on memory....