 Hey everybody, this is Brian. Welcome to the 29th lamp tutorial. Today we're going to be discussing how to send email from your web page. Okay, first thing you need to know is the mail function. Mail function is actually very, very simple. Mail to subject message headers parameters. Two is obviously who it's to. Subject is the subject line. The message is the message body. The line should not exceed 70 characters, but that's kind of not really true. I mean, it can work, but it shouldn't work, etc. I've had no problems with it in the past. Headers, if you don't really understand what a header is, you should go out and read the RFC about SMTP. But this is like the message from, you know, blind carbon copies and things of that nature. And then optional parameters if you really want them. And here's an example of how to send an email. Just very simply setting up our variables to subject message from headers. And then you're calling the mail function and then echoing out mail sent. And here's a little bit more of a more complex example using a form. But before you get any of this to work, you need a mail server. That's right. You have to actually be able to send email. Well, PHP has this thing called an INI file or a config file, essentially. And we need to go out there and find that and see what it's set up for. Where is it? Etsy, PHP 5, somewhere, somewhere. There we go. Patchy 2. And there's our PHP INI. Now, you should know, you can go on a client and there's another one. So there's two of them. Hmm. Why is there two? Well, it's because the PHP INI file is designed in a way you can actually override at different levels. So if you have multiple PHP websites on the same server, each one can have its own INI file. For the course of this tutorial, all you really need to know, and ironically open right to it, is the SMTP. Notice how it says local host SMTP port 25. Well, 25 is a standard send mail transfer protocol port. If you don't know what SMTP is, that's actually how you send email SMTP. The easiest way to tell if you have this installed, open a terminal. And you can type in a lot of commands and stuff to really find out. But the easiest way is just to tell it into yourself on port 25. Notice how it says unable connect remote host connection refuse. That tells me I'm not running a mail server on that port. So we need to install an SMTP server. And I'm doing this tutorial from the perspective that you'll be setting up your own lamp environment. So I'm just going to install what's called postfix. We're going to go sudo apt get install postfix. All right. It'll configure some things and it'll say, please select the mail server configuration that best suits your needs. Now, a little bit of explaining here. No configuration means it doesn't have anything. Internet site means this is an internet facing web server. Internet with smart host kind of goes beyond the scope of this. Satellite system, etc. Local only is if you're only going to send emails to yourself, I believe. But don't quote me on that. You want internet site. Now it's saying the mail name is the domain name used to qualify all mail addresses without a domain name. This includes mail to and from. Do not make your machine send out mail from root at example.org unless root at example.org has told you to. Had to give that little disclaimer. A fully quantified domain name is how we find things out on the internet. For example, .org. Pretty good, huh? So if you have a domain name just enter it. I'm just going to leave this the default because the tutorial. It'll then install postfix. And I'm using postfix because it's a really simple, no fuss, no moss. You just install it and it runs. Alright, updating database of man pages. Now if you're on a production environment you're probably going to want something a little more beefy like send mail. Although postfix should work for most of your just general needs. But when in doubt consult with your network administrator. If you're setting up in a production environment and you shouldn't be, you kind of know that you shouldn't be doing it. So go grab somebody who knows and then let them set it up. We're just going to wait for postfix to finish installing. And while it's doing that we can actually grab this example code and load up our trusty IDE here. And I just want to go over this real quickly. All we've got are some variables here. Two, subject, message, from, and headers. And we're calling the mail function. And then we're calling the two, the subject, the message, and the headers. Now notice the headers is from. So all we're doing is we're saying, who is this from? Well from is someone else at example.com. Two, who do we want to send this to? Oh man. See the problem with this is if I put an email out here, whoever gets that's going to get like 9 million messages. So I'm going to use my own email. So I don't get sued. Voidrealms.com, by the way, Voidrealms.com is my personal website. And you can go out there and get the example code for all of these tutorials. So you have the two, who you're sending the message to, the subject. This is a test mail, the message. And you can actually get into some pretty complex stuff with mail. We're going to keep this one really simple just because we're showing the basics. But you can actually say this is an HTML encoded email and send HTML emails. All right. Now that PostFix is installed, we can go back out and let's just tell that back in the 25. Notice how we are now telling it again. So if we say, hello, me at home. Notice how it responds with a 250 test virtual box. 250 means okay, next. I'm not going to really get too deep into the RFQ for how to send mail. But PostFix is a very simple mail server. All right. Now when we run our handy little PHP script, let's actually save this. Open this up in our browser. Actually, let me cancel this out real quick. I want to change their code a little bit. It's a little misleading. Mail actually returns a boolean value. So we can actually say if the mail was sent else, you know, danger or something happened. Oops. Seems like I typed a little faster than the auto type. So we're just going to say if mail because it's going to return a boolean. So if it successfully sends a mail, we're going to say mail sent. Otherwise mail bad kind of like, you know, Frankenstein with fire bad. So we're going to save this. Now we're going to actually, I know I'm going to regret putting my email in this tutorial because I'm going to get like a million test messages saying test mail. So when this loads up to the mail set, so probably in about 15 or 20 seconds, my cell phone is going to light up saying, you have mail. So if you're watching this tutorial, I beg you, please do not use my email address for testing. I will simply change my mailing address and a million people will show up at your door and beat you with rubber chickens. Use your own email address for testing or use a test mailbox. Yes, you may have noticed I use someone at example.com. If you're coming from a security world like I am, this is very easy way to spoof an email. Simply put, someone else at example.com just doesn't exist. I'm sure there's an example.com, but I'm pretty sure that email address does not exist. So you could say Santa at North Pole. I actually did this to a boss of mine once. Wow, that's embarrassing when you misspell North Pole. I did this to a boss of mine once. I sent him an email from Santa saying he'd been a naughty boy that year. He failed to see the humor in that. So to this day, I don't think he knows that was me. So let's keep that our secret. Anyways, this is Brian. Thank you for watching. I hope you found this educational and entertaining.