So I want to attach a file to the email in Mailer library. As I see in the Mailer description, it says that you can attach files, but I don't see how:
final message = Message()
..from = Address(username)
..recipients.add('dest#example.com') //recipent email
..ccRecipients.addAll(['destCc1#example.com', 'destCc2#example.com']) //cc Recipents emails
..bccRecipients.add(Address('bccAddress#example.com')) //bcc Recipents emails
..subject = 'Test Dart Mailer library :: 😀 :: ${DateTime.now()}' //subject of the email
..text = 'This is the plain text.\nThis is line 2 of the text part.' //body of the email
How would I attach a file here (csv file for example)?
You can check this
Iterable<Attachment> toAt(Iterable<String> attachments) =>
(attachments ?? []).map((a) => FileAttachment(File(a)));
// Create our message.
final message = Message()
..from = Address('$username#gmail.com', 'My name 😀')
..recipients.addAll(toAd(tos))
..ccRecipients.addAll(toAd(args[ccArgs] as Iterable<String>))
..bccRecipients.addAll(toAd(args[bccArgs] as Iterable<String>))
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>"
..attachments.addAll(toAt(args[attachArgs] as Iterable<String>));
Related
hello i'm trying to send email with attachment using mailer flutter so i i specified the file path but when the email is sent i find just the text without the attachment what is wrong ? . this the code :
final equivalentMessage = Message()
..from = Address(username, '***** ')
..recipients.add(Address('********'))
//..ccRecipients.addAll([Address('destCc1#example.com'), 'destCc2#example.com'])
//..bccRecipients.add('bccAddress#example.com')
..subject = 'Test Dart Mailer library :: 😀 :: ${DateTime.now()}'
..text = 'This is the plain text.\nThis is line 2 of the text part.'
..html =
'<h1>Test</h1>\n<p>Hey! Here is some HTML content</p><img src="cid:myimg#3.141"/>'
..attachments = [
FileAttachment(File('/path/hello.txt'))
];
I'm trying to send an attachment through flask-mail. The email is sent, the attachment is also present in the email but I cannot open the attached file.
Gmail shows a message Couldn't Load Image when i click on the image attachment.
This is my code :
msg = Message('You have a new Message', sender = 'noreply#gmail.com',
recipients = ['user#gmail.com'])
msg.body = f'''Details are -
Name : {form.name.data}
Email : {form.email.data}
Phone : {form.phone.data}
Template : {form.template.data}
Message : {form.message.data}
'''
msg.attach(
form.details.data.filename,
'application/octect-stream',
form.details.data.read())
mail.send(msg)
Where am I wrong ?
I try to use the sample in the robotframework-imaplibrary:
Open Mailbox host=imap.domain.com user=${mail} password=${PW}
${LATEST} = Wait For Email sender=${sender} timeout=300
${parts} = Walk Multipart Email ${LATEST}
:FOR ${i} IN RANGE ${parts}
\\ Walk Multipart Email ${LATEST}
\\ ${content-type} = Get Multipart Content Type
\\ Continue For Loop If '${content-type}' != 'text/html'
\\ ${payload} = Get Multipart Payload decode=True
\\ Should Contain ${payload} your email
\\ ${HTML} = Open Link From Email ${LATEST}
\\ Should Contain ${HTML} Your email
Close Mailbox
I get the error for the 3rd line ( ${parts} = Walk Multipart Email ${LATEST} ) after the test run:
TypeError: initial_value must be str or None, not bytes
Does anyone know a working example of an email checking?
I have a flask application, where I want to send an email, along with some data fetched from a form. Everything works fine, but the issue is, that when the email is received the HTML code is not rendered it is only displayed the raw code. Here is what I have done so far
if google_response['success']: #this line is used for a ReCaptcha response
msg = Message('Thank you for contacting me', sender='(my email address is put here as a string)', recipients=[request.form['email']])
name = request.form['name']
msg.body = render_template('email.html', name=name)
mail.send(msg)
return render_template('index.html')
else:
return render_template('index.html')
What, am I doing wrong?
I am assuming this has to do with how you are creating your email. You should be using a Multipart Email to do so. My guess would be that you're using using your HTML as the text for the email and not actually attaching it to the email.
Since you haven't provided us with any of that code, I'll give you an example of how to generate an email that includes HTML formatting.
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
to_address = ''
from_address = ''
msg = MIMEMultipart('alternative')
msg['Subject'] = ''
msg['From'] = from_address
msg['To'] = to_address
text = ''
html = 'your HTML code goes here'
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('', '')
mail.sendmail(to_address, from_address, msg.as_string())
mail.quit()
We want to send email using from email address instead of smtp email address...
I tried to send mail where from email address and smtp authenticated email address are different.
It gives me error.
You can based on the following code below. Hope it helps
MailMessage mailMessage = new MailMessage();
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress("sender#domain.com", "Subject"); // You can try changing this to the email address you want
mailMessage.ReplyToList.Add("sender#domain.com"); // Here you can add reply to
mailMessage.Subject = "ENQUIRY - " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
mailMessage.Body = ""; // The body of email
SmtpClient smtpClient = new SmtpClient("mail.company-domain.com");
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Port = 587; // We used this port instead of port 25
smtpClient.Send(mailMessage);