create your own

How to use the 'mail' function in PHP

74
rate or flag this page

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

RSS for comments on this Hub

Gloria Cowdery profile image

Gloria Cowdery  says:
3 months ago

Thankyou, you have 'saved' my husband and I!

Gloria Cowdery

steelbreeze profile image

steelbreeze  says:
3 months ago

Feels good to hear that.. my pleasure!!

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working