Headers on a mime message must be strings. You have assigned a tuple to From, and a list to To.

Make those strings too:

message['From'] = "Michal <{}>".format(FROM)
message['To'] = ', '.join(TOADDR)
Answer from Martijn Pieters on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › 'tuple' object has no attribute 'encode' for stmplib, two variables into one variable?
r/learnpython on Reddit: 'tuple' object has no attribute 'encode' for stmplib, two variables into one variable?
April 29, 2022 -

I am trying to call the function send_email with the variable message in it and combining it with a string contained in AlertSMSText and assigning it to text as well as html to HTMLEmailBody.

For some reason when I do text = AlertSMSText, message and it gives me the error AttributeError: 'tuple' object has no attribute 'encode' but if I do text = AlertSMSText + message and html = HTMLEmailBody + message it works fine except it ignores \n contained in the string that AlertSMSText has in it.

SendEmailAlert.py

def send_email(message):
  email_subject = JSONFileReader.SubjectEmail  
  sender_add = JSONFileReader.SenderEmail  
  receiver_add = JSONFileReader.ReceiverEmail  
  smsEmail_add = JSONFileReader.SMSEmailAdd  # SMS email
  AlertSMSText = JSONFileReader.AlertSMSText #Alert SMS Text message
  HTMLEmailBody = JSONFileReader.AlertHTMLEmailBody  #Alert Email message in HTML format
  password = JSONFileReader.gmailPass  # Password for the email you're sending from

  # Below is creating the SMTP server object by giving the SMPT server an address and port number
  smtp_server = smtplib.SMTP("smtp.gmail.com", 587)
  smtp_server.ehlo()  # setting the ESMTP protocol
  smtp_server.starttls()  # setting up to TLS connection
  smtp_server.ehlo()  # calling the ehlo() again as encryption happens on calling startttls()
  smtp_server.login(sender_add, password)  # logging into the senders email

  rcpt = smsEmail_add.split(",") + [receiver_add]  # Formats the recipients emails
  # Create message container - the correct MIME type is multipart/alternative.
  msg = MIMEMultipart('alternative')
  msg['Subject'] = email_subject
  msg['From'] = sender_add
  msg['To'] = receiver_add
  msg['Cc'] = smsEmail_add

  message.encode('utf-8')

  # Create the body of the message (a plain-text is for SMS and HTML is for email)
  text = AlertSMSText, message
  html = HTMLEmailBody, message


  # Record the MIME types of both parts - text/plain and text/html.
  part1 = MIMEText(text, 'plain')
  part2 = MIMEText(html, 'html')

  # Attach parts into message container.
  # According to RFC 2046, the last part of a multipart message, in this case
  # the HTML message, is best and preferred.
  msg.attach(part1)
  msg.attach(part2)

  smtp_server.sendmail(sender_add, rcpt, msg.as_string()) #Sends the email

  smtp_server.quit()  #Terminates the SMTP server

 ip= "10.1.50.2"
 send_email(ip + " is down.\n\nPlease reply with 'Ack' to pause alerts for 60 minutes")

In the JSON, the AlertSMSTextvariable equals to "Alert SMS Text": "This is an alert for your services \n\n IP: "

The output when text = AlertSMSText + message is all on one line and ignores newline \n:

This is an alert for your services IP:10.1.50.0 is down. Please reply with 'Ack' to pause alerts for 60 minutes

I want it to be:

This is an alert for your services

IP:10.1.50.0 is down. 

Please reply with 'Ack' to pause alerts for 60 minutes
Discussions

python - How to fix AttributeError: "NoneType" object has no attribute "encode" using smtplib - Stack Overflow
I tried to make a script to allow a user to send the contents of a file via gmail. This is is my code: if input1 == 'mail': path = input("open -- ") with open(path, "r") as file: ... More on stackoverflow.com
🌐 stackoverflow.com
email - Python error: 'tuple' object has no attribute 'encode' - Stack Overflow
What I am doing is taking information from a web page and attempting to put it into an e-mail in a format like: First Name: first \n#first is a variable Last Name: last #last is a variable Below i... More on stackoverflow.com
🌐 stackoverflow.com
python - 'tuple' object has no attribute 'encode' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - 'SMTP' object has no attribute 'encode' - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most · Connect and share knowledge within a single location that is structured and easy to search More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 52137357 › attributeerror-tuple-object-has-no-attribute-encode-python
smtp - AttributeError: 'tuple' object has no attribute 'encode' python - Stack Overflow
307 How to send email to multiple recipients using python smtplib? 1 Python error: 'tuple' object has no attribute 'encode' 0 Trying to send an email using python, getting an IndexError: tuple out of range · 2 Object has no attribute 'encode' 1 AttributeError: 'tuple' object has no attribute 'decode' while trying to decode string ·
🌐
GitHub
github.com › django-ses › django-ses › issues › 170
On email.send(), error 'tuple' object has no attribute 'encode' · Issue #170 · django-ses/django-ses
July 30, 2019 - Actually, turns out this was a problem with my AWS keys being read as a tuple, changing the formatting worked and everything is working as expected.
🌐
Django
code.djangoproject.com › ticket › 26802
#26802 (Sending mails with attachment results in 'bytes' object has no attribute 'encode') – Django
We could easily fix this in Python 3.5 by using a real Charset instance instead of 'utf-8'). Python 2.7 is not affected because there is no charset sniffing with encode(). We are left with Python 3.4, which we could special-case and decode the text before passing it to MIMEText.__init__.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 73197383 › smtp-object-has-no-attribute-encode
python 3.x - 'SMTP' object has no attribute 'encode' - Stack Overflow
Traceback (most recent call last) <ipython-input-59-f34ee49d6941> in <module>() 6 msg['To'] = "@mail.com" 7 msg['Subject'] = "RANDEVU BULUNDU" ----> 8 msg.attach(MIMEText(s, "html")) 9 s.send_message(msg) /usr/lib/python3.7/email/mime/text.py in __init__(self, _text, _subtype, _charset, policy) 32 if _charset is None: 33 try: ---> 34 _text.encode('us-ascii') 35 _charset = 'us-ascii' 36 except UnicodeEncodeError: AttributeError: 'bytes' object has no attribute 'encode'
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
send email when temperature more than 45. - Raspberry Pi Forums
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText me = "[email protected]" you = "[email protected]" user = "[email protected]" pw = "xxxxxx" msg = MIMEMultipart('alternative') msg['Subject'] = "suhu" msg['From'] = me msg['To'] = you text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org" part1 = MIMEText(text, 'plain') msg.attach(part1) s = smtplib.SMTP('smtp.gmail.com:587') s.ehlo() s.starttls() s.login(user,pw) s.sendmail(me, you, msg.as_string()) s.quit() i try to add : ... AttributeError: 'tuple' object has no attribute 'encode' and it must run in looping, because in every 60 second i must upload my data into Firebase.
🌐
CopyProgramming
copyprogramming.com › howto › attributeerror-tuple-object-has-no-attribute-encode
AttributeError: 'tuple' object has no attribute 'encode' - Complete Guide & Solutions 2026 - Attributeerror: 'tuple' object has no attribute 'encode'
December 22, 2025 - The AttributeError: 'tuple' object has no attribute 'encode' is a common Python programming error that occurs when developers attempt to call the method on a tuple object instead of a string. In Python, the method is exclusively a string operation—tuples do not have this method, making the ...
🌐
Reddit
reddit.com › r/django › attributeerror: 'tuple' object has no attribute 'encode'
r/django on Reddit: AttributeError: 'tuple' object has no attribute 'encode'
August 13, 2020 -

I am creating a Contact page for my django project. forms.py has name, subject, sender and message. Here's the view:

And the form:

<form method="POST">

{%csrf_token%}

{{form.as_p}}

<button type="submit">Send</button>

</form>

After I click Send I get this error: AttributeError: 'tuple' object has no attribute 'encode' and I can't seem to figure out what's wrong despite checking other questions of the same type.

I am using google SMTP server, from this link following its instructions.

🌐
Hemispherehouse
hemispherehouse.com › iuw5t6nj › attributeerror-'tuple'-object-has-no-attribute-'encode'-smtplib
attributeerror 'tuple' object has no attribute 'encode' smtplib
Tag: python,email,attributeerror,smtplib. Consider starting a new topic instead. (FYI I got no issue to retrieve my registered module and to restore the input/output tensor.) asked May 11 Danyal Sjoerd 19.2k points Problem: Does anyone have the skills to resolve this error? Thus you need to access the first item in the tuple, which is [0] as Python is zero-based. Calling a function that returns a hash value: AttributeError: 'tuple' object has no attribute 'encode...
🌐
Reddit
reddit.com › r/learnpython › trouble with sending emails...
r/learnpython on Reddit: Trouble with sending emails...
June 2, 2016 -

Hi all! So Ive been trying to make a program that sends me an email via python when something happens but I'm having alot of difficulty with the email part...

I have been reading a heap of different things about how to do it and eventually went with [this one] (https://docs.python.org/2.7/library/email-examples.html).

I filled in my code using a free smtp server and it looks like:

 def SendEmail():
    from email.mime.text import MIMEText
    # Open a plain text file for reading.  For this example, assume that
    # the text file contains only ASCII characters.
    fp = open('message.txt', 'r+')
    # Create a text/plain message
    msg = MIMEText(fp.read())
    fp.close()

    # me == the sender's email address
    # you == the recipient's email address
    msg['Subject'] = 'The contents of %s' % 'test'
    msg['From'] = 'myemaile@gmail.com'
    msg['To'] ='myemail@gmail.com'

    # Send the message via our own SMTP server, but don't include the
    # envelope header.
    s = smtplib.SMTP('smtp-pulse.com')
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit()

inserting my actual email, however when I run it I get the following error:

(501, b'Syntactically invalid HELO argument(s)')

Ive googled this and all I can find is errors due to the input host address or something due to them hosting this incorrectly, but I'm not really sure how to fix it...

Any help would be greatly appreciated :D

EDIT: Okay so u/johninbigd kindly pointed out that the 'TO' field has to be a list, I did forget about that and now I'm getting this error:

AttributeError: 'list' object has no attribute 'encode'

Edit: WOOO finally got it working. The issue was just I had to go into google settings and allow less secure apps or something like that. Also removed all that code and followed the instructions from here:

http://www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/

🌐
Dev2QA
dev2qa.com › home › python send plain text, html content, attached files, embedded images email example
Python Send Plain Text, Html Content, Attached Files, Embedded Images Email Example Windows Tricks
August 3, 2021 - ''' def send_plain_text_email(... = from_addr '''Because to_addrs is a tuple, so you need to join the tuple element to a string with comma separated, otherwise it will throw AttributeError: 'tuple' object has no attribute ...
🌐
Narkive
comp.lang.python.narkive.com › hROupzov › attributeerror-tuple-object-has-no-attribute-encode
AttributeError: 'tuple' object has no attribute 'encode'
The parameters have to be supplied as a second argument to .execute() -- your statement above is creating a tuple "sql" consisting of a string (the insert template) AND a tuple of parameters; I'm guessing you are passing just one argument to .execute(). That argument is a tuple that can't be "encoded" into a valid SQL statement.
🌐
GitHub
github.com › Kludex › python-multipart › issues › 78
AttributeError: 'tuple' object has no attribute 'encode' · Issue #78 · Kludex/python-multipart
February 7, 2024 - I get an error when sending a request from a C# client library stemming from the parse_options_header. This error only started cropping up in 0.0.7 and did not exist beforehand. Here's some sample code to reproduce the error With python-...
Author   lorenpike
🌐
The Coding Forums
thecodingforums.com › archive › archive › python
[email/quoprimime.py] AttributeError: 'tuple' object has no attribute 'lstrip' | Python | Coding Forums
January 6, 2009 - Hello I successfully use the email package to send e-mail from Python scripts, but this script fails when I fetch addresses from an SQLite database where data is Unicode-encoded: ====== from email.MIMEText import MIMEText import smtplib,sys import apsw connection=apsw.Connection("test.sqlite") cursor=connection.cursor() subject = "My subject" f = open("message.txt", "r") message = f.read() f.close() msg = MIMEText(message) msg['Subject'] = subject From = "(e-mail address removed)" msg['From'] = From server = smtplib.SMTP("smtp.acme.com") sql="SELECT email FROM people WHERE email IS NOT NULL" r