codescracker


php

PHP Send Emails



« Previous Tutorial Next Tutorial »


Here you will learn about how you can easily send email using PHP. You can also use PHP to send bulk email at once.

What to do Before Sending Email using PHP

Before sending any email using PHP, firstly you must have to setup your environment so that you can send email using PHP without any distruption.

Let's see how to setup environment for XAMPP users to send email from localhost using PHP.

PHP XAMPP Setup to Send Email

Using PHP, you are free to send email from your system with the help of XAMPP.

But before sending email using XAMPP from your computer system, you have to open the php.ini and sendmail.ini file and make some changes.

You can find php.ini file in the directory C:\xampp\php\ and sendmail.ini file in the directory C:\xampp\sendmail\.

Let's first edit the php.ini file as per the instruction given below.

Search for SMTP, smtp_port, sendmail_from, and sendmail_path, and then initialize smtp.gmail.com, 587, codescracker@gmail.com, and "\"C:\xampp\sendmail\sendmail.exe\" -t" to these as shown in the following code:

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from=codescracker@gmail.com
sendmail_path="\"C:\xampp\sendmail\sendmail.exe\" -t"

Now let's edit the file named sendmail.ini as per the instruction given below:

This time search for smtp_server, smtp_port, error_logfile, debug_logfile, auth_username, auth_password, and force_sender, and then initialize smtp.gmail.com, 587, error.log, debug.log, codescracker@gmail.com, mypassword, and codescracker@gmail.com to these as shown in the following sample code:

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=codescracker@gmail.com
auth_password=mypassword
force_sender=codescracker@gmail.com

You only have to replace codescracker@gmail.com with your own email id (this email id will be used to send the emails to anyone) and mypassword with your own password.

After doing all the settings that are provided above, you have to restart your XAMPP server then you can go further to write your email sending code using PHP to start sending email using the code. Let's see how to do this.

Note - If you will find some error after successfully setup according to the above instructions then you can easily find and fix the error using the file present in the directory C:\xampp\sendmail\. In this directory search for the file named error.txt and open that file to find and fix any of your error.

PHP Send Email Example

Let's take an example to understand how to write code to send emails using PHP.

<?php 
	// below variable contains the email address of the person
	// who will get the email using this example of sending email
	$sendEmailTo = "codescrackerParther@gmail.com";
	// below variable contains the subject value
	$sendEmailSubject = "Hello partner, greeting email for you";
	// now put the message in a php variable
	$sendEmailMessage = "How are you my partner";
?>
<html>
<head>
	<title>Sending Emails using PHP Example - codescracker.com</title>
</head>
<body>
<?php
	// now set the content-type before sending html email
	$headers = "MIME-Version: 1.0 \r\n";
	$headers = $headers . "Content-type:text/html;charset=UTF-8 \r\n";
	$headers = $headers . "From:codescracker@gmail.com \r\n";
	$checkMail = mail($sendEmailTo, $sendEmailSubject, $sendEmailMessage, $headers);
	if($checkMail == true)
	{
		echo "Your email sent successfully to your friend.";
		exit();
	}
	else 
	{
		echo "Error occurred while sending the email, try again";
		echo "<br/>exiting...";
		exit();
	}
?>
</body>
</html>

Here is the sample output produced by the above email sending example using PHP after sending the email successfully.

php send email

And here is the output you will see if some error occurred while sending the email using the above email sending example:

sending email using php example

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test