Use of mail() Function in PHP on Ubuntu

The mail() function is a built-in PHP function that is used to send email from the localhost. But some configurations are required to send email using this function. You will need the real mail server information for setting up the configuration. You can use the mail server information of your active hosting server, or you can use any free mail server information. A Gmail account and the SMTP server information of Gmail are used in this tutorial to send email using the mail() function.

Setup Configuration

Two files will be required to modify in order to set up the configuration: the php.ini file and another file is required to configure to set up the mail server. Follow the steps shown below to set up the configuration. Many packages are available in PHP for sending an email. msmtp is a PHP mail server client that is used in this tutorial to send email using the PHP mail() function.

Steps:

Run the following command to install the msmtp package on Ubuntu to set up the mail server:

$ sudo apt-get install msmtp

Create a configuration file with the named /etc/msmtprc with the following command:

$ sudo nano /etc/msmtprc

Add the following content to the file. Add the valid email address for the user and the valid password for the password that will be used to send an email. Set the sender email address for form value.

defaults

tls on

tls_starttls on

tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default

host smtp.gmail.com

port 587

auth on

user username@gmail.com

password user_password

from sender_email_address

Run the following commands to set the ownership and permission for /etc/msmtprc file:

$ sudo chown fahmida /etc/msmtprc

$ sudo chmod 0600 /etc/msmtprc

Open the php.ini file and modify the following content with the following values:

SMTP=smtp.gmail.com

smtp_port=587

sendmail_path = "/usr/bin/msmtp -t -i"

Syntax:

bool mail (string $to, string $subject, string $message [, mixed $additional_headers [, string $additional_parameters ]])

This function can take four arguments. The first argument takes the receiver’s email address. The second argument takes the subject of the email. The third argument takes the email body. The last argument is optional, and it contains additional information of the email as a string or an array.

Sending email using mail() function

Different examples of sending an email using the mail() function of PHP are shown in this section of the tutorial.

Example 1: Send a simple text email

This example shows how a simple text email can be sent using the mail() function. Create a PHP file with the following script.

You have to set a valid email address for $to variable to get the output of the following script. A simple text is set as a message body.

<?php


//Set the reciever's email address

$to = "receiver@gmail.com";

//Set the subject of the email

$subject = "It is a testing email";

//Set the email body

$message = "It is testing email body";

//Set the header information

$headers = "From: sender@gmail.com\r\n";

$headers .= "Reply-To: sender@gmail.com\r\n";


//Send email using message mail() function

if(mail($to,$subject,$message,$headers))

{

echo "Email has sent successfully.\r\n";

}

else{

echo "Email has not sent. <br />";

}

?>

The following output will appear after running the script from the webserver if the mail() function works properly.


The following output will appear if you check the inbox of the email address that has been set as the receiver email address.

Example 2: Send an HTML formatted email

This example shows how a HTML formatted email can be sent using mail() function. Create a PHP file with the following script.

$message variable is initialized with HTML content that will be sent as an email body. It is mentioned in the $header variable that the content of the email is HTML.

<?php

        //Set the reciever's email address

        $to = "wdpfr41@gmail.com";

        //Set the subject of the email

        $subject = 'Registration Successful';


        //Set the email body

        $message = '

       

               

                        <h2>Your registration is completed successfully.</h2>

                        <p>username = <b>fahmida20</b><br />

                        password = <b>aswQ34ghqw</b></p>

               

       

        '
;


        //Set carriage return

        $nl = "\r\n";

        //Set the Content-type header

        $headers = 'MIME-Version: 1.0'.$nl;

        $headers .= 'Content-type: text/html; charset=iso-8859-1'.$nl;


        //Set the additional headers information

        $headers .= 'To: Mehrab Hossain '.$nl;

        $headers .= 'From: Admin '.$nl;

        $headers .= 'Cc: staff@example.com'.$nl;

        //Send email using message mail() function

        if(mail($to,$subject,$message,$headers)){

                echo "Email has sent successfully.\r\n";

        }

        else{

                echo "Email has not sent. \r\n";

        }

?>

The following output will appear after running the script from the webserver if the mail() function works properly.


The following output will appear if you check the inbox of the email address that has been set as the receiver email address.

Example 3: Send an email with the attachment

This example shows how an email can be sent by attaching a text file. Create a text file with the following script.

The content of the text file is read and encoded before attachment and the md5() function is used in the script to generate a random number for mixed content. All necessary information is added in $headers and $message variables before sending the email.

<?php

        //Set the reciever's email address

        $to = "wdpfr41@gmail.com";

        //Set the subject of the email

        $subject = "File Attachment";


        //Set carriage return

        $nl = "\r\n";

        //Set a random hash that will be used for mixed content

        $separator = md5(time());


        //Read the content of the attached file

        $filename = 'myfile.txt';

        $content = file_get_contents($filename);

        $content = chunk_split(base64_encode($content));


        //Set the email message with attachment information

        $message = "--" . $separator . $nl;

        $message .= "Content-Type: text/plain; charset="iso-8859-1"" . $nl;

        $message .= "Content-Transfer-Encoding: 8bit" . $nl;

        $message .= "A text file is attached with the email." . $nl;

        $message .= "--" . $separator . $nl;

        $message .= "Content-Type: application/octet-stream; name="" . $filename . """ . $nl;

        $message .= "Content-Transfer-Encoding: base64" . $nl;

        $message .= "Content-Disposition: attachment" . $nl;

        $message .= $content . $nl;

        $message .= "--" . $separator . "--";


        //Set the Content-type header

        $headers = 'MIME-Version: 1.0'.$nl;

        $headers .= "From: fahmida18bd@gmail.com".$nl;

        $headers .= "Reply-To: fahmida18bd@gmail.com".$nl;

        $headers .= "Content-Type: multipart/mixed; boundary="" . $separator . """ .$nl;


        //Send email using message mail() function

        if(mail($to,$subject,$message,$headers)){

                echo "Email has sent with attachement successfully.\r\n";

        }

        else{

                echo "Email has not sent. \r\n";

        }

?>

The following output will appear after running the script from the webserver if the mail() function works properly.


The following output will appear if you check the inbox of the email address that has been set as the receiver email address.

Conclusion

Many ways exist in PHP to send emails. PHP mail() function does not work properly on Ubuntu after setting the configuration. This tutorial shows the necessary configurations and the way of sending emails using the PHP mail() function.



from Linux Hint https://ift.tt/35g6Vh0

Post a Comment

0 Comments