In the previous post we learned about the SMTP server connection and sending message. Now we'll learn about sending the email with different message substypes.
Considering the message subtypes
A text message could be formatted as plain text, or it might actually be an HTML page. You wouldn’t know from just seeing the type, so messages require a subtype. The type is text and the subtype is html when you send an HTML page to someone. The type and subtype are separated by a forward slash, so you’d see text/html if you looked at the message. The following sections show how to create two messages. The first message is a plain-text message and the second message uses HTML formatting.
1. A text message
The following program describe how to create a simple text message using Python:
from email.mime.text import MIMEText
import smtplib
msg = MIMEText("Howdy!")
msg['Subject'] = "Update status"
msg['From']='Vivek <vivek@gmail.com>'
msg['To'] = 'Satya <satya@gmail.com>'
s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login(msg['From'], 'xyz123')
s.sendmail('Vivek <vivek@gmail.com>',['Satya <satya@gmail.com'],msg.as_string())
print("Message Sent!")
When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . . .
2.Sending an Email With an Attachment
In the following program, we are going to send an email with an image attachment. The process is similar to sending a plain text email.
Refer to the following program:
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
password = "your_password"
msg['From'] = "vivek@covrisolutions.com"
msg['To'] = "satya@covrisolutions.com"
msg['Subject'] = "Photos"
msg.attach(MIMEImage(open("m2.jpg",'rb').read()))
server = smtplib.SMTP('m1.hs9.in',465)
server.starttls()
server.login(msg['From'], '123456789')
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print("Message Sent!")
The MIMEImage class is a subclass of MIMENonMultipart which is used to create MIME message objects of image types. When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . . .
3. Sending an HTML message
An HTML message is basically a text message with special formatting. The following program create an HTML e-mail to send off:
from email.mime.text import MIMEText
import smtplib
msg = MIMEText(
"<h1>A Heading</h1><p>Hello There!</p>","html")
msg['Subject'] = 'A Test HTML Message'
msg['From']='vivek@covrisolutions.com'
msg['To'] = 'satya@covrisolutions.com'
s = smtplib.SMTP('m1.hs9.in',465)
s.sendmail('SenderAddress', ['RecipientAddress'],msg.as_string())
print("Message Sent!")
The example follows the same flow as the text message example in the previous section. However, notice that the message now contains HTML tags. You create an HTML body, not an entire page. This message will have an H1 header and a paragraph.
The most important part of this example is the text that comes after the message. The "html" argument changes the subtype from text/plain to text/html, so the recipient knows to treat the message as HTML content. If you don’t make this change, the recipient won’t see the HTML output.
When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . .
Here I am ending today's topic. We'll continue with this topic and discuss IMAP and imapclient module in the next post. So till we meet next keep practicing and learning Python as Python is easy to learn!
Considering the message subtypes
A text message could be formatted as plain text, or it might actually be an HTML page. You wouldn’t know from just seeing the type, so messages require a subtype. The type is text and the subtype is html when you send an HTML page to someone. The type and subtype are separated by a forward slash, so you’d see text/html if you looked at the message. The following sections show how to create two messages. The first message is a plain-text message and the second message uses HTML formatting.
1. A text message
The following program describe how to create a simple text message using Python:
from email.mime.text import MIMEText
import smtplib
msg = MIMEText("Howdy!")
msg['Subject'] = "Update status"
msg['From']='Vivek <vivek@gmail.com>'
msg['To'] = 'Satya <satya@gmail.com>'
s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login(msg['From'], 'xyz123')
s.sendmail('Vivek <vivek@gmail.com>',['Satya <satya@gmail.com'],msg.as_string())
print("Message Sent!")
When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . . .
2.Sending an Email With an Attachment
In the following program, we are going to send an email with an image attachment. The process is similar to sending a plain text email.
- Create an SMTP object for connection to the server.
- Log in to your account.
- Define your message headers and login credentials.
- Create a MIMEMultipart message object and attach the relevant headers to it, i.e. From, To, and Subject.
- Read and attach the image to the message MIMEMultipart object.
- Finally, send the message.
Refer to the following program:
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
password = "your_password"
msg['From'] = "vivek@covrisolutions.com"
msg['To'] = "satya@covrisolutions.com"
msg['Subject'] = "Photos"
msg.attach(MIMEImage(open("m2.jpg",'rb').read()))
server = smtplib.SMTP('m1.hs9.in',465)
server.starttls()
server.login(msg['From'], '123456789')
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print("Message Sent!")
The MIMEImage class is a subclass of MIMENonMultipart which is used to create MIME message objects of image types. When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . . .
3. Sending an HTML message
An HTML message is basically a text message with special formatting. The following program create an HTML e-mail to send off:
from email.mime.text import MIMEText
import smtplib
msg = MIMEText(
"<h1>A Heading</h1><p>Hello There!</p>","html")
msg['Subject'] = 'A Test HTML Message'
msg['From']='vivek@covrisolutions.com'
msg['To'] = 'satya@covrisolutions.com'
s = smtplib.SMTP('m1.hs9.in',465)
s.sendmail('SenderAddress', ['RecipientAddress'],msg.as_string())
print("Message Sent!")
The example follows the same flow as the text message example in the previous section. However, notice that the message now contains HTML tags. You create an HTML body, not an entire page. This message will have an H1 header and a paragraph.
The most important part of this example is the text that comes after the message. The "html" argument changes the subtype from text/plain to text/html, so the recipient knows to treat the message as HTML content. If you don’t make this change, the recipient won’t see the HTML output.
When we run this program we get the following output-
Message Sent!
------------------
(program exited with code: 1)
Press any key to continue . .
Here I am ending today's topic. We'll continue with this topic and discuss IMAP and imapclient module in the next post. So till we meet next keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment