Hey everyone and welcome back. Now in the previous nugget we had just installed Postfix
for our basic email server configuration. Now understand this, with respect to Postfix
we can actually read local mail as we just saw. But quite honestly the way Postfix handles this
is a little bit clunky, a little bit cumbersome, we can definitely do better and that is what we
hope to do within this skill right here or rather within this nugget right here should I say.
Okay so like I say Postfix is going to hold local mail. Now the way Postfix is actually going to
store this information, it's going to store it in this rather cluttered way. What you might see
is it is stored in var, bool and then mail and then the username will have a particular little
file here. So in my case it would be ipv0 and for the record sometimes it may actually miss out,
spool it may just be var, mail, ipv0 but the point is here is that this is a system wide
configuration meaning that we have all the files pretty much in the same location within the var
directory, one file per user and when that user gets a new email those files happen to be appended
to that same file so check this out. If I happen to do a little example here, in fact you know what
I'll do I'll go into my aliases so can you remember the location it's the aliases let's go in here
and I will remove the root ipv0 binding and I will write this out and I will say sudo new aliases
to reload the configuration what I will now do is say mail and I'll just say test one and this can
go to the root account at localhost and the message will just be this is test one and I'll say control
shift d and then I will say mail test two and again at localhost and I'll just say this is test two
control shift d okay so two different emails have been sent to the root account if I say sudo su
who am I I am the root account if I say mail you can actually see where we're reading from in this
case it's var mail and then the name of the user which is root and all the messages are all within
this file if I happen to hit enter we can see the first message here and if I hit enter again we can
see further down within the same file appended to it we have the second email so notice what is going
on here we go into this directory here and we have one big file that just keeps appending
storing all of the user's emails together so you can imagine if we have lots and lots of different
users this is not the most manageable way to handle this now what we do have if I just press q and hit
enter clear the screen so if I just say mail s and just say hello ipv0 one and I'll send this to ipv0
at local host I'll just say this is first message to ipv0 and control shift d and do similar thing
hello ipv0 to at ipv0 local host again this is second to ipv0 so control shift d if I now say mail
check the location right here now we can see we are in var mail this time it's ipv0 as opposed to
roots but notice again all the messages are just being concatenated within this file right here if I
hit enter we can see the first message if I hit enter again we can see the second message and if I
sent a third message to ipv0 this file would just be getting larger and larger and larger so we can
see here this is not optimal let's try to solve this issue then so what I will do here I'll just
press q to quit I'll clear the screen and the solution we're going to be looking at is one called
proc mail now proc mail does things a little bit differently what this is going to do it's going
to manage things by putting our mail within user specific locations so really you should be thinking
about something like you know a home directory pwd somewhere like here so as opposed to var
spool mail and the username or var mail username we can just have configurations neatly held together
within home directories for example now there are two different ways in which proc mail can
actually operate and the first way which I will explain is honestly in my opinion not that much
better than the original postfix manner of storing emails but the second way is a lot better a lot
cleaner and it happens to be by far the most popular and most used method of storing emails
locally so let's talk about the two different ways we can store information within this home
directory by using proc mail so the very first way is the mbox method now this is similar to
postfix in this regard because what it's going to do is that whilst we will have things stored in a
user defined directory such as home ipv0 and then something like say inbox and then something like
say mail and then a file called inbox for example the difference here though is that as opposed to
having the system-wide location of you know var mail ipv0 whatever it may be this time as we see the
location is specific to ipv0 this is my home directory after all I have a mail directory but
this file right here is indeed what I just said it is a file it is not a directory so you can imagine
what this is going to do it's going to have a similar effect to what we just saw with postfix
in that if I send a message to ipv0 it's going to be stored within this file right here and if I send
a second message to ipv0 it's going to be appended to this inbox file again and the third message
the same the fourth message the same so as you can see here this is exactly what the problem is the
continual appending of all these emails if you happen to have 500 emails in your account you're
going to have one file with 500 emails all tacked on this is not very manageable at all so inbox
whilst it is maybe a little bit better because we have this separation of user specific locations as
opposed to the system-wide storing of the emails this is still super clunky we can definitely do
better and the good news is is that we do have that other solution and this is called the mail
dir method and the way the mail dir method works is it's very similar to inbox in that we're going
to still be utilizing that home directory so say for example this would be home ipv0 for me so we're
still no longer using varspool mail or var mail but inside of here this time as opposed to having
an inbox file we can actually have a directory and within that directory we can have individual
files for the emails so the emails are no longer going to be appended and appended and appended
they can be neatly sorted individually and accessed individually so this is much more modern
much better organized and much less headaches when trying to sift and filter through your emails so
now that we know what the basics about proc mail actually does let's have a look at the general
configuration and get it up and running and integrated with postfix to check this out then
let's have a look so what i'm going to see here is sudo apt install and i will say proc mail and this
is going to run through the installation now that is all we have to do for the installation of proc
mail proc mail is not going to have any type of specific configuration file instead we are going
to go to the postfix configuration file and tell it to use proc mail now the pop quiz is what is the
main configuration file for postfix if you're answering etsy postfix main.cf then you're absolutely
correct let's go to that file right now so we'll say sudo nano etsy postfix and main.cf now check
this out what i'm going to do here is i'm going to go down to the very bottom of this particular
file and what i could do here is i could manually say mailbox underscore command equals proc mail
and this would ultimately instruct postfix to actually use the proc mail service now once i
make this modification i would have to save the file and then reload the service but one way we
can also do this and this is a command we want to know for the actual examination and that is the
postconf command now the postconf command is going to allow us to modify the configuration
file for postfix but it's going to allow us to do so without having to reload the service itself
so the command i will do here in fact just to show you if i go into the configuration file notice if
i go down to the very bottom i haven't actually made any changes yet let's go back if i issue this
command saying sudo postconf and dash e to edit the file and i just specify the changes i want to
make so i'm going to say mailbox underscore command and that is equal to proc mail so if i hit enter
now and i go back into my configuration file and hit enter if i scroll on down to the very bottom
notice here that we actually have added this configuration file directly using the postconf
command without having to reload that server so what i will do is i will escape and like i say
postfix has now been installed or rather configured to use the proc mail service so now what i'm going
to do is i'm going to first go through some global settings because proc mail can be configured to
act globally across all users on the system so we'll first look at that and then we'll look at
user specific local settings how we can have proc mail just target particular users with
particular settings so the very first thing that we want to do right here is when we are creating
a global configuration for proc mail what we want to do is we want to create a file within the
etsy directory and let me just spell etsy correctly there we go and we want to create a proc mail rc
now this file will not exist just yet but we are going to create it so let's just do that right now
so i will say sudo nano i'll go into the etsy directory and i will say proc mail rc now here are
some settings i'm going to have to do i'm going to say mail dir is equal to i'm going to say dollar
sign home which is my home directory this is a variable so in my case this will be forward slash
home forward slash ipv0 for you at home this will be forward slash home forward slash whatever your
username happens to be and then i'll say forward slash mail now one thing to note here is that this
mail dir this has nothing to do with mbox or the mail dir settings remember we had two methods one
and two number two is the one which is the much better one this actual keyword here has nothing to
do with any of this all we're specifying here is the actual mail directory where we can store stuff
really quite simple next thing we want to specify is the default location that we want to store our
mail in case no actual mailbox is entered i will say default and the default location again once
again we go into our home directory and then i will go into mail and then i will have a file called
mailbox now one thing i want to note here is that this particular configuration here this
is what has specified that i'm using the first method of mbox the reason why is simple because
what i've done here is i've actually specified a file however if i happen to end this line with a
forward slash inbox would now be a directory and this would mean we're using the second method
as mail dir but for now we will just start off with the mbox method of configuration and what i
will do is i will save this file then so what i will do here is i will do an ls i have this
inbox right here i'm just going to remove this for now i've got a clean slate now like i say if we
just quickly peek at our file once again we need to have this mail directory proc mail after all is
going to look for this directory if i happen to do an ls we do not have such a directory so what
i'm going to say is mkdir mail so if i go into mail and i do a pwd this is exactly the path that we
have specified within our proc mail rc notice that this part here is ultimately substituted by the
dollar sign home value which we see right here okay so let's go back so what i will now do is i will
actually send a mail to ipv0 and i'll say cc don't care about who i'm going to add the subject will
be random test thingy and the message will be hello ipv0 once again now do control shifty now
while i'm in my directory if i do an ls we have this file called inbox and if i do an ls-l we can
see it's not a directory let's cat this file so we'll say cat inbox and lo and behold here is my
message here check this out if i go back out of the directory here we are let me just do the
similar thing once again i'll mail ipv0 once more i'll say the subject is another message i'll just
say blah blah blah whatever it may be control shifty kill so do an ls let's go into our mail
directory again we only have one singular file we can see here and we cat it and we can see the
first message right here and then the second one appended right to it so this is really quite similar
to the post fix method again the big difference here is the location where we're getting this
information it is in a user specific location as opposed to the system way configuration of var
mail and then whatever the username happened to be now one thing if i just happen to note here
is let me just go back under clear the screen do a cd now if i just happen to click or rather type
the word mail we see no mail for ipv0 what i want to do here is to invoke this command i have to tell
the system where to look for this mail file but the way i can do this is by saying mail dash f and
then specifying the path to my inbox now so i will say home ipv0 forward slash mail and then point it
to this inbox file so if i hit enter now now the mail command can access those messages just like
we could in the original post fix manner so i could hit enter and we can see the first message and hit
enter again and see the second message no problem at all so i'll press q to quit and we can see here
the location this time is no longer var mail ipv0 we're using once again this home directory
now let's try this using the mail dir method as opposed to the inbox method so what i'm going to
do here is if i go back into my directory it's going to mail and i'll just remove this inbox
text file okay so it's no longer present and let's go back cd and i will go into my xc directory
once again and proc mail rc what i'm now going to do is i'm just going to change this from
inbox to mail dir by adding this slash so if i happen to save this and exit out what i'm now
going to do is i'm going to mail ipv0 once again and i'll just say subject hey there i'll just say
hey there one and control shift d and let's mail ipv0 once more and i'll just say hey there two and
again hey two and exit out so now what should happen is if i do an ls and i go into the mail
directory i do an ls now notice we actually have what we can see a inbox directory here okay so we
go into the directory here and we're going to have this new structure we're going to have a folder for
current a folder for new and a folder for temporary so what i can do here is go into new and do an ls
notice we actually have two distinct files i'll do an ls dash l actually so what i can do here is i
can cat the first one and hit enter here is the very first message and completely separate i will
cat this one and we have a completely separate text file holding an individual email so in my
opinion this is a much much better way to handle things but for now what i will do is i will go back
into propmail.rc and i will just change it back to the inbox manner for now okay so let me just
save this so we're back to inbox now the file we just manipulated there was in the exit directory
and it was propmail.rc now again the big hint here is that we are in the exit directory this should
be telling you that we are using system wide configuration now what we're going to do is
we're going to create some local settings and this is going to be a little bit different then so let
me show you what i'm now going to do is i'm going to make sure i'm in my local directory my home
directory so i do a cd pwd here i am and i'm going to create another file but it's going to be called
dot propmail.rc and again this is going to be in my home directory now within here we're going to
create something called a recipe now the recipe ultimately acts as a type of filter it's going
to allow us to invoke particular control over our messages and the way we're actually going to control
our messages is by invoking some of the pattern matching used via the egrep command this is
effectively just using regular expressions if you happen to be well versed in regex this should be
not so daunting but for others who are not used to regex regex can be pretty intimidating okay so
let me walk you through the basic syntax right here at the start of this file to denote it i'm going to
say colon zero colon this is basically telling propmail that this is the start of a new rule now
i'm going to do some basic regex here i'm going to say star and then an up arrow and then two and
then i'm going to specify who this is for so in this case here i'm just going to say two roots and
then the action i'm going to take is i'm going to create a root file now if you're standing
scratching your head that is absolutely natural the reality is this can be a bit confusing so all
that's happening here is that this part right here this part is a regular expression so these
characters all have particular meanings if we happen to see a star this basically means to match
zero or more previous characters whereas if you happen to see a plus sign this means to match
one or more previous characters if you just happen to see a dot or a period this means match any
single character except a new line character if we happen to use square brackets we can specify
a range of particular characters if i happen to do the up arrow or the carrot symbol this means it
must match the beginning of a new line so this would be the beginning if i happen to see the dollar
sign this would be to match the ends of a line and if i have the backslash n this is going to
match a new line character this is basically your return key when you're having a new line now there
are much much more rules with respect to reject and the good news is is that you absolutely do not
have to have all of this memorized the reality is people who use rejects tend to use on line help
but ultimately that is all we're doing we're trying to match a particular thing so basically if you
happen to see an email that had the subject and let's maybe say the subject began with the word
alert you could specify a particular rejects expression to catch this particular pattern
or maybe the email was mailed to a particular user say for example like i say the root user
you could specify a particular rejects pattern to catch that and that's all i'm doing right here
all this rejects pattern does is it means any message that is being sent to the root user
i am taking a particular action and the action i'm taking is i'm actually going to create a separate
root file which is going to house the emails that have been sent towards the root user or
alternatively what i could do is i can specify if i happen to see in the subject line and the subject
is priority what i want to do is i want to create a priority file and dump the message in there so
if i save this and exit out what i will do is i will remove my mail directory and i will recreate it
so if i go to super user and i send a mail to ipv0 and i put the cc is nothing and the subject
can just be boring stuff i can just say this as a boring message and control shift d if i substitute
user into ipv0 i do an ls i go into mail do an ls i should have in my inbox i cat this file we can
see here we have a boring message from the root user but again if we go back and i go back to root
again who am i and i say mail ipv0 and this time i put in the subject its priority and i just say
this is really important check it straight away and then control shift d and then i substitute
user back to ipv0 do an ls go into mail ls now notice i actually have a separate file within my
mail known as priority file and if i happen to cat my priority file we can see here this is being
separated and it sticks out like a sore thumb so once again one last time back to root user mail
ipv0 subject is just hey there how you doing nothing important and we send one more again we want
them to really pay attention so we put priority in it just say remember the deadline and control
shift d go back to ipv0 do an ls go into mail ls in the inbox file appended to that we see at the
very bottom appended here is how you're doing which has been misspelled by the way and ls if i go
into the priority file appended to the priority file is remember the deadline and that is because
the regular expression happened to search for the subject matter with priority and because we saw that
we were able to filter and our local dot proc mail rc file within our home directory matched on that
filter and dumped that into the priority file as specified and again there are almost limitless
rules you can do to invoke to control and filter this type of behavior so really that is how we
can actually control our local mail like i say we can use postfix natively but it really isn't as
well equipped or advanced as proc mail to handle and control all of these types of settings these
settings by the way which will make your life much much easier okay docks i hope this has been
informative for you and i'd like to thank you for viewing