PHP 메일() 함수
예시
간단한 이메일 보내기:
<?php
// the message
$msg = "First line of text\nSecond line of text";
// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);
// send email
mail("[email protected]","My subject",$msg);
?>
정의 및 사용
mail() 함수를 사용하면 스크립트에서 직접 이메일을 보낼 수 있습니다.
통사론
mail(to,subject,message,headers,parameters);
매개변수 값
Parameter | Description |
---|---|
to | Required. Specifies the receiver / receivers of the email |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters |
message | Required. Defines the message to be sent. Each line should be separated with a LF (\n).
Lines should not exceed 70 characters.
Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: |
headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).
Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file. |
parameters | Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) |
기술적 세부 사항
반환 값: | 주소 매개변수 의 해시 값을 반환 하거나 실패하면 FALSE를 반환합니다. 참고: 이메일이 전달되도록 승인된 경우에도 이메일이 실제로 전송 및 수신되었음을 의미하지는 않습니다. |
---|---|
PHP 버전: | 4+ |
PHP 변경 로그: | PHP 7.2: headers 매개변수는 배열도 허용합니다. PHP 5.4: headers 매개변수에 대한 헤더 삽입 보호 기능이 추가되었습니다. PHP 4.3.0: (Windows 전용) 모든 사용자 정의 헤더(예: From, Cc, Bcc 및 Date)가 지원되며 대소문자를 구분하지 않습니다. PHP 4.2.3: 안전 모드에서 매개변수 매개 변수가 비활성화됨 PHP 4.0.5: 매개 변수 매개 변수가 추가됨 |
더 많은 예
추가 헤더가 포함된 이메일 보내기:
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
HTML 이메일 보내기:
<?php
$to = "[email protected], [email protected]";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
mail($to,$subject,$message,$headers);
?>
❮ 완전한 PHP 메일 참조