'list' object has no attribute 'encode - python-3.x

Can somebody help me?
i want to attach a excel file for an email sender in python but gives everytime an error. I looked online and everywhere it said it like it is in my code. can somebody tell me whats wrong
thank you verry much
message = MIMEMultipart()
message['Subject'] = 'Maandelijks rapport'
message['To'] = receiver_email
message['From'] = sender_email
message.attach(MIMEText('In de bijlage vind je het maandrapport terug!'))
part = MIMEBase('application', "octet-stream")
part.set_payload(open('test.xls', "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Desposition', 'attachment; filename="test.xls"')
message.attach(part)
#send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
smtp.login(sender_email, password)
print('connecting with smtp server...')
smtp.login(sender_email, password)
smtp.sendmail(sender_email, receiver_email, message.as_string())
print('connection succeed!')
print('email has been sent!')
smtp.quit()

Related

Sending email via smtp only works if sender and recipient are the same v.v

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
user = 'user#icloud.com'
password = 'apppassword'
host = 'smtp.mail.me.com'
text = 'E-Mail Body'
subject = 'This is a Subject'
to_emails = ['user2#icloud.com']
from_email = 'John Doe <user#icloud.com>'
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = 'user2#icloud.com'
msg['Subject'] = subject
body = MIMEText(text, 'plain')
msg.attach(body)
msg_str = msg.as_string()
server = smtplib.SMTP(host, port=587)
server.starttls()
server.ehlo()
server.login(user, password)
server.sendmail(from_email, to_emails, msg_str)
server.quit()
I am trying to program an email client in python.
At the moment I fail to send via iCloud.
The email only arrives if I send the email to the iCloud mail that sends it. So to myself.
So you could say it's the loneliest email client in the world right now.
But all kidding aside, I don't get it. Is it me? Is it iCloud?

Attachment in email became "noname"

I tried sending email using python and put attachment, but when it sent the attachment became noname without any extension.
message = MIMEMultipart()
message ['From'] = sender_email
message ['To'] = ', '.join(receiver_email)
message ['Subject'] = 'Python Email'
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'Final Project - Basic Python.pdf'
attach_file = open('Final Project - Basic Python.pdf', 'rb')
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)

Django Aws Ses email send issue b'Invalid MAIL FROM address provided'

In my settings.py i have a mail congiguration Like :
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_PORT = os.getenv('EMAIL_PORT') //465
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')
And in my code i am using like :
connection = get_connection(
host=settings.EMAIL_HOST,
port=settings.EMAIL_PORT,
username=settings.EMAIL_HOST_USER,
password=settings.EMAIL_HOST_PASSWORD,
use_tls=settings.EMAIL_USE_TLS,
)
print('11111111')
print(connection)
print('222222222')
mail = send_mail('diditwork?', 'test message', settings.EMAIL_HOST_USER, [userObj.email], connection=connection)
But in Result i am getting the error Like :
File "/var/www/html/gelmeko/myenv/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 125, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python3.6/smtplib.py", line 867, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'Invalid MAIL FROM address provided', 'AKI**************')
Can any one please help me related this ?? what i am doing wrong here
i am sending the mail through AWS SES credentials.
Try :-
Use EMAIL_PORT = 587` instead of `465`

while sending the mail using smtplib in python got the error (11004, 'getaddrinfo failed')

I'm sending a mail using a python script on a development server which don't have internet connection but i check the connectivity with the SMTP server by pinging the server and it is connected. But i'm getting error (11004, getaddrinfo failed) while connecting to it using smtplib.
Please help me in this issue!
Below is the code im using
msg = MIMEMultipart()
msg["From"] = "sender"
msg["To"] = 'recipient'
msg["Subject"] = "Testing Email"
message = 'Hi,\n \nThis is testing mail.'
ctype, encoding = mimetypes.guess_type(file)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(file, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename='report.xlsx')
msg.attach(MIMEText(message, 'plain'))
msg.attach(attachment)
try:
server = smtplib.SMTP(server_name)
server.set_debuglevel(0)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
except Exception as e:
print(e.args)
Try this:
server = smtplib.SMTP("smtp.google.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()

Not able to send the mail without password authentication in python

I am not able to send the mail without password authentication in python.Its printing the last message but mail is not sent.Below is the code that i am using.Anyone please help i am stuck because of this. Thanks in advance.
import smtplib
fromaddr = 'xyz#gmail.com'
toaddrs = ['abc#gmail.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: 'testing'
This is a test
.
'''
msg = msg.format(fromaddr =fromaddr, toaddr = toaddrs[0])
server = smtplib.SMTP("gmail-smtp-in.l.google.com:25")
server.starttls()
server.ehlo("example.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit/close()
print("mail sent")

Resources