How to Create a Send Email Form Using PHP & MySQL



PHP



In this article, we show how to create a send email form using PHP & MySQL.

We are going to create a Send Email Form in this article to send emails to all contacts that we have on an email list table in a MySQL database.

Before doing this article, check out how to create an HTML form that places user information into a database.

When we write the subject and body of the email and then click the submit button, it will be made so that the email goes to all of the contact emails contained within a MySQL table.

The form below is nonfunctional, so nothing will happen. It simply shows you how the frontend interactive is when you write the code.









The HTML Code to create the above forms is below:

HTML Code


The HTML form transfer to the PHP file, sendemail.php, and then this file executes the PHP script that is located in it.

So after creating this, we now must create the PHP file, sendemail.php.

PHP Code




The following PHP code is broken up into blocks so that we can go over each block and see what each one does.

Block 1 is where you enter in your user name, password, host name, database name, and table name to connect to the table of the MySQL server that you want to connect to.

Block 2 is the email address from which you want to send out the email to all the contacts that you have on your email list.

Block 3 is the PHP code used to retrieve the subject title and the body of the email content that you enter in and want to send to all members of your email list.

Block 4 is the code used to connect to the database of your MySQL server.

Block 5 is the code to select all emails that are on the email list and the code that actually executes the connection to the database.

Block 6 is the code that fetches each row in your email list table of your database to extract all the information from each and every user. In this table, we store 3 sets of information, the user's first name, last name, and email. If we just used $row= mysqli_fetch_array($result));, this would only retrieve one row of a user's information and we would have to keep repeating this line over and over. Besides being terrible coding, it's also inefficient because we wouldn't know how many rows there are in the table unless we checked. A while loop is is perfect because while the fields are not empty or equal to zero, it will keep retrieving row after row until there are no more rows left to achieve. We run the while and we retrieve each of the three sets of information, the first name, last name, and email of the user.

Block 7 writes the salutation of the email and mails off the email using the mail function. We're able to address the user by his or her first and last name, since we have all of this information on the database. We then use an echo statement to confirm that the email was, in fact, sent to the user.

Block 8 closes the connection to the database.

Related Resources

How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP




HTML Comment Box is loading comments...