Home  >  Article  >  Backend Development  >  How to send email using PHP HTTP request

How to send email using PHP HTTP request

PHPz
PHPzOriginal
2023-05-21 19:10:521370browse

PHP is a widely used programming language, and one of its common applications is sending emails. In this article, we will discuss how to send emails using HTTP requests. We will introduce this topic from the following aspects:

  1. What is an HTTP request
  2. Basic principles of sending emails
  3. Using PHP to send HTTP requests
  4. Sample code for sending mail
  5. What is HTTP request

HTTP request refers to a request sent to the web server to obtain web resources. HTTP is a protocol used to transfer information between web browsers and web servers. An HTTP request consists of HTTP method, request headers and request body.

The HTTP method refers to the operation the client wants to perform. For example, the common GET and POST methods. Request headers contain information about the request, such as the requested URL and the requested HTTP version. The request body contains the data sent to the server.

  1. Basic principles of sending emails

Before understanding how to use HTTP requests to send emails, we need to understand the basic principles of sending emails. When we send an email, a message is sent to the SMTP server. The SMTP server is responsible for delivering the email to the recipient's mail server.

The process of sending an email is as follows:

  1. Create an email and fill in relevant information, such as sender, recipient, subject, etc.
  2. The email is sent to the SMTP server.
  3. The SMTP server verifies the identity of the sender and sends the message to the recipient's mail server.
  4. The recipient's mail server verifies the email and then notifies the user of the email.
  5. Sending HTTP requests using PHP

PHP provides some built-in functions to easily work with HTTP requests. These functions include file_get_contents() and curl(). The file_get_contents() function can obtain content from the specified URL through the GET or POST method. The curl() function can use various HTTP methods, such as GET, POST, PUT and DELETE.

  1. Sample Code

Here is some sample code to demonstrate how to send an HTTP request using PHP to send an email.

Use the file_get_contents() function:

$url = 'http://smtp.gmail.com:587';
$data = array(
    'username' => 'your_username',
    'password' => 'your_password',
    'from' => 'your_email',
    'to' => 'receiver_email',
    'subject' => 'This is the email subject',
    'body' => 'This is the email body'
);

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded
",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

if ($result === false) {
    die("Sending email failed.");
} else {
    echo "Email sent successfully.";
}

Use the curl() function:

$url = 'http://smtp.gmail.com:587';
$data = array(
    'username' => 'your_username',
    'password' => 'your_password',
    'from' => 'your_email',
    'to' => 'receiver_email',
    'subject' => 'This is the email subject',
    'body' => 'This is the email body'
);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

$result = curl_exec($curl);

if ($result === false) {
    die("Sending email failed.");
} else {
    echo "Email sent successfully.";
}

curl_close($curl);

In the above code, we first set the various parameters of the email, including the sender, Recipient, subject and text, etc. Then use the file_get_contents() function or curl() function to send an HTTP request to the SMTP server to send the email. Finally, determine whether the sending is successful based on the return result.

Summary

So far, we have discussed the method of using PHP to send HTTP requests to send emails. We first learned the basics of HTTP requests, and then introduced the basic principles of sending emails. Next, we discussed how to use PHP built-in functions to send emails. Finally, we give some sample code to demonstrate how to use the file_get_contents() function and curl() function to send emails.

The above is the detailed content of How to send email using PHP HTTP request. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn