Python Forum
Creating Email Message Class in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Email Message Class in Python
#1
My homework is creating an e-mail Message class (we create an e-mail Message class that models a basic email application). The first step in the homework is to define the Message class which includes a sender, recipient, and body. The constructor of this Message class should have the parameters self, sender, and recipient, with values set for sender and recipient and body set as an empty string.

This is what I have so far and I think I am on the right track:

class Message:
  def __init__(self, sender, recipient):
    self.sender = sender
    self.recipient = recipient

p1 = Message("From: John Doe", "To: Jane Doe")

print(p1.sender)
print(p1.recipient)
The return output is simply:

From: John Doe
To: Jane Doe


My teacher did not explain a lot of the other stuff in class so I need help with a couple of things. First, I need help setting up an empty string for the body. I also need to create an "append" method with the parameters "self" and "line" ("line" contains the line of text to add to the body of the email message) and I'm not really sure how to do that. Additionally, the teacher has asked us to create a method to_string that returns a string representation of the entire message and each email message line should be ended with a newline character "\" (no clue what this even means). We also need a helper method str_ok() that validates str parameters.

Some program specifications:
Use mutators set_sender(...), and set_recipient(...) for the sender and recipient instance members
Use append() method for the body instance member
When the set methods detect an invalid string, the mutator returns False and a new default string is stored in that member
When the append method detects an invalid string, no action should be taken
Helper methods:
string_to_string()
def str_ok() (returns True if string's length is <= MAX_LEN (inclusive) and returns False otherwise)

Thank you so much!
Reply
#2
an empty string is as simple as ''
to add an empty string as a attribute body
self.body = ''

a newline character is \n

Tutorials on classes
https://python-forum.io/Thread-Classes-Class-Basics
https://docs.python.org/3/tutorial/classes.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code tp determin a userName and print message jackAmin 4 1,836 Nov-20-2022, 12:03 AM
Last Post: rob101
  Creating class with getters and setters. amandacstr 3 2,355 Jun-19-2019, 07:10 PM
Last Post: nilamo
  Creating a script with a class have specific properties dvldgs05 13 5,842 Oct-15-2018, 08:54 PM
Last Post: dvldgs05
  I want to manage my email with python timfox123 1 2,826 Apr-16-2017, 12:53 PM
Last Post: sparkz_alot
  Please help me!! Can't get rid of error message in Python. TOM 3 5,154 Nov-01-2016, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020