Sending Email in PHP Using mail() Function


If you have to send email in PHP, then simply use the in-built mail() function. It must be enabled by your PHP web host. It normally is. If you need to see whether PHP mail is activated or not, just try the simplest example from below. If the mail is delivered, then the host supports PHP mail.

Here’s a simple format for using mail

mail(to, subject, message, additional headers, additional parameters)

to
The receiver’s email address. Format: ashish@gmail.com or ashish@gmail.com, michael@gmail.com or Ashish <ashish@gmail.com>

subject
The email’s subject.

message
The email’s message. A single line should not be longer then 70 characters. For new lines use “rn” (including the double quotes). If your message lines are longer than 70 characters use the wordwrap function wordwrap($message, 70, “rn”);. Message can be in HTML as well.

additional headers
Additional information like the from, CC, BCC, reply-to email.

additional parameters
Any other additional parameters. This is quite advanced and is not always required.

Example:

Here’s a bit more advanced example:

Headers can contain a whole lot of other information. Here’s how you use headers to the fullest.

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers