Reputation: 303
Trying to send email through methods. I have a method
addHeader: headername with: aString
|email|
email:= aString.
'To'= headername ifTrue[ self message: 'To:', with ].
'From'= headername ifTrue[ self message: 'From:', with].
'Subject'= headername ifTrue[ self message:'Subject', with].
My question was Workspace
addHeader:'To' with:'[email protected]'.
addHeader:'From' with:'[email protected]'
When i execute above code one by one. All these values should be added to this method.
message: aString
"Recieves To: [email protected]"
^ message
"next time when it recieves From: [email protected]. How can i concatenate
both earlier String and current String"
How to get this result
'To: [email protected]
From: [email protected]'
Upvotes: 1
Views: 148
Reputation: 7468
I'm not really sure I got exactly what you are trying to do. Anyway, let's suppose that you want to send a mail specifying the sender, the subject, and so on. There are different ways you can do this, but in every case you have to keep the state between different calls of your method, and this is done by defining an instance variable (or more variables, depending on the way you choose to follow). If you want to keep the signature of your method, i.e. to keep using a single method to add different parts of the header, you could use a single var containing a Dictionary.
This can be done defining a Header class containing a single instance variable that will contain a Dictionary, for instance headerDictionary
. This var has to be initialized in a class method new
as follows:
new
headerDictionary := Dictionary new.
At this point your addHeader:email:
method can simply put values in the dictionary as follows, without if's or external methods (BTW I wouldn't call it addHeader:email:
, since for instance the value for Subject is not an email, but these are just bells and whistles):
addHeader: headerName email: aString
headerDictionary at: headerName put: aString.
In this way in your workspace you can execute the following lines and end up with the dictionary contained in hdr containing the values you want:
hdr := Header new.
hdr addHeader:'To' email:'[email protected]'.
hdr addHeader:'From' email:'[email protected]'.
Upvotes: 4