How to use the 'mail' function in PHP
By steelbreeze
Below I'm going to explain how to use the mail function of PHP. With this function you can send mail through send mail or smtp depending on whether you use Unix/Linux or Windows respectively.
PHP makes working with email very easy, letting you send messages using a single call to the 'mail' function.
Before you can send email using the 'mail' function, you first have to set up PHP's email related options.Here are the relevant lines of an out-of-the-box php.ini file under Windows:
[mail function]
SMTP =
sendmail_from = me@localhost.com
;sendmail_path =
If you are using Windows, then the SMTP server will be used to send mail and if you are using Unix/Linux, then the local sendmail system will be used. I will assume that you know how to setup any of these and that you will configure your system accordingly or already have and move forward. If not, just use google to search for the appropriate information, there's plenty of it out there. If you are running on Windows, however, chances are that your ISP has already provided an SMTP server for your use. It's the same server you set your email program to use when you send messages. Set the SMTP setting to the host name/IP address of that server.
Set 'sendmail_from' to the default 'from' email address you want to use for messages that are sent by PHP. The 'sendmail_path' under Linux/Unix should be uncommented(i.e. remove the semicolon from the start of the line) and set to the path and file name of the 'sendmail' program on your system. Under Linux, this will usually be /usr/sbin/sendmail.
With these settings set and your Web server restarted, PHP should be checked out with full email capabilities. The syntax used is:
mail("to-address@somedomain.com", "Message Subject", "This is the body of the message.");
Mailing to multiple recipients can be accomplished if you simply separate each address with commas:
mail("to1@mail.com, to2@mail.com, ...", "Message Subject", "Message Body");
For adding From or Reply-to addresses, just add them as a fourth parameter, separated by carriage return/new line pairs
mail("to@mail.com", "Message Subject", "Message Body", "From: webmaster@host.com\r\nReply-to: admin@host.com");
If you want to use this function to send mail to addresses from a database, see this:
//Retrieve $email and $password from the database based on the $username provided in a //form.
mail($email, "Your Password", "Hi there!
You filled out a form on our website indicating that you have lost your password. As requested, we are sending it to you be email.
username: $username
password : $password
Please write down this information somewhere safe.
-The Webmaster.
");
If you have any queries, feel free to post it in the comments.
Comments
Feels good to hear that.. my pleasure!!
Function Great Work
Hi steelbreeze
i hve a problem in sending mail
actually mail gets send but when the receipient receive it
i have to display the "from" name to it with an email address
for example
"Hardik "
Here i get only "Hardik " gets displayed but not
what should i do please reply
i have to display the "from" name to it with an email address
for example
"Hardik <hardik@gmail.com>"
Here i get only "Hardik " gets displayed but not
what should i do please reply
Hardy 7 seconds ago
i have to display the "from" name to it with an email address
for example
"Hardik ≶hardik@gmail.com>"
Here i get only "Hardik " gets displayed but not <hardik@gmail.com>
what should i do please reply
mmmmmmmmmmmmmmm
asasasasas
dsds
Gloria Cowdery 2 years ago
Thankyou, you have 'saved' my husband and I!
Gloria Cowdery