php - mail

Development/PHP 2013. 4. 6. 16:49


Headers are the parts seen at the top of emails. Typically :

To :A comma seperated list of recipient emails.
From :The senders email address.
Reply-To :The email address where replies should be sent to.
Return-Path :Kinda the same thing as the Reply-To. Some email clients require this, others create a default.
Subject :Subject of the email.
CC :Carbon Copy. A comma seperated list of more recipients that will be seen by all other recipients.
BCC :Blind Carbon Copy. A comma seperated list of more recipients that will not be seen by any other recipients.

The TO and SUBJECT filds have already been discussed as the first and second variables in the MAIL command. The extra headers seen above can be entered as a 4th variable in the MAIL command. 

<?php
mail("yourplace@somewhere.com","My email test.","Hello, how are you?","From: myplace@here.com\r\nReply-To: myplace2@here.com\r\nReturn-Path: myplace@here.com\r\nCC: sombodyelse@noplace.com\r\nBBC: hidden@special.com\r\n");
?>

Looks kinda messy when it's all in there without using variables. That above example has all of the extra headers being declared. You may specify all or some or none as you desire. There are 2 very important things to note here : 

1.These headers need their NAMES: shown unlike the first three variables in the mail command. The header names are CaSe SeNsItIvE. Use per example.
2.Each header is ended with the Return and Newline characters. \r\n There are no spaces between each header.

For better organization, these extra headers can be declared in a variable string like our previous examples. 

<?php
$to = "yourplace@somewhere.com";
$subject = "My email test.";
$message = "Hello, how are you?";

$headers = "From: myplace@here.com\r\n";
$headers .= "Reply-To: myplace2@here.com\r\n";
$headers .= "Return-Path: myplace@here.com\r\n";
$headers .= "CC: sombodyelse@noplace.com\r\n";
$headers .= "BCC: hidden@special.com\r\n";

if ( mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
   } else {
   echo "The email has failed!";
   }
?>

The $headers variable could have been typed out into one long string, but that is a bit much when it comes to editing and viewability. Instead our example shows each one seperated and the .= combines them together.


출처 - http://www.htmlite.com/php029.php



'Development > PHP' 카테고리의 다른 글

php - mail() 함수 사용시 한글 깨짐 문제  (0) 2013.04.06
php - 점(.) 연산자  (3) 2013.04.06
copyright 년도 자동표기하기  (0) 2013.03.06
php - include vs require  (0) 2013.01.08
php - date() 오류  (0) 2012.07.21
Posted by linuxism
,