使用PHP发送邮件

在进行该项目时,我必须创建一个特定的“申请人问卷”,在该问卷中,我必须将整个调查表发送到伤口后面指示的电子邮件地址,并且我立刻想起了有关PHP的mail()函数。


bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]]) 

必填参数:
  • 收件人电子邮件
  • 信标题
  • 字母文字

可选参数:
  • 其他电子邮件标题
  • 高级命令行选项

返回值:
  • 如果信件已被接受则为真
  • 否则为假。


最简单的例子

 <?php mail("E-mail ", "", "  \n 1-  \n 2-  \n 3- "); ?> 


让我们继续一个更复杂的例子。


 <?php $to = "<mail@example.com>, " ; $to .= "mail2@example.com>"; $subject = " "; $message = ' <p> </p> </br> <b>1-  </b> </br><i>2-  </i> </br>'; $headers = "Content-type: text/html; charset=windows-1251 \r\n"; $headers .= "From:    <from@example.com>\r\n"; $headers .= "Reply-To: reply-to@example.com\r\n"; mail($to, $subject, $message, $headers); ?> 


首先,我们确定将信件发送给谁,&to变量对此负责,但是,如果有多个收件人,我们将电子邮件地址写成逗号分隔。 邮件。

我不会描述变量$ subject和$ message,这已经很清楚了。

在我们的示例中,$ headers变量包含3行:
  • 在第一行中,我们确定发送的HTML字母和windows-1251编码。
  • 在第二个中,我们指出这封信来自谁。
  • 在第3位指示电子邮件地址,以回复信件。


现在最有趣的带有附件的发送信(附件)


 $subject = " "; $message =" "; //  ,     , , ,    .. $filename = "file.doc"; //   $filepath = "files/file.doc"; //   //      ,    $boundary = "--".md5(uniqid(time())); //   $mailheaders = "MIME-Version: 1.0;\r\n"; $mailheaders .="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n"; //       boundary $mailheaders .= "From: $user_email <$user_email>\r\n"; $mailheaders .= "Reply-To: $user_email\r\n"; $multipart = "--$boundary\r\n"; $multipart .= "Content-Type: text/html; charset=windows-1251\r\n"; $multipart .= "Content-Transfer-Encoding: base64\r\n"; $multipart .= \r\n; $multipart .= chunk_split(base64_encode(iconv("utf8", "windows-1251", $message))); //     //   $fp = fopen($filepath,"r"); if (!$fp) { print "   22"; exit(); } $file = fread($fp, filesize($filepath)); fclose($fp); //   $message_part = "\r\n--$boundary\r\n"; $message_part .= "Content-Type: application/octet-stream; name=\"$filename\"\r\n"; $message_part .= "Content-Transfer-Encoding: base64\r\n"; $message_part .= "Content-Disposition: attachment; filename=\"$filename\"\r\n"; $message_part .= \r\n; $message_part .= chunk_split(base64_encode($file)); $message_part .= "\r\n--$boundary--\r\n"; //    ,       $multipart .= $message_part; mail($to,$subject,$multipart,$mailheaders); //   //   60 . if (time_nanosleep(5, 0)) { unlink($filepath); } //   

Source: https://habr.com/ru/post/zh-CN444744/


All Articles