I am working on learning how to send an email via python. I have something set up using EmailMessage
, but I am unsure as to what my error is trying to tell me in the set_content line.
Code:
Email_Address = os.environ.get('Email_User') #email address variable
Email_Password = os.environ.get('Email_Pass') #email password variable
msg = EmailMessage() #making an email class object
msg['Subject'] = 'That_thing' #subject line
msg['From'] = Email_Address #from address line
msg['To'] = Email_Address #to address line. can be made into a list variable for multiple receivers
msg.set_content('Did you get that thing I sent you?') # email body
with open('that_thing.jpeg', 'rb') as f: #rb==read bytes
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
msg.add_attachment(filedata=file_data, maintype='image', subtype=file_type, filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: #using smtp on secure socket layer to log in and send email
smtp.login(Email_Address, Email_Password) #user login information
smtp.send_message(msg) #sending the actual email
Error:
TypeError Traceback (most recent call last)
<ipython-input-6-3336c41d40e5> in <module>
13 file_type=imghdr.what(f.name)
14 file_name=f.name
---> 15 msg.add_attachment(filedata=file_data,maintype='image',subtype=file_type,filename=file_name)
16 with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp: #using smtp on secure socket layer to log in and send email
17 smtp.login(Email_Address,Email_Password) #user login information
~\Anaconda3\lib\email\message.py in add_attachment(self, *args, **kw)
1145
1146 def add_attachment(self, *args, **kw):
-> 1147 self._add_multipart('mixed', *args, _disp='attachment', **kw)
1148
1149 def clear(self):
~\Anaconda3\lib\email\message.py in _add_multipart(self, _subtype, _disp, *args, **kw)
1133 getattr(self, 'make_' + _subtype)()
1134 part = type(self)(policy=self.policy)
-> 1135 part.set_content(*args, **kw)
1136 if _disp and 'content-disposition' not in part:
1137 part['Content-Disposition'] = _disp
~\Anaconda3\lib\email\message.py in set_content(self, *args, **kw)
1160
1161 def set_content(self, *args, **kw):
-> 1162 super().set_content(*args, **kw)
1163 if 'MIME-Version' not in self:
1164 self['MIME-Version'] = '1.0'
~\Anaconda3\lib\email\message.py in set_content(self, content_manager, *args, **kw)
1090 if content_manager is None:
1091 content_manager = self.policy.content_manager
-> 1092 content_manager.set_content(self, *args, **kw)
1093
1094 def _make_multipart(self, subtype, disallowed_subtypes, boundary):
TypeError: set_content() missing 1 required positional argument: 'obj'
The video I was watching has it the same way I do, and they do not get an error.
Any help is appreciated.
Update: first issue resolved. I had my .py as Email making it think it was importing itself. now to figure out attachments.
Update:
I got it to work. After searching through the error I learned that imghdr
has a known issue. Found imghdr / python - Can't detec type of some images (image extension) which has a patch to fix the issue.