php傳送郵件附帶附件

2021-06-19 15:23:45 字數 3014 閱讀 2894

<?php

define("eol", "\r\n");

//define("smtp_host", "smtp.sina.com.cn");//smtp伺服器

define("smtp_port", "25");//smtp伺服器端口

define("smtp_user", "");//smtp伺服器的使用者帳號

define("smtp_pass", "");//smtp伺服器的使用者密碼

define("log", "./mail.log");

$from = "";//smtp伺服器的使用者郵箱

$to = "";//傳送給誰 可用逗號隔開多個郵箱

$cc = "";

$bcc = "";

$attachment="";//附件目錄

$subject="每日彙總";//郵件主題 很多客戶端會有亂碼,所以轉一下碼

$smtp = new smtp(smtp_host,smtp_port,smtp_user,smtp_pass, true);//這裡面的乙個true是表示使用身份驗證,否則不使用身份驗證.

$smtp->addattachment($attachment);

$smtp->sendmail($to, $from, $subject, $body, $cc, $bcc);

class smtp

/* main function */

public function sendmail($to, $from, $subject = "", $body = "", $cc = "", $bcc = "")

$headers .= "from: $from<" . $from . ">" . eol;

$headers .= "subject: " . $subject . eol;

$headers .= "date: " . date("r") . eol;

$headers .= "x-mailer: webmail ver 1.0 (php version/" . phpversion() . ")" . eol;

$headers .= "message-id: <" . date("ymdhis", $sec) . "." . ($msec * 1000000) . "." . $from . ">" . eol;

if (count($this->attachments) > 0)

$headers .= '--' . $bndp . '--' . eol;

$this->clear_attachments();

} else

$sent = true;

foreach ($mail_to as $rcpt_to)

if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $headers, $body)) else

fclose($this->sock);

}// var_dump($mail_to);die;

$this->log_write(" send over;");

return $sent;

} public function addattachment($file, $dispo = "attachment")

} private function clear_attachments()

/* private functions */

private function smtp_send($helo, $from, $to, $header, $body = "")

#auth

if ($this->auth)

if (!$this->smtp_putcmd("", base64_encode($this->pass)))

} if (!$this->smtp_putcmd("mail", "from:<" . $from . ">"))

if (!$this->smtp_putcmd("rcpt", "to:<" . $to . ">"))

if (!$this->smtp_putcmd("data"))

if (!$this->smtp_message($header, $body))

if (!$this->smtp_eom())

if (!$this->smtp_putcmd("quit"))

return true;

} private function smtp_sockopen($address) else

} private function smtp_sockopen_relay()

$this->log_write("connected ok");

return true;

} private function smtp_sockopen_mx($address)

foreach ($mxhosts as $host)

$this->log_write("connected to mx host " . $host);

return true;

} $this->log_write("error: cannot connect to any mx hosts (" . implode(", ", $mxhosts) . ")");

return false;

} private function smtp_message($header, $body)

private function smtp_eom()

private function smtp_ok()

return true;

} private function smtp_putcmd($cmd, $arg = "")

private function strip_line_breaks($address)

public function log_write($message)

}

傳送附件郵件demo

import smtplib from email.mime.text import mimetext from email.mime.multipart import mimemultipart from email.mime.image import mimeimage from email.h...

php 通過sendcloud傳送郵件 附件功能

function send mail 配置檔案 method post 郵件傳送必須是post header content type multipart form data boundary mime boundary eol,content data context stream context...

php實現郵件傳送並帶有附件

emailclass.php 複製 如下 class cmailfile function attach file filename,downfilename,mimetype,mime filename function encode file sourcefile return encoded ...