Send greetings cards via email using php and mysql

by  
Share:
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.

View Demo

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(‘youremail@gmail.com’, ‘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 SMTP authentication 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 .

<?php
require_once 'PHPMailer/class.phpmailer.php';
?>

Instantiate the phpmailer class.
<?php
/*
defaults to using php "mail()"; 
the true param means it will throw exceptions on errors, 
which we need to catch
*/
$mail = new PHPMailer(true); 
?>

Add your email address (sender) and the the person you want to send ( reciever) email address.
<?php
$mail->SetFrom('youremail@gmail.com', 'Shahrukh Khan');
$mail->AddAddress("someone@gmail.com");
?>

Add subject, message, and attachment (if any).
<?php
$message = '<html><body>';
$message .= '<h1>Sample message</h1>';
$message .= "</body></html>";

$mail->Subject = ' thesoftwareguy demo ' ;
$mail->MsgHTML($message);
$mail->AddAttachment("images/test.jpg"); 
?>

Just the final, step send the mail.
<?php
$mail->Send();
?>

If you still have problem sending mail check the error message inside the catch block that is generated on failure.
<?php
} catch (phpmailerException $e) {
  echo $e->errorMessage();
} 
?>

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.

<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
?>

Now the most crucial part. You must configure the Gmail setting correctly to work.
<?php
/ enables SMTP debug information (for testing)
$mail->SMTPDebug = 0;                    
// enable SMTP authentication 
$mail->SMTPAuth = true;                  
// sets the prefix to the server
$mail->SMTPSecure = "ssl";                 
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";      
// set the SMTP port for the GMAIL server
$mail->Port = 465;                   

//give your gmail email id here
$mail->Username    = 'test@gmail.com'; 
// always enclose your password in single quotes
$mail->Password = 'yourpassword';

?>

Add email subject line, message body, attachment (if any) and send mail just like earlier code.
<?php
$message = '<html><body>';
$message .= '<h1>Sample message</h1>';
$message .= "</body></html>";

$mail->Subject = ' thesoftwareguy demo ' ;
$mail->MsgHTML($message);
$mail->AddAttachment("images/test.jpg"); 
$mail->Send();
?>

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.
View Demo
Download

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

shivasurya on

nice one bro ! keep going !
can u say me how to send mail using html tags to make it colourful!

Reply
Shahrukh Khan on

to send html mails you need to add inline style say something like sample text Add inline style only

Reply
RIZWAN on

VERY GOOD DESING HOW CAN I DO IT IN MY FYP PROJECT PLZ SHARE WITH ME RAING TYPE THAT THINGS SOURCE CODE

Reply
Shahrukh Khan on

can u please elaborate what exactly you want to do in your project.

Reply
deepak on

shahrukh bhae can u send me simple mail function demo… its urgent . I am the frnd of zia toffer….. and khurshid sir student and also teach SMU in howrah maidan branch ..

Reply
deepak on

rply kro bhae….. agar mail send krna h server se to phpmailer use hoga ya nhi plz

Reply
Shahrukh Khan on

yes. it can be done using phpmailer using ur localserver/or online server. for further enquires.. pls contact me directly. via email or phone.

Reply
deepak on

sir give ur email address

deepak on

Its inner join from three table but this code is not working.?

SELECT product.product_name, category.category_name, sub_category.sc_id, sub_category.subcategory_name, sub_category.prices, sub_category.subcategory_Images
FROM product, category
INNER JOIN sub_category
WHERE product.pc_id = category.psc_id
AND category.psc_id = sub_category.sc_id

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0000 sec) what is the problem bro

deepak on

sir which directory i will put php mailer folder. and guide me step by step.. plz

deepak on

Sir which directiry i will put php mailer……

fahad on

awesomwe

Reply
chandani on

Hello nyc one script … By the using this script can i send auto generated mail ? or not .. ? plzz give me rply

Reply
Shahrukh Khan on

To send auto generated method you can use cron job

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.