較經常使用配置以下:git
require("PHPMailer_5.2.4/class.phpmailer.php"); $mail = new phpmailer(); $mail->SMTPDebug = true; // 設置是否輸出SMTP調試信息 $mail->isSMTP(); // 使用SMTP發送 $mail->Priority = 3; // 郵件優先級, 1高,3普通,5低 $mail->CharSet = "utf-8"; // 定義郵件編碼 $mail->ContentType = "text/plain"; // 文檔類型 $mail->Host = "smtp.163.com"; // 郵件服務器地址 $mail->SMTPAuth = true; // 開啓SMTP認證 $mail->Username = 'user@example.com'; // 登錄SMTP用戶名 $mail->Password = 'secret'; // 登錄SMTP密碼 $mail->SMTPSecure = 'tls'; // SMTP加密方式,可選"tls"和"ssl" $mail->Port = 587; // 鏈接的TCP端口 $mail->Encoding = "base64"; // 郵件的編碼方式,可選:"8bit","7bit","binary","base64",和"quoted-printable" $mail->From = 'from@example.com'; // 發件人郵箱 $mail->FromName = 'Mailer'; // 發件人姓名 $mail->addAddress('joe@example.net', 'Joe User'); // 增長收件人地址,參數一爲收件人郵箱,參數二可選,爲收件人姓名 $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); // 添加回復標籤,參數同addAddress $mail->addCC('cc@example.com'); // 添加抄送,參數同addAddress $mail->addBCC('bcc@example.com'); // 添加密送,參數同addAddress $mail->addAttachment('/var/tmp/file.tar.gz'); // 增長附件,參數一爲附件路徑,必選,其餘參數可選,分別爲名稱,編碼,類型,默認爲$name = '', $encoding = 'base64', $type = 'application/octet-stream' $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; // 郵件內容,HTML(若是isHTML爲true)或text(若是isHTML爲false) $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // 當郵件不支持HTML格式顯示的文本 if($mail->IsError() || !$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }