import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
server_address = "smtp.gmail.com"
server_port = 587
login, password = "#hidden", "#hidden  "
msg = MIMEMultipart()
msg['From'], msg['To'], msg['Subject'] = "your_email", "recipient_email", "Тема вашего письма"
msg.attach(MIMEText("Содержимое письма", 'plain'))
file_path = "C:\\Users\\Test\\OneDrive\\Desktop\\recording.zip"
with open(file_path, "rb") as file:
part = MIMEBase('application', 'octet-stream')
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename={file_path}")
msg.attach(part)
with smtplib.SMTP(server_address, server_port) as server:
server.starttls()
server.login(login, password)
server.send_message(msg)
#🔒 why doesn't it work and gives an error?P.S this code should take my zip file and send it to me by
27 messages · Page 1 of 1 (latest)
@sharp echo
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
the error is like this (PS C:\Users\Test\OneDrive\Desktop\cartridge> & C:/Users/Test/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Test/OneDrive/Desktop/cartridge /main4.py
Traceback (most recent call last):
File "c:\Users\Test\OneDrive\Desktop\cartridge\main4.py", line 27, in <module>
server.send_message(msg)
~~~~~~~~~~~~~~~~~~~^^^^^
File "C:\Users\Test\AppData\Local\Programs\Python\Python313\Lib\smtplib.py", line 975, in send_message
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^
rcpt_options)
^^^^^^^^^^^^^
File "C:\Users\Test\AppData\Local\Programs\Python\Python313\Lib\smtplib.py", line 876, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (552, b"5.3.4 Your message exceeded Google's message size limits. To view our\n5.3.4 message size guidelines, go to\n5.3.4 https://support.google.com/mail/? p=MaxSizeError 2adb3069b0e04-53e44a45769sm2062888e87.60 - gsmtp", 'your_email')
PS C:\Users\Test\OneDrive\Desktop\cartridge>)
Official Gmail Help Center where you can find tips and tutorials on using Gmail and other answers to frequently asked questions.
The error is pretty clear. What is your question?
Did you read the error?
+-
Did you? It tells you exactly what the problem is.
It even gives you a link that has more information.
thank you
PS C:\Users\Test\OneDrive\Desktop\патрон> & C:/Users/Test/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Test/OneDrive/Desktop/патрон/main4.py
File "c:\Users\Test\OneDrive\Desktop\патрон\main4.py", line 15
file_path = "C:\Users\Test\OneDrive\Desktop\патрон\recording.zip"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
PS C:\Users\Test\OneDrive\Desktop\патрон>
new error
\U is a special character which indicates that the characters that follow it represent a unicode character code. You need to either use // slashes, or a raw string: r"C:\Users..." (note the r).
Neither one nor the other method did not work and there are errors (the same)
PS C:\Users\Test\OneDrive\Desktop\патрон> & C:/Users/Test/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Test/OneDrive/Desktop/патрон/main4.py
Traceback (most recent call last):
File "c:\Users\Test\OneDrive\Desktop\патрон\main4.py", line 27, in <module>
server.send_message(msg)
~~~~~~~~~~~~~~~~~~~^^^^^
File "C:\Users\Test\AppData\Local\Programs\Python\Python313\Lib\smtplib.py", line 975, in send_message
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
rcpt_options)
^^^^^^^^^^^^^
File "C:\Users\Test\AppData\Local\Programs\Python\Python313\Lib\smtplib.py", line 890, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'recipient_email': (553, b'5.1.3 The recipient address <recipient_email> is not a valid RFC 5321\n5.1.3 address. For more information, go to\n5.1.3 https://support.google.com/a/answer/3221692 and review RFC 5321\n5.1.3 specifications. 2adb3069b0e04-53e38e08361sm2227658e87.23 - gsmtp')}
PS C:\Users\Test\OneDrive\Desktop\патрон>
SMTP is an internet standard used by mail servers to send and receive email. SMTP error messages help you understand why a message wasn't sent successfully. If incoming or outgoing messages
You appear to have told it to send an email to a fake address.
no, I specifically hid my email and password (work) so as not to show it here

Oh, then it looks like the email address is malformed in some way.
Everything is ok with the mail, I just checked it
thanks for the work but I was able to solve the problem myself
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.