This PHP tutorial will show you how to make use of mail function from PHP. This is just for a beginner to see how he can send e-mails using PHP. In the future I will present SwiftMailer a powerful PHP library for sending e-mails. An e-mail send with mail function has the following form:
Destination represents the e-mail address to who we want to send the e-mail. There can be multiple addresses in the destination. The subject represents the subject of the e-mail. The message represents the content of the message you want to send. The header can have multiple things: From, Cc, Bcc being separated by the CRLF (\r\n) characters.. I will talk about it below. All e-mail must have the field from. This can be achieved by settings it up in header or in php.ini.
Mail function returns TRUE is the message was accepted for delivery or FALSE if not. If the message was accepted for delivery it’s not a must to be delivered.
Let’s see a basic usage:
The code above will send an e-mail to firstname.lastname@example.org with $subject and $msg for message.
Next we will add a header to our message:
Please note the comma after first $to. You can add as many address you want. For further information and examples check:
PHP Mail Function
It is a good practice to add an ‘if statement’ and see the if the result of mail is ‘TRUE’ or ‘FALSE’ and know how to deal with it then.
Thanks for taking the time to read this PHP tutorial on mail function.