php 邮件发送是html 没样式_PHP-使用PHP发送电子邮件

必须在php.ini文件中正确配置PHP ,并详细说明系统如何发送电子邮件。打开/ etc /目录中的php.ini文件,并找到标题为[邮件功能]的部分。

Windows用户应确保提供了两个指令。第一个称为SMTP,它定义您的电子邮件服务器地址。第二个称为sendmail_from,它定义您自己的电子邮件地址。

Windows的配置应如下所示:

[mail function]; For Win32 only.SMTP = smtp.secureserver.net; For win32 onlysendmail_from = webmaster@tutorialspoint.com

Linux用户只需要让PHP知道他们sendmail应用程序的位置即可。路径和任何所需的开关应指定给sendmail_path指令。

Linux的配置应该看起来像这样-

[mail function]; For Win32 only.SMTP = ; For win32 onlysendmail_from = ; For Unix onlysendmail_path = /usr/sbin/sendmail -t -i

现在您可以开始了-

发送纯文本电子邮件

PHP利用mail()函数发送电子邮件。此功能需要三个必填参数,用于指定收件人的电子邮件地址,邮件主题和实际邮件,此外还有其他两个可选参数。

mail( to, subject, message, headers, parameters );

这是每个参数的说明。

f405c85dcfea5aa12ee3f8e9fdce49c2.png

一旦调用了邮件功能,PHP将尝试发送电子邮件,如果成功,它将返回true;如果失败,则将返回false。

可以将多个收件人指定为逗号分隔列表中mail()函数的第一个参数。

发送HTML电子邮件

当您使用PHP发送文本消息时,所有内容将被视为简单文本。即使您将HTML标签包含在文本消息中,它也将显示为简单文本,并且不会根据HTML语法设置HTML标签的格式。但是PHP提供了将HTML消息作为实际HTML消息发送的选项。

发送电子邮件时,您可以指定Mime版本,内容类型和字符集来发送HTML电子邮件。

以下示例将HTML电子邮件发送到xyz@somedomain.com,并将其复制到afgh@somedomain.com。您可以使用以下方式对程序进行编码:它应接收用户的所有内容,然后发送电子邮件。

            Sending HTML email using PHP                     <?php $to = "xyz@somedomain.com";         $subject = "This is subject";                  $message = "This is HTML message.";         $message .= "

This is headline.

"; $header = "From:abc@somedomain.com "; $header .= "Cc:afgh@somedomain.com "; $header .= "MIME-Version: 1.0"; $header .= "Content-type: text/html"; $retval = mail ($to,$subject,$message,$header); if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } ?>

通过电子邮件发送附件

要发送包含混合内容的电子邮件,需要将Content-type标头设置为multipart / mixed。然后可以在边界内指定文本和附件节。

边界以两个连字符开头,后跟一个唯一的数字,该数字不能出现在电子邮件的消息部分。PHP函数md5()用于创建32位十六进制数字以创建唯一数字。表示电子邮件最后部分的最后边界也必须以两个连字符结尾。

<?php // request variables // important   $from = $_REQUEST["from"];   $emaila = $_REQUEST["emaila"];   $filea = $_REQUEST["filea"];      if ($filea) {      function mail_attachment ($from , $to, $subject, $message, $attachment){         $fileatt = $attachment; // Path to the file         $fileatt_type = "application/octet-stream"; // File Type                   $start = strrpos($attachment, '/') == -1 ?             strrpos($attachment, '//') : strrpos($attachment, '/')+1;         $fileatt_name = substr($attachment, $start,             strlen($attachment)); // Filename that will be used for the             file as the attachment                   $email_from = $from; // Who the email is from         $subject = "New Attachment Message";                  $email_subject =  $subject; // The Subject of the email          $email_txt = $message; // Message that the email has in it          $email_to = $to; // Who the email is to                  $headers = "From: ".$email_from;         $file = fopen($fileatt,'rb');          $data = fread($file,filesize($fileatt));          fclose($file);                   $msg_txt=" You have recieved a new attachment message from $from";         $semi_rand = md5(time());          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";          $headers .= "MIME-Version: 1.0" . "Content-Type: multipart/mixed;" . "            boundary="{$mime_boundary}"";                  $email_txt .= $msg_txt;         $email_message .= "This is a multi-part message in MIME format." .             "--{$mime_boundary}" . "Content-Type:text/html;             charset = "iso-8859-1"" . "Content-Transfer-Encoding: 7bit" .             $email_txt . "";         $data = chunk_split(base64_encode($data));                  $email_message .= "--{$mime_boundary}" . "Content-Type: {$fileatt_type};" .            " name = "{$fileatt_name}"" . //"Content-Disposition: attachment;" .             //" filename = "{$fileatt_name}"" . "Content-Transfer-Encoding:             base64" . $data . "" . "--{$mime_boundary}--";         $ok = mail($email_to, $email_subject, $email_message, $headers);                  if($ok) {            echo "File Sent Successfully.";            unlink($attachment); // delete a file after attachment sent.         }else {            die("Sorry but the email could not be sent. Please go back and try again!");         }      }      move_uploaded_file($_FILES["filea"]["tmp_name"],         'temp/'.basename($_FILES['filea']['name']));      mail_attachment("$from", "youremailaddress@gmail.com",          "subject", "message", ("temp/".$_FILES["filea"]["name"]));   }?>                                       
Your Name: Your Email Address: Attach File:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值