Hello, in this video we are going to cover sending email attachments. So an email attachment can be anything like a PDF or an image. For example, I've got a PDF file next to my index file, right this dummy dot PDF, you literally just have some dummy text in this like one line of code, no matter what the contents are, you know, This tutorial will work exactly the same. So let's get started. So first of all, what we need to do is create a two variable and this is the email that I send in the message to so sonar systems, which I have open right here, and dollar from equals wherever it's coming from an office and make sure the domain is the same as the domain You know, have the website that the flower is running off. And we'll have a swarm name as well.
So it just makes it look a little better and we'll say some ink. And then what we're gonna do is have an email subject so So Jeff, and I going to pull up a bunch of new lines of easily scroll subject equals email attachment. Mail option. And now what we need to do is specify the file. So dollar file equals and for this, you just specify just the name but the location as long as it's in the same location. I just need to specify the name but if it was in a folder called no attachment, you do something different and it could be an online file.
Well, so Okay, there we go. No PDF NetFlow we need to do is actually do the, you know, the body of the website. So body equals and you know you can make it as complex as you want but I'm literally going to quick for a quick h1 tag in there and say I mean while at the floor from it, and now we're going to do is we need to create some headers. So this is where a lot of the code can be so he calls and say form and dollar from name. Then we're going to say dot and we're going to put angle bracket and Morgan Stanley is dot dawn All right. There we have space, dollar dot from dollar dollar and the hack.
Okay, so that'd be the name and then email address. Next all we need to do is create a mime boundary. This is just for you know separation of the tags and we'll just use empty phi function for this on the call it semi Rand equals MD five. Asked me based on time again, you can use something a bit more complex than this before we need this will be more than enough. Let me do a line boundary is going to be equal to multi part on the school boundary underscore x And then he put curly braces and we just put semi round. Excellent.
So, now what we need to do is add the headers for the attachment itself for dollar headers, dot equals for this backslash n mine dash version 1.0 backslash n dot con and toy with multi port backslash mixed semi colon backslash n and we need to just add the boundary. So boundary Nucor backslash and then we just put the curly braces and we'll just put dollar my Embry like so I'm missing something I am missing something so backslash. Yep. quotation quotation yep so that's indeed the flows or the quotation marks good which is what we need. We got the headers for the attachment we need to do a multi part boundary No. And also setting you know the character set for example.
Message equals dash dash, dollar mind boundary backslash n dot content type text slash HTML Shall sex equal, backslash UTF eight backslash quotation mark, backslash and quotation mark. There's quite a bit here. But once you've got this all set up, you can put this into a function if you want recommend doing now the next tab pointing to a class and learning more about it, but over the next few videos, we'll go over the PHP mailer anyway to the more advanced library external library, which is much better than using the built in function bonders show you the built in one as well. Okay, so let's say content transfer encoded seven bit. backslash n backslash n dot what's the variable cause hold on Finally, set a backslash n backslash n. quickly check if the file exists and this is also a good check to do a bit more optional but definitely recommended file if the greater than zero then we're going to check you know if the file that we've attached is a foil or the built in method for that is underscore foil.
Dollar file and in here, we are going to you know, start composing the message message dot equals That's the dollar mind boundary, backslash n dollar FP equals F open default opening the file for read permissions, and data equals F read. Then we're going to use the file I'm going to specify get, specify the file size, which we just easily do but this one the actual file itself and you'll always dynamically get the file sizes I have to constantly be checking that and it's not practical for you to be checking that it's online website. Then we just do f close the file. Now we need to do is All data equals shown in this course, base 64 in code validator and we just need to almost there now we've got close close here, message dot equals C to add some more, you know, content types onto here.
Okay, yeah. Content Type content type. Application for octet stream, semi Hold on. name equals backslash quotation mark. I see the back that she was escaping this. So it doesn't actually count.
Select and kill this tag as the purpose of if you were wondering what's going to put up they the dollar foil.so backslash backslash quotation mark, quotation mark backslash and quotation mark the abdus looking over to me, and yes either from simply close so far correctly. Content rich colons base dot base name dot a file dot backslash And we're going to number one again I know we've done quite a bit well we are almost there content dash disco this edition of Excel will provide all the source codes that you can literally just copy and paste their recommend still going through line by line, you know just over the code and figuring out why does but overall you can just copy and paste the file name equals backslash quotation mark, quotation mark, dub, base name dollar file dot backslash In the quotation mark here docx, file size Dola, file, dot, semi colon backslash and literally the last part of this append.
And then we'll say content trends include in base 64, Fs and backslash dollar data dot quotation mark. So backslash n backslash. Think we literally don't have your dot inside of here now, like we've got a couple more lines, but these are really small lines now. dot equals dash dash, curly brace, mine boundary, dash dash, dollar path equals dash F dot dollar and now we just send the email so dollar equals mail, dollar subject. Message headers don't turn past and now we just need to put the email states that CO is going to use a you know, inline if statement on what's called a conditional semi car. otherwise called, or we didn't email, question mark.
And if it's true, then we're going to say mail sent. And then if it's false, we'll say mail. And there we go. So we shall be able to run this. So Paige is not working unable to handle the requests on my shoulder. The internet is actually working and it's not some other issue.
The Internet seems to be working a joke. So okay, so that mean there must be some problem here. Let me just delete this. Okay, so that seems to be a Okay. What I'm going to do is delete this All of it just from here to the pot. There we go.
Okay, so something seemed like something inside of here is breaking it for some reason. An email sent but we're going to need the headers anyway so shouldn't work realistically. So in here there's something wrong. Honestly, it's probably something to do with this. No actually probably not. That should be not Fred there should be f read uncomfortable.
So mildly sand and Okay, so as the one minutes ago, okay, so that's the one that, you know, didn't have all the code in there. Hence why there has messed up. But this one has successfully sent, and it's got the PDF data all there was to it. So yeah, that's how you send an attachment. One thing that I wanted to mention is the first time I tested this, it took about 15 minutes for the attachment to come through. So just be patient, and you don't get any errors.
And you know, you can use the code from the GitHub page, be patient you know, also wait up to an hour to do the first time that that mouse I was connecting to, you know, particular mouse over that you might get a bit of a delay. Apart from that you should be all good to go for email attachments using the mail function. If you have any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next video.