phpmailer發送郵件(附件)

之前一直都在用發送郵件 可是沒有用過發送附件 剛接到需求頭大 在網上找了半天可是仍是都是沒有附件的 如今整理一份php

設置smtp什麼的我就不說了 直接貼代碼服務器

 

common裏面的代碼this

須要use一下編碼

use PHPMailer\phpmailer;
use PHPMailer\smtp;
function sendEmail($desc_content, $toemail,  $desc_url){
    echo $desc_content;
    echo $toemail;
    $mail = new PHPMailer();
    $mail->isSMTP();// 使用SMTP服務
    $mail->CharSet = "utf8";// 編碼格式爲utf8,不設置編碼的話,中文會出現亂碼
    $mail->Host = "smtp.163.com";// 發送方的SMTP服務器地址
    $mail->SMTPAuth = true;// 是否使用身份驗證
    $mail->Username = "xxx@163.com";// 發送方的163郵箱用戶名,就是你申請163的SMTP服務使用的163郵箱</span><span style="color:#333333;">
    $mail->Password = "xxxx";// 發送方的郵箱密碼,注意用163郵箱這裏填寫的是「客戶端受權密碼」而不是郵箱的登陸密碼!</span><span style="color:#333333;">
    $mail->SMTPSecure = "ssl";// 使用ssl協議方式</span><span style="color:#333333;">
    $mail->Port = 465;// 163郵箱的ssl協議方式端口號是465/994
    $mail->setFrom("xxx@163.com","Mailer");// 設置發件人信息,如郵件格式說明中的發件人,這裏會顯示爲Mailer(xxxx@163.com),Mailer是當作名字顯示
    $mail->addAddress($toemail,'博客回覆消息');// 設置收件人信息,如郵件格式說明中的收件人,這裏會顯示爲Liang(yyyy@163.com)
    $mail->addReplyTo("xxx@163.com","Reply");// 設置回覆人信息,指的是收件人收到郵件後,若是要回復,回覆郵件將發送到的郵箱地址
    //$mail->addCC("xxx@163.com");// 設置郵件抄送人,能夠只寫地址,上述的設置也能夠只寫地址(這我的也能收到郵件)
    //$mail->addBCC("xxx@163.com");// 設置祕密抄送人(這我的也能收到郵件)
    //$mail->addAttachment("bug0.jpg");// 添加附件
    $mail->Subject = "郵件回覆!";// 郵件標題
    $mail -> AddAttachment('xx.xls','個人附件.xls'); // 添加附件,並指定名稱
    $mail -> IsHTML(true);
    $mail->Body = "如下是博客博主回覆你的內容:".$desc_content."點擊能夠查看文章地址:".$desc_url;// 郵件正文
    //$mail->AltBody = "This is the plain text純文本";// 這個是設置純文本方式顯示的正文內容,若是不支持Html方式,就會用到這個,基本無用

    if(!$mail->send()){// 發送郵件
        echo 22;
        return $mail->ErrorInfo;
        // echo "Message could not be sent.";
        // echo "Mailer Error: ".$mail->ErrorInfo;// 輸出錯誤信息
    }else{
        return 1;
    }
}

 

而後在控制器直接調用就好了 固然內容能夠修改的  看本身須要傳什麼了url

public function sendenail()
{
    $res = sendEmail("內容", "xxx@163.com", "url");
    var_dump($res);
}

 

 

郵箱發送附件和發送郵件都多了兩句話spa

$mail -> AddAttachment('xx.xls','個人附件.xls'); // 添加附件,並指定名稱ssl

$mail -> IsHTML(true);博客

 

還有一個錯好像是由於大小寫的問題 吧小寫的smtp改爲大寫的it

switch($this->Mailer) {
  case 'sendmail':
    return $this->SendmailSend($header, $body);
  case 'smtp':
    return $this->SmtpSend($header, $body);
  default:
    return $this->MailSend($header, $body);
}
相關文章
相關標籤/搜索