Forum Topic: Php: Mail

(817 views • 2 replies)

This topic is 1 page long.

<< < > >>
None

NinoGrounds

Reply To Post Reply & Quote

Posted at: 10/21/06 12:44 PM

NinoGrounds EVIL LEVEL 17

Sign-Up: 11/28/05

Posts: 3,758

PHP: Main

The mail function is used to send E-mail Messages through the SMTP server specified in the php.ini Configuration file. Thus, if it is wrong configured, it will not work. Here, you won't learn how to make a client which will also accept, but it's doable. You can help yourself on PEAR and PECL Repository (someone should make an Tutorial how to install/extend PHP libraries) .

PHP's mail() function works thus:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

It is important for you to understand that it returns a boolean true/false on success or failure, respectively.

This example will send message "message" with the subject "subject" to email address "example@domain.com". Also, the receiver will see that the eMail was sent from "Example2 <example2@domain.com>" and the receiver should reply to "Example3 <example3@domain.com>"

<?php
mail(
"example@domain.tld", // E-Mail address
"subject", // Subject
"message", // Message
"From: Example2 <example2@domain.com>\r\nReply-to: Example3 <example3@domain.com>) // Additional Headers
;
?>

These headers are optional though.
Also, there is no requirement to write E-mail addresses in format "Name <email>", you can just write "email".

This will send the same message as the first example but includes From: and Reply-To: headers in the message. This is required if you want the person you sent the E-mail to reply to you. Also, some E-mail providers will assume mail is spam if certain headers are missing so unless you include the correct headers you mail will end up in the junk mail folder.

Keep in mind, though, that by default mail() sends plain text message, so to make a HTML message, you should use this headers:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Note: \r\n is actually \n, a newline, but you must use \r\n to keep compatibility.

And that's it! To make something like newsletter, all you need is to run a loop and use mail() .

Much love


None

elbekko

Reply To Post Reply & Quote

Posted at: 10/21/06 01:18 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,587

Nice tutorial, good job =)

"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

None

Muncey

Reply To Post Reply & Quote

Posted at: 2/23/07 04:28 PM

Muncey EVIL LEVEL 10

Sign-Up: 10/24/05

Posts: 815

Great tutorial, thanks. You people don't get enough thanks for these, 201 views and only 1 reply... pathetic really.

Thanks again.


All times are Eastern Standard Time (GMT -5) | Current Time: 07:19 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!