PHP Email Tutorial, Sending email from PHP


 

PHP Email Tutorial, Sending email from PHP

It is much easier to send email from PHP than from Perl. Read this tutorial if you have any doubts.

It is much easier to send email from PHP than from Perl. Read this tutorial if you have any doubts.

Sending mail in PHP

In this section we will learn how to send email from PHP program.

You can send email from your PHP script using mail(...) function.

Syntax:
bool mail(string to, string subject, string message, string additional_headers).
will send e-mail message with the message body containing in message feild to the receiver specified in to field.
You can assign several destination addresses in the to field by placing them in the string separated by commas or blank spaces.

Example 1.
mail("[email protected], [email protected], [email protected]",
    "Test subject",
    "Test\n message body\n\n End line");


Also you can assign values to variables and then pass them to function call.

Example 2.
$email="[email protected], [email protected]";
$subject="Test subject";
$letter="Test\n message body\n\n End line";
mail($email, $subject, $letter);

So owner of the e-mail address admin@yourdomain.com receives "Test msubject" letter. You can control Reply-To, From message fields and other special fields passing the info into additional parameters string in the last mail() function argument.

mail("[email protected]",
    "Test letter 2",
    "Test\n body 2\n\n End line",
    "From: webmaster@$SERVER_NAME\nReply-To: webmaster@$SERVER_NAME\nX-Mailer: PHP/" . phpversion());

Additional mail message fields separated by new line symbol (\n).

Example 3.
mail($email, "Subscription",
    "$letter",
    "From: [email protected]\nReply-To: [email protected]\nX-Mailer: PHP/" . phpversion());


 

Ads