mail()函數用於在PHP中發送電子郵件。 mail()函數可送短信. HTML消息. 和附件。
PHP mail()函數
PHP mail()函數
語法
$to:指定郵件的接收者或接收者。 收信者格式:
user@example.com
user@example.com, anotheruser@example.com
User user@example.com
User user@example.com, User2 anotheruser@example.com
$subject: 郵件主題。
$message: 發送的郵件內容
$additional_headers(可選):指定附加頭,如From,CC,BCC等。附加頭也用CRLF(\r\n)分隔。
注意:消息的每一行應使用CRLF(\r\n)分隔,並且行不應大於70個字符。
user@example.com
user@example.com, anotheruser@example.com
User user@example.com
User user@example.com, User2 anotheruser@example.com
$subject: 郵件主題。
$message: 發送的郵件內容
$additional_headers(可選):指定附加頭,如From,CC,BCC等。附加頭也用CRLF(\r\n)分隔。
注意:消息的每一行應使用CRLF(\r\n)分隔,並且行不應大於70個字符。
PHP郵件範例
文件:mailer.php
<?php
ini_set("sendmail_from", "rh3@dyhms.no-ip.org");
$to = "joke111.y@gmail.com"; //receiver address
$subject = "郵件主旨";
$message = "從 localhost 轉 NAT server 發信";
$header = "發信者: rh3@dyhms.no-ip.org\r\n";
ini_set("sendmail_from", "rh3@dyhms.no-ip.org");
$to = "joke111.y@gmail.com"; //receiver address
$subject = "郵件主旨";
$message = "從 localhost 轉 NAT server 發信";
$header = "發信者: rh3@dyhms.no-ip.org\r\n";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
如果在運行服務器上運行此代碼,它將向指定的接收方發送電子郵件。
PHP郵件:發送HTML消息
PHP郵件:發送HTML消息
要發送HTML消息. 則需在消息標題中提及Content-type text/html。
<?php
$to = "joke111.y@gmail.com";//change receiver address
$subject = "This is subject";
$message = "<h1>This is HTML heading</h1>";
$to = "joke111.y@gmail.com";//change receiver address
$subject = "This is subject";
$message = "<h1>This is HTML heading</h1>";
$header = "From: rh3@dyhms.no-ip.org\r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: text/html;charset=UTF-8 \r\n";
$header .= "MIME-Version: 1.0 \r\n";
$header .= "Content-type: text/html;charset=UTF-8 \r\n";
$result = mail ($to,$subject,$message,$header);
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
PHP郵件:使用附件發送郵件
要發送附件, 則需提及許多標題信息,參示例:
<?php
$to = "joke111.y@gmail.com";
$subject = "This is subject";
$message = "This is a text message.";
# Open a file
$file = fopen("/tmp/test.txt", "r" );//change your file location
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/tmp/test.txt");
$content = fread( $file, $size);
$to = "joke111.y@gmail.com";
$subject = "This is subject";
$message = "This is a text message.";
# Open a file
$file = fopen("/tmp/test.txt", "r" );//change your file location
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("/tmp/test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
$num = md5( time() );
# Define the main headers.
$header = "From:xyz@example.com\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
$header = "From:xyz@example.com\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$result = mail ( $to, $subject, "", $header );
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
$result = mail ( $to, $subject, "", $header );
if( $result == true ){
echo "Message sent successfully...";
}else{
echo "Sorry, unable to send mail...";
}
?>
沒有留言:
張貼留言