Friday, May 15, 2020

phpMyPassion

How to Send an Email Using PHPMailer ?

                                                            
Many of developers struggle to integrate send mail functionality in a website. So I thought why do we make the easy process for sending mail functionality by post. 

                                                              Image Source:- Google Images

Here I comes with a simplified process for sending an email using PHPMailer. I integrated this many of websites developed by me using PHP.   I have created my own code using PHPMailer library to send an emails easily.

So first of all I am providing the introduction of PHPMailer as below.

PHPMailer:-

PHPMailer is a library that is used to send emails safely and easily using PHP code from a web server. In a website Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming. PHPMailer simplifies the process of sending emails easily.

Process to Integrate:-

Step:- 1 Clone this git repository or download it using below URL.
 
git clone https://github.com/phpmypassion/phpmailer.git

Step:- 2 Update composer for latest update of library.

composer update

Step:- 3 Include function.php where you want to send mail.

<?php
   require 'functions.php'; 

Step:- 4 Now call send_mail() function with param

sendemail($to, $from, $fromname, $subject, $message);

$to :- Email id on that you want to send an email.
$from :-  Email id from which  you want to send an email.
$fromname :- Your business name.
$subject :- What is all about that mail.
#message :- Your mail content

Your file for sending mail could be look like below:-

I have used below file after filling up contact us form for sending email. 

if (isset($_POST['con_submit']) && !empty($_POST['con_submit'])) {
			// PREPARE THE BODY OF THE MESSAGE
			$message = '<html><body>';
			$message .= 'PhpMyPassion.com - Hi admin, you got a new query.';
			$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
			$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['con_name']) . "</td></tr>";
			$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['con_email']) . "</td></tr>";
			$message .= "<tr><td><strong>Phone Number:</strong> </td><td>" . strip_tags($_POST['con_phonenumber']) . "</td></tr>";
			$message .= "<tr><td><strong>Interested for:</strong> </td><td>" . strip_tags($_POST['con_intrest']) . "</td></tr>";
			$message .= "<tr><td><strong>Message:</strong> </td><td>" . $_POST['con_message'] . "</td></tr>";
			$message .= "</table>";
			$message .= "</body></html>";

			//  MAKE SURE THE "FROM" EMAIL ADDRESS DOESN'T HAVE ANY NASTY STUFF IN IT
			$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i";
			if (preg_match($pattern, trim(strip_tags($_POST['con_email'])))) {
				$cleanedFrom = trim(strip_tags($_POST['con_email']));
			} else {
				header('Location: thank_you.php?msg=fail');
			}
			if (empty($_POST['con_name'] || $_POST['con_email'] || $_POST['con_phonenumber'] || $_POST['con_intrest'])) {
				header('Location: thank_you.php?msg=empty');
			} else {
				
					$to = 'to@gmail.com';

					$subject = 'PhpMyPassion.com Contact Us';

					$thanksemail = $_POST['con_email'];
					$message2 = lead_user_mail_format();

					$fromname = 'PhpMyPassion.com';
					$from = 'support@gotPhpMyPassion.com.com';
					if ($to != '' && $thanksemail != '') {
						$res = sendemail1($to, $from, $cc, $fromname, $subject, $message);
						$res1 = sendemail($thanksemail, $from, $fromname, $subject, $message2);
						if ($res && $res1) {
							header('Location: thank_you.php?msg=success');
						} else {
							header('Location: thank_you.php?msg=fail');
						}
					} else {
						header('Location: thank_you.php?msg=fail');
					}
				
			}
		}
		else {
			header('Location: thank_you.php?msg=fail');
		}

I hope this article simplified the process for sending an emails to any number of users.


About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.