These days E-mail is the powerful way of communicating with each other without spending a single buck. If you are  a Php developer and wish to send e-mail by using Php then you are at the right place. Here i am going to tell you how you can send plain text email, HTML email and email with attachment by php email form.

Php have a quite simple mail function to send e-mail. In-fact the function name is ‘mail’. Let me introduce you this function.

mail($to,$subject,$message,$header)

$to : E-mail address of the receiver(Required).

$subject : E-mail subject(Required).

$message : E-mail message body(Required).

$header : for sending additional information like sender email, reply to etc.(Optional)

 

Sending Plain Text mail:-

1
2
3
4
5
6
7
8
9
10
11
<?php
 
$to="ankursharma@7tech.co.in";
 
$sub="This is a plain text mail!";
 
$msg="This is the message body! \n\n From:\n 7tech.co.in team";
 
mail($to,$sub,$msg);
 
?>

Use \n for next line.

 

Sending HTML mail:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
 
$to="ankursharma@7tech.co.in";
 
$from="info@7tech.co.in";
 
$sub="This is a plain text mail!";
 
$msg="This is the message body! <br/><br/> From:<br/><a href='http://www.7tech.co.in'>7tech.co.in team</a>";
 
$headers  = "From: $from\r\n";
 
$headers .= "Content-type: text/html\r\n";
 
mail($to,$sub,$msg,$headers);
 
?>

 

Sending HTML mail with Attachment:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
$file_path = "file.html"; // server path where file is placed
$file_path_type = "text/html"; // File Type
$file_path_name = "newfilename.html"; // this file name will be used at reciever end 
 
$from = "info@7tech.co.in"; // E-mail address of sender
$to = "ankursharma@7tech.co.in"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.&lt;br&gt;&lt;br&gt;Thank You!&lt;br&gt;&lt;a href='http://7tech.co.in'&gt;7tech.co.in Team&lt;/a&gt;"; 
 
$headers = "From: ".$from; 
 
$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file); 
 
$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x"; 
 
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\""; 
 
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n"; 
 
$data = chunk_split(base64_encode($data)); 
 
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";  
 
if(@mail($to, $subject, $message, $headers)) {
echo "File send!";
 
} else {
echo 'Failed';
}
?>