How To Send Mail in PHP

In PHP, It is very easy to set up mail system.Mail system is diffrent in Windows and Linux system. How to doing this is describe below :

Introduction

The mail() function allows you to send emails directly from a script.

For the mail functions to be available, PHP requires an installed and working email system. PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed mail function

How To Configure in System :

For Windows System :

windows user should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.

The configuration for Windows should look something like this:-

[mail function]

; For Win32 only.

SMTP = smtp.secureserver.net

; For win32 only

sendmail_from = xyz@xyz.com

For Linux System :

Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive.

The configuration for Windows should look something like this:-

[mail function]

; For Win32 only.

SMTP = smtp.secureserver.net

; For win32 only

sendmail_from = xyz@xyz.com

; For Unix only

sendmail_path = /usr/sbin/sendmail -t -i

There are three types of  data send in emails :

  1. Email with simple text
  2. Email with HTML view
  3. Email with attachment

Plain text email

Syntax :

mail( to, subject, message, headers, parameters );

to : Specifies the receiver / receivers of the email. It is Required filed.

subject : Specifies the subject of the email. This parameter cannot contain any newline characters. It is Required filed.

message : Specifies the message to be sent. It is Required filed.

headers : Specifies additional headers, like From, Cc, and Bcc.  It is Optional filed.

parameters : Specifies an additional parameter to the send mail program. It is Optional filed.

After this function, PHP will attempt to send the email then it will return true if successful or false if it is failed.

Sending HTML email

When you send a text message using PHP then all the content will be treated as simple text. Even if you will include HTML tags in a text message, it will be displayed as simple text and HTML tags will not be formatted according to HTML syntax. But PHP provides option to send an HTML message as actual HTML message.

While sending an email message you can specify a Mime version, content type and character set to send an HTML email.

Example :

<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>

<?php

$to = "xyz@xyz.com";
$subject = "subject of mail";
$message = "<b>This is HTML message in mail.</b>";
$header = "From:abc@abc.com \r\n";
$header .= "Cc:def@def.com \r\n";$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$senddata= mail ($to,$subject,$message,$header);

if( $senddata== true )
{
echo "Message sent successfully...";}
else
{
echo "Opps!.. Message could not be sent...";
}
?>

</body>
</html>

 

 

You may also like...

Leave a Reply

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