15 PHPmailer使用

这个例子也是从官方网站上面找来的,大家有空可以看看官网的例子
http://phpmailer.worxware.com/index.php?pg=examples

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
 
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
 
$mail->IsSMTP(); // telling the class to use SMTP
 
try {
  $mail->Host       = "mail.yourdomain.com"; // SMTP server
  $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}

另外最近在公司用mail函数发送邮件时,配置相关参数,不让其乱码.

//php的mail函数发邮件时,配置相关参数,不让其乱码
$headers       = 'MIME-Version: 1.0' . "\r\n";
$headers     .= 'Content-type: text/html; charset=utf-8' . "\r\n";
// Additional headers
//$headers     .= 'To: sms-grp <test@test.com>' . "\r\n";
$headers     .= 'From: sms-grp <test@test.com>' . "\r\n";
$headers     .= 'Reply-To: sms-grp <test@test.com>' . "\r\n"; 
$subject     =     "=?UTF-8?B?".base64_encode('你好')."?=";
$message    =    "你好,<br />今晚有空吗?";

另外一种,改装版

function sendEmail($subject,$content,$toEmailArr,$attachArr='',$isHtml=true,$from='test@qq.com',$fromName='testName',$smtpUserName="",$smtpPass="")
{
        //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
        $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
        $mail->IsSMTP(); // telling the class to use SMTP
 
        try
        {
            $mail->SMTPDebug  =     2;                             // enables SMTP debug information (for testing)
            $mail->SMTPAuth   =     true;                          // enable SMTP authentication
            $mail->Host       =     "smtp.qq.com";                 // sets the SMTP server
            $mail->Port       =     25;                            // set the SMTP port for the GMAIL server
            $mail->Username   =     $smtpUserName;                 // SMTP account username
            $mail->Password   =     $smtpPass;                    // SMTP account password
            $mail->Subject       =     $subject;
 
            $mail->AddReplyTo    ($from, $fromName);
            $mail->SetFrom        ($from, $fromName);
            if($isHtml==true)
            {
                $mail->MsgHTML($content);    
            }
            else
            {
                $mail->Body  =         $content;
            }
 
            if(!empty($toEmailArr) && is_array($toEmailArr))
            {
                foreach ($toEmailArr as $toEmail)
                {
                    $mail->AddAddress    ($toEmail,'');
                }
            }
            if(!empty($attachArr) && is_array($attachArr))
            {
                foreach ($attachArr as $attach)
                {
                    $mail->AddAttachment($attach);
                }
            }
            $mail->Send();
            return true;
        }
        catch (phpmailerException $e)
        {
            echo $e->errorMessage(); //Pretty error messages from PHPMailer
        }
        catch (Exception $e)
        {
            echo $e->getMessage(); //Boring error messages from anything else!
        }
        return false;
}

Related posts:

Leave a Reply

Your email address will not be published.