天天看点

使用phpmailer发送email

http://phpmailer.codeworxtech.com/

这里下载phpmailer

<html>

<head>

<title>使用PHPMailer发邮件</title>

</head>

<body>

<?php

 if (!array_key_exists('Submitted',$_POST))

 {

?>

    <form method="post" action="mail2.php">

    <input type="hidden" name="Submitted" value="true"/><br/>

    邮件服务器: <input type="text" name="Host" size="25"/><br/>

    如果要求验证:<br/>

    用户名: <input type="text" name="Username" size="25"/><br/>

    密    码: <input type="password" name="Password" size="10"/>

    <hr/>

    发给: <input type="text" name="To" size="25"/><br/>

    来自 Email: <input type="text" name="From" size="25"/><br/>

    来自 姓名: <input type="text" name="FromName" size="25"/><br/>

    主题: <input type="text" name="Subject" size="25"/><br/>

    <textarea name="Message" cols="50" rows="10"></textarea><br/>

    使用HTML: <input type="checkbox" name="HTML"/>

    <input type="submit" value="发送Email"/>

    </form>

 }

 else

    require("class.phpmailer.php");

    $To = $_POST['To'];

    $From = $_POST['From'];

    $FromName = $_POST['FromName'];

    $Subject = $_POST['Subject'];

    $Message = $_POST['Message'];

    $Host = $_POST['Host'];

    if (array_key_exists('HTML',$_POST))

    {

     $HTML = true;//HTML格式

    }

    else

     $HTML = false;//纯文本

    $Mail = new PHPMailer();

    $Mail->IsSMTP(); // 通过SMTP发送

    $Mail->Host = $Host; //SMTP服务器

    if (array_key_exists('Username',$_POST))

     $Mail->SMTPAuth=true;

     $Mail->Username=$_POST['Username'];

     $Mail->Password=$_POST['Password'];//实际中这里肯定需要做安全处理     

     $Mail->SMTPAuth=false;

    } 

    $Mail->From = $From;

    $Mail->FromName = $FromName;

    $Mail->AddAddress($To);

    $Mail->AddReplyTo($From);

    $Mail->WordWrap = 50; //设置自动换行

    $Mail->IsHTML($HTML);

    $Mail->Subject    = $Subject;

    $Mail->Body = $Message;

    $Mail->CharSet = "gbk";//中文字符集

    if($Mail->Send())

     echo "消息已发送";

        echo "消息未发送<br/>";

        echo "Mailer错误: " . $Mail->ErrorInfo;

</body>

</html>

  本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/140404,如需转载请自行联系原作者

继续阅读