How to send PHP mail

Create a mail sender with PHP: learn how to use the mail function, create HTML emails, and send attachments in no time!

Sending PHP Mail

Sending a mail using PHP is a simple and straightforward process. PHP offers a few different methods for sending an email. The most common method is to use the mail() function.


$to = '[email protected]';
$subject = 'The subject';
$message = 'The message';
$headers = 'From: [email protected]';

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

The mail() function accepts four parameters: the recipient, the subject, the message, and the headers. The recipient parameter is a string containing the email address of the recipient. The subject parameter is a string containing the subject of the email. The message parameter is a string containing the contents of the email. Lastly, the headers parameter is a string containing additional headers, such as the sender's email address.

The mail() function also supports attachments. To add an attachment to an email, use the addAttachment() method. The addAttachment() method accepts two parameters: the path to the attachment, and the name of the attachment. For example:


$attachmentPath = '/path/to/attachment.pdf';
$attachmentName = 'attachment.pdf';

mail->addAttachment($attachmentPath, $attachmentName);

Once all the parameters have been set, the mail() function can be used to send the email. The mail() function returns true if the email was sent successfully, or false if the email was not sent.

Sending an email using PHP is a quick and easy process. The mail() function can be used to send an email with or without attachments. Once the parameters have been set, the mail() function can be used to send the email. The mail() function returns true if the email was sent successfully, or false if the email was not sent.

Answers (0)