Categories
Python Answers

How to send email to multiple recipients using Python smtplib?

Sometimes, we want to send email to multiple recipients using Python smtplib.

In this article, we’ll look at how to send email to multiple recipients using Python smtplib.

How to send email to multiple recipients using Python smtplib?

To send email to multiple recipients using Python smtplib, we can use the sendmail method.

For instance, we write:

import smtplib
from email.mime.text import MIMEText

s = smtplib.SMTP('smtp.live.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())

We create the SMTP instance by using the SMTP server address as the argument.

Then we create the message with the MIMEText class.

Next, we set the subject of the message with:

msg['Subject'] = "subject line"

We set the sender email address with:

msg['From'] = sender

And we set the recipients’ emails with:

msg['To'] = ", ".join(recipients)

We combine the recipients into a string with join.

Finally, we send the email with:

s.sendmail(sender, recipients, msg.as_string())

Conclusion

To send email to multiple recipients using Python smtplib, we can use the sendmail method.

Categories
Python Answers

How to make an immutable object in Python?

Sometimes, we want to make an immutable object in Python.

In this article, we’ll look at how to make an immutable object in Python.

How to make an immutable object in Python?

To make an immutable object in Python, we can create a named tuple.

For instance, we write:

import collections

Immutable = collections.namedtuple("Immutable", ["a", "b"])
i = Immutable(a=1, b=2)
print(i)

to call collections.namedtuple to create the Immutable class with a constructor that has a and b as parameters.

Then we use the Immutable constructor by calling it with a and b.

Therefore, i is Immutable(a=1, b=2) according to print.

Conclusion

To make an immutable object in Python, we can create a named tuple.

Categories
Python Answers

How to trim string whitespace with Python?

Sometimes, we want to trim string whitespace with Python.

In this article, we’ll look at how to trim string whitespace with Python.

How to trim string whitespace with Python?

To trim string whitespace with Python, we can use the string’s strip method to trim leading and trailing whitespace.

rstrip strips trailing whitespace only.

And lstrip strips leading whitespace only.

For instance, we write:

s = "  \t a string example\t  "
s1 = s.strip()
s2 = s.rstrip()
s3 = s.lstrip()
print(s1)
print(s2)
print(s3)

We call strip, rstrip and lstrip on s to strip leading and trailing whitespaces, trailing whitespaces, and leading whitespaces respectively.

And then we assign the returned string to s1, s2 and s3.

Therefore, we get:

a string example
     a string example
a string example     

from the print output.

Conclusion

To trim string whitespace with Python, we can use the string’s strip method to trim leading and trailing whitespace.

rstrip strips trailing whitespace only.

And lstrip strips leading whitespace only.

Categories
Python Answers

How to send file using POST from a Python script?

Sometimes, we want to send file using POST from a Python script

In this article, we’ll look at how to send file using POST from a Python script

How to send file using POST from a Python script?

To send file using POST from a Python script, we can use the requests module.

For instance, we write:

import requests

with open('test1.png', 'rb') as f:
    r = requests.post('http://httpbin.org/post', files={'test1.jpg': f})

We open the file with open.

And we open the file with read permission with 'rb'.

Then we call requests.post to make a POST request.

And we set the files parameter to a dictionary containing the file to send the file as the request payload.

Conclusion

To send file using POST from a Python script, we can use the requests module.

Categories
Python Answers

How to split a string of space separated numbers into integers with Python?

Sometimes, we want to split a string of space separated numbers into integers with Python.

In this article, we’ll look at how to split a string of space separated numbers into integers with Python.

How to split a string of space separated numbers into integers with Python?

To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.

For instance, we write:

n = list(map(int, "42 0".split()))
print(n)

We call split to split the string into a list.

Then we call map with int to map the list entries into integers.

Finally, we call list to convert the map into a list.

Therefore, n is [42, 0].

Conclusion

To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.