How to Send greetings cards via email using PhP and Mysql

This is going to be my last blog for 2013 so I thought to post a tutorial that will be trending at least one week from now and that is sending greeting to your loved ones. Be it New Year, Christmas, Eid or Diwali, we always send greetings to our loved ones.In this tutorial you will learn to send greetings cards via email using php and mysql. Since you will be sending mails you have to run this script on a live server or setup your email account via smtp/pop3 in the phpmailer script to run in localhost.

See Demo Image:

Scope of the script?

Before getting started get a detail explanation of the script. You can use this script to send greeting card to your customers, subscribers or any other person that has their email ID in your database.

  • User has an admin interface to manage greeting cards( Add/edit/delete/ Listing) .
  • User has an admin interface to manage subscribers ( Add/edit/delete/activate/deactive/ Listing) .
  • On the homepage user can select between active/inactive/custom subscribers to send email.
  • User can select only one greeting card per email.
  • User can send emails to single/multiple emails IDs.
  • User can attach a custom message and subject line with the greeting card.

Installing the script to your localhost/server.

I always try to make the script as user-friendly as possible so you have to make least possible change when installing/ migrating the script to some other place. In order to run this script you have to do the following:

    • Download the zip file and extract in a folder.
    • Create a database with whatever name you like. for example I use greeting-cards.
    • Now run the database.sql script in PhpMyAdmin under that database. It will add two tables and some demo data.
    • Change your database username, password, and database name in the constants.php which is located inside libs folder.
    • If you are running your script in a live server, go to index.php just replace $mail->SetFrom(‘’, ‘Shahrukh Khan’); with your name and email ID.
    • If you are running this script in your local server see testmail_2.php. I have used Gmail ID with SMTPauthentication to send mail from localhost.

Sending emails with phpmailer with default php mail() function

I am using php mailer library to send mails. It’s a very useful library for sending mails. To send html email to single/ multiple user with php mailer just follow these steps:

Include the phpmailer class file to your page .

Instantiate the phpmailer class.

Add your email address (sender) and the the person you want to send ( reciever) email address.

Add subject, message, and attachment (if any).

Just the final, step send the mail.

If you still have problem sending mail check the error message inside the catch block that is generated on failure.

Sending mails from your localhost with your Gmail ID using smtp authentication.

Before starting let me tell you, I have tested this example in php5 so I cannot guarantee if this can work in the older version.To send mails from your localhost (local server) there are various techniques but I am using the Gmail with smtp, as I find it quick and easy. You will need to change just a few things rest will be same as above.

Include class file and instantiate the class. Also tell the class to use SMTP method.

Now the most crucial part. You must configure the Gmail setting correctly to work.

Add email subject line, message body, attachment (if any) and send mail just like earlier code.

Greetings cards, subscribers and validation.

I think sending mail was the heart of the script, the other section are basic and you guys are capable of doing that yourself. For the time being the greetings cards must be an image(jpg, jpeg, png, bmp, gif). you can extend that functionality if you like.

Subscribers can have two states, viz active and inactive.

I have given 4 possible options for user to send emails.

  • All subscriber includes both active and inactive subscribers.
  • Active subscribers only.
  • Inactive subscribers only.
  • Custom select where all subscribers will show and you can select among them using check box.

I have used JQuery to validate client side data and also to show greetings card based on the user choice from the drop down.

Check out the demo and download the script. Please share your comments below.

Download Project Source Code and Database

Related Post

4 Comments on “How to Send greetings cards via email using PhP and Mysql”

  1. This is perfect for sending cards during the holiday without the hassle. I’ll definitely have to try this out for Christmas later on this year.

  2. This post is so helpful. Thanks for sharing this, now I have an idea on how to send email through php.

Leave a Reply

Your email address will not be published. Required fields are marked *