How to use PHP mailer library to send bulk emails.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone today I'm going to show you how to send mass emails in PHP using the PHP model I will in this tutorial we're not going to be sending an email to an email address from a text box but instead we are going to be send an email to email addresses in a database so for example you have a bunch of emails I need to send an email to each individual email a different promo code so other things you need before you can proceed is you need warm Sava or any form of PHP hosting you need an email account you need a database in which we're going to store the email addresses and the different promo codes in and you need to download the PHP mailer library you can use any free email or email account for this and I'll drop a link to where you can download wampserver which also comes with a database and also a link where you can download the HTML aligned before so the first thing we need to do is download the library okay ah to download it just click on the download zip button which download the zip file and I'm just going to subtract the zip folder okay after extracting the zip folder not all the files I needed for this tutorial I'll recommend you read through the documentation for PHP Miller so you can have an idea of what to do or else you can achieve with this library the only far is we need a one two and three so I'm just going to copy this files out and paste them here okay the next thing we need to do is create a PHP file just going to save this in the same folder as long as the instructor files save us I'm going to name it just going to name it index dot PHP it's a very simple I be very very easy to use the next thing we're going to do is if you scroll down to the page there is already an example there sample there and we're just going to copy that example modify it okay we need to close that PHP on the top there if your attends you should note you need to include this file which is part of the library which is download essentially pitching will auto load and we have that file there as well if you put this follows in a different folder you need to go here and change the directory to this file ok so now we just need to configure this and the first thing we need to do is declare two variables the first variable is going to be for HTML the next variable is going to be for text so what the hitch demo variable contain is the HTML version of the email and what the text variable will contain is the text version of the email for those of you you don't know the difference between HTML and text emails are HTML at the beautiful emails you get every now and then text version it's just the text version of that email it's quite a good idea to send multicast emails just in case you send any email to a device that doesn't support HTML email instead of just displaying a blank email or a bunch of HTML code the device will display the text version of the email it's just called as HTML version and text version Tech's motion we're just going to write this as a text this is the text oh yeah this is the text of Russia I'm in the HTML we are actually writing reading HTML code here just write one paragraph and we're going to paint it so we're going to paint this red so you can see the difference between an HTML email and a text email okay the next thing we need to do is configure the email settings the first thing you need is the host for the email address you using this will vary depending on the email provider using but this is what we use on for this tutorial the username is the email address that wouldn't be necessary for the email provider I'm using the part is two and five you the set address also has to be done the from address also has to be the email address of the account you using and you can always change the name to you for example this is not as you said promo and let's just change the name to test email there are two different ways you can configure the other dress but we are not going to set the recipient name we're just going to set the recipient email address so we're using the same email ask the to address so the email will be sending that deliver to this email address you can also set the replied to address you can copy people in email as well but we're not going to believe all of this you can also send attachments so let's say for example the coupon we send in it could be maybe a digital coupon but for now let's just comment down because I'm not sure we're going to be needing that for now is hit demo true the subject of the email test email subject why not for the body is going to be a variable on another variable the olds body is ready text version goes and the body is ready hit team of Russian girls all I have to do is save this now so if we run this example anemia will get delivered to this email address this is just an example to send to one email address to be sure it is working properly before we configure the database so to test this to test this email all we have to do is just launch the index file we just created and that's the link to it then I'm just going to press ENTER and wait for it to load if the message to send successfully get message saying this message has been sent and if it fails you should get message saying I mean message could not be sent and it gives you an error wrong but now we're just going to go to the email account there you go the email has been received and if you check that is the subject the from name and if we look is painted red so he general washing work they check the text fashion one minute II do varies on the email provider using but this is how it's done here and if we click on text this is the text version there you go there you can be the head as well to check and this confirms that it was a multi-part email okay now that we show the email is working the next stimulative is configure the database to configure the database to make the code very easy and need the first ability is create a new function we're just going to name this function send email and the function is going to have a few input parameters the first input parameter is going to be the email address next input parameter is going to be name and the total input parameter is going to be promo code so now we need to rewrite the email instead of hard-coding the other dress we're just going to use the variable there and now we need to rewrite the email so let's say for example this is your promo code I will just write HTML again so we can easily differentiate the HTML and the text version little concatenate ah on promo code we also need names so we're just going to write hi space name goes in here a line color since we even hit table we can have hit CMO tags like break we're just going to add two breaks here so it's not mashed up on one line the same thing for the text version without HTML tags so for brake line and text version this is how you create a brake line and text version the color this is your promo code text version and the promo code goes in here let's just make it neat and tidy so if you notice we using a variable that's going to be passed into the function as the email address and the next variable name is going to be used in the content of the email so high name and in the content of the email we have promote the content in your unit is named promo code an email on our hard coded so this data are going to be pulled in from a database the next thing we need to do is create a table in your database so I'm just going to go over to PHP myadmin already have a database here now I'm just going to add a new table so let's call this table email we need an ID column we need a name email on promo code so we need four columns correct the first one is ID which is going to be the primary key of the table and we need to auto increment okay the next one is going to be name it's going to be a voucher let's just use 17 years in case some of your customers are very long name email address 70 I made promo code promo code might be 70 so then is the bit they need to be very cars click and save the next thing we need to do is populate this tables I'm just going to write the queries to populate tables here and we'll wooden mini Linda the name is going to be user 1 on his email address we're using the same email address the test for all the users because I don't have team treated for any email addresses you can use for this tutorial so we just using one even address and the promo code can be a same way once next customer to and his name is used to we test them with three different accounts to keep your shotgun simple I'm user 3 click on go you see will have a populated database the next step is to go back to our PHP file and write a select statement to retrieve data from that date first we need I'm not using a wampserver database for this immediate a live database so depending on your database provider your settings might be different so now this is all we need to get into our database the next thing we need to do is write a statement so this is a query I've prepared from area Ron I'm just going to paste it here if you notice here the host name is the variable here the database name is the variable here the user name is the variable here on the password that's the password the next area to do is exception we're not really using exceptions yet because I haven't included any try encourage but we'll just leave you out further we'll just leave it there for now then the next time you need to do is prepare the statement and if you look when selecting the ID IV name name email email promo code promo code from table email table email there you go and next area to do is we need to execute the statement and then we use a while loop to look through the individual rules of the database the very collaborative we're going to do next is after looping through a specific role in the database be able to call the function send in email let's send email there all we have to do is copy this code here and paste it here I guess we don't really need the ID for this email file we don't really need the idea so let's just comment it out for now so name will go into name email will go into name promo code we're going to promo code so for every single row in the database this function will be called and your function is the email so Row 1 it will call the function send the email to Row 1 do the same thing for o 2 Row 3 Row 4 or 5 until the very end of the database ok so to be sure to email gets sent to every single user we're just going to come here and change message has been sent to our user name let's just print the name here email same thing is happening here we're going to print the email they're just copied out there so now if we run this code we should get three emails to the email address and the reason I get in three moves to the same email is because using the same email for each I mean for each of the recipients but the emails will have different names and different promo codes so now our code is complete and all we have to do is run the index file okay I will get some errors here or your promo code I think I spelt it wrong but if we look we get the different emails here what that one is too easily but it's something wrong with the promo code so we're just going to go back to the code and check where air is coming from okay if you are the promo code that's felt differently so we're just going to copy that promo code here and paste it here good and we're just going to delete this on try again so we're just going to try it now oh I'm just going to add some break spaces in this so we can try this again because it's just mushed up so if we come here so we're just going to trigger if you look our message has been sent to user 1 user 2 and user 3 and if if we go to the email account you have three different emails to three to four recipients and there you go that's the first one they use it 3 promo code 3 user - from a code - and user 1 promo code 1 we could also verify the text version and there you go and there you go that's how to send emails in PHP in 78 lines I'll post a I'll put a copy of the source code on my website so you can download the news if you want to thank you very much for watching and let me know if you have any questions thank you
Info
Channel: Tayyib Oladoja Youtube
Views: 19,038
Rating: undefined out of 5
Keywords: Emails, PHP, PHP mailer, sending emails, how to send email
Id: Tce19C77ucc
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Mon Mar 21 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.