php email,PHP发送电子邮件(Email)

本文详细介绍了如何使用PHP的mail()函数发送电子邮件,包括文本、HTML消息及带有附件的邮件。首先,设置邮件的基本信息如发件人、收件人、主题和正文。然后,通过添加额外头部信息,可以指定Content-Type以发送HTML邮件。最后,通过读取文件并进行base64编码,可以添加附件到邮件中。提供的代码示例展示了如何实现这一过程。
摘要由CSDN通过智能技术生成

本篇文章帮大家学习php发送电子邮件(Email),包含了PHP发送电子邮件(Email)使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

PHP mail()函数用于在PHP中发送电子邮件。 您可以使用PHP mail()函数来发送短信,HTML消息和附件与消息。

PHP mail()函数

语法

$subject: 代表邮件的主题。

$message: 表示要发送的邮件的消息。

$additional_headers(可选):指定附加头,如From,CC,BCC等。额外的附加头也应该用CRLF(\r\n)分隔。

注意:消息的每一行应使用CRLF(\r\n)分隔,并且行不应大于70个字符。

PHP邮件示例

文件:mailer.php

ini_set("sendmail_from", "maxsujaiswal@jikedaquan.com");

$to = "maxsujaiswal1987@gmail.com";//change receiver address

$subject = "This is subject";

$message = "This is simple text message.";

$header = "From:maxsujaiswal@jikedaquan.com \r\n";

$result = mail ($to,$subject,$message,$header);

if( $result == true ){

echo "Message sent successfully...";

}else{

echo "Sorry, unable to send mail...";

}

?>

如果在运行服务器上运行此代码,它将向指定的接收方发送电子邮件。

PHP邮件:发送HTML消息

要发送HTML消息,您需要在消息标题中提及Content-type text/html。

$to = "abc@example.com";//change receiver address

$subject = "This is subject";

$message = "

This is HTML heading

";

$header = "From:xyz@example.com \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...";

}

?>

PHP邮件:使用附件发送邮件

要使用附件发送消息,您需要提及许多标题信息,在下面给出的示例中使用。

$to = "abc@example.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));

# Get a random 32 bit number using time() as seed.

$num = md5( time() );

# Define the main headers.

$header = "From:xyz@example.com\r\n";

$header .= "MIME-Version: 1.0\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";

# 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--";

# Send email now

$result = mail ( $to, $subject, "", $header );

if( $result == true ){

echo "Message sent successfully...";

}else{

echo "Sorry, unable to send mail...";

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值