Php send mail function
The classic PHP mail function!
In PHP, you can send an email using the mail()
function, which is a built-in function that sends an email message to one or more recipients. Here's a basic example:
<?php
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP';
$headers = 'From: [email protected]'. "\r\n".
'Reply-To: [email protected]'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
mail($to, $subject, $message, $headers);
?>
In this example:
$to
is the email address of the recipient.$subject
is the subject line of the email.$message
is the body of the email.$headers
is an array of headers that specify the sender, reply-to, and content type of the email.
The mail()
function takes four arguments:
$to
: The email address of the recipient.$subject
: The subject line of the email.$message
: The body of the email.$headers
: An array of headers that specify the sender, reply-to, and content type of the email.
You can also use the mail()
function with additional parameters, such as:
$additional_headers
: An array of additional headers that can be added to the email.$attachment
: A file path or a file resource that can be attached to the email.
Here's an example that includes additional headers and an attachment:
<?php
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP';
$headers = 'From: [email protected]'. "\r\n".
'Reply-To: [email protected]'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
$additional_headers = array(
'X-Mailer: PHP/'. phpversion(),
'MIME-Version: 1.0'
);
$attachment = 'path/to/file.txt';
mail($to, $subject, $message, $headers, $additional_headers, $attachment);
?>
Note that the mail()
function is a simple function that sends an email using the PHP mail() function, which is a wrapper around the system's mail function. This means that the email may not be sent immediately, and it may not be delivered to the recipient's inbox.
If you need more advanced features, such as sending emails with attachments, HTML content, or tracking, you may want to consider using a third-party library or service, such as PHPMailer or SendGrid.