How to send emails from php | php से ईमेल कैसे भेजें: In this tutorial, you will learn how to send simple text or HTML email directly from the script using the PHP mail () function. The best way to send an email is to send an email from your hosting server with a simple php code. This method sends emails very quickly. Using this method, the email is not sent to the samp folder. Email goes directly to the Inbox folder.
Also Read: Google Analytics api – Google Analytics how to add php website
Also Read: Best USA loan company | Get loans for home improvements
So read the full article then you will understand how to send email using php mail () function send emails from php. You can also use it in your designated place as needed. Let’s get started.
What is the PHP mail() Function?
Sending email messages is very common for web applications, that means every website will need send email to user for different reasons. For example,
- sending a email OTP for verify email id when user creates an account on your website,
- sending a welcome email when a user creates an account on your website,
- sending a email for user password reset,
- sending a newsletter to your registered users, or receiving user feedback or comments through a website contact,
- sending offer details etc.
For sending a email You can use the PHP built-in mail () function. You can compose and send email messages to one or more recipients at same time from your php application sing mail () function. Either in plain text format email or in formatted html email also. The basic syntax of this php mail () function is- mail(to, subject, message, headers, parameters)
mail(to, subject, message, headers, parameters)
The following table summarizes all the Parameter (features) of this function –
Paramete | Description | |
to | The recipient’s email address. | Required |
subject | Subject of the email to be sent. This parameter i.e. the subject line cannot contain any newline character (\n). | Required |
message | Defines the message to be sent. Each line should be separated with a line feed-LF (\n). Lines should not exceed 70 characters | Required |
headers | This is typically used to add extra headers such as “From”, “Cc”, “Bcc”. The additional headers should be separated with a carriage return plus a line feed-CRLF (\r\n). | Optional |
parameters | Used to pass additional parameters. | Optional |
Email is sent through the php mail function using this basic parameters. Either in plain text format email or in formatted html email also.
How to send emails from php | php से ईमेल कैसे भेजें
Now we Email is sent through the php mail function using this basic parameters. Here we send plain text format email.
Sending plain text emails php code |सादा पाठ ईमेल भेजना php कोड
The easiest way to send an email with PHP is to send a text email. In the example below we first declare the variables – the recipient’s email address, subject line and message body – then we pass these variables to the mail () function to send the email.
<?php
$to = 'yourfriendname@email.com'; // The recipient's email address.
$subject = Test Email'; //Subject of the email to be sent
$message = 'Hi Friend, its test mail for caking'; //Defines the message to be sent.
$from = 'from@email.com'; //Where email is being sent from
// Sending email
if(mail($to, $subject, $message)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Sending a HTML formatted email php code
When we send a text message using PHP, all content will be treated as plain text only. We’re going to improve that send output email, and convert text email to HTML-formatted email.
Also Read: Create Welcome page using php with mysqli Database
The process of sending HTML email will be the same. However, this time we need to provide an HTML formatted message with additional html code. The code is given below so you can test using it if needed.
<?php
$to = 'yourfriendname@email.com'; // The recipient's email address.
$subject = Test Email'; //Subject of the email to be sent
$from = 'from@email.com'; //Where email is being sent from
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Hi friend, its test email</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Note: However, the PHP mail() function is a part of the PHP code. So you need a mail server for send an email. if you use live hosting server then you don’t need mail server because mail server is in build in your live hosting server.
The last few words
I hope you like today’s article. You must share this article with your family and friends. If you have any issues please let us know in the comment box. We will try to resolve your comment as soon as possible. If the codes do not work properly then don’t forget to let us know. If you need a complete website email code using php mail() function, please contact us. We will try to make your website v as soon as possible.