Php mail to
The classic "PHP mail to" question!
In PHP, you can send an email using the mail()
function, which is a built-in function that allows you to send an email to a recipient. The basic syntax is:
mail($to, $subject, $message, $headers);
Here's a breakdown of the parameters:
$to
: The recipient's email address.$subject
: The subject line of the email.$message
: The body of the email.$headers
: An optional parameter that allows you to specify additional headers, such as the sender's email address and the email's priority.
Here's an example of how you might use the mail()
function:
$to = '[email protected]';
$subject = 'Hello from PHP!';
$message = 'This is a test email sent from PHP.';
$headers = 'From: [email protected]'. "\r\n".
'Reply-To: [email protected]'. "\r\n".
'X-Priority: 1'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
mail($to, $subject, $message, $headers);
This code sends an email to [email protected]
with the subject "Hello from PHP!" and the body "This is a test email sent from PHP.". The email is sent from [email protected]
and has a reply-to address of the same email address.
Note that the mail()
function is a simple function that relies on the underlying operating system's mail server to deliver the email. This means that you may need to configure your server's mail settings in order to use the mail()
function successfully.
Additionally, the mail()
function is not very secure and can be vulnerable to spam and other security issues. For more secure and reliable email sending, you may want to consider using a third-party email library or service, such as PHPMailer or Sendgrid.