Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Sending an Email with Python3 via smtplib: Difference between revisions

From John's Wiki
Created page with "It possible to use the module [https://docs.python.org/3/library/smtplib.html <code>smtplib</code>] to send an Email with python3. <pre> #!/usr/bin/env python3 import smtplib from email.message import EmailMessage to_addr = 'to@example.com' from_addr = 'from@example.com' mail_server = 'mail.example.com' passwd = 'YOUR_MAILBOX_PASSWORD' message = EmailMessage() message["To"] = to_addr message["From"] = from_addr message["Subject"] = 'Sent with Python & Smtpli..."
 
No edit summary
 
Line 1: Line 1:
It possible to use the module [https://docs.python.org/3/library/smtplib.html <code>smtplib</code>] to send an Email with python3.
It possible to use the module [https://docs.python.org/3/library/smtplib.html <code>smtplib</code>] to send an Email with python3.


<pre>
<syntaxhighlight lang="python" line>
#!/usr/bin/env python3
#!/usr/bin/env python3


Line 22: Line 22:
s.sendmail(to_addr, from_addr, message.as_string())
s.sendmail(to_addr, from_addr, message.as_string())
s.quit()
s.quit()
</pre>
</syntaxhighlight>


[[Category: Sending an Email with...]]
[[Category: Sending an Email with...]]

Latest revision as of 21:40, 28 November 2024

It possible to use the module smtplib to send an Email with python3.

#!/usr/bin/env python3

import smtplib 
from email.message import EmailMessage

to_addr = 'to@example.com'
from_addr = 'from@example.com'
mail_server = 'mail.example.com'
passwd = 'YOUR_MAILBOX_PASSWORD'

message = EmailMessage()
message["To"]      = to_addr
message["From"]    = from_addr
message["Subject"] = 'Sent with Python & Smtplib'
message.set_payload('This is a test message!')

s = smtplib.SMTP_SSL(mail_server, 465)
s.login(to_addr, passwd)
s.sendmail(to_addr, from_addr, message.as_string())
s.quit()