使用PHPMailer发送HTML格式文本

时间:2017-12-21 14:19:30

标签: php html

我想使用PHPMailer发送带有HTML文本的电子邮件。

 <!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Robots.txt</title>
</head>
<body>
<?php
require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/OAuth.php";
require "PHPMailer/src/SMTP.php";
require "PHPMailer/src/POP3.php";
require "PHPMailer/src/Exception.php";
require 'PHPMailer/src/PHPMailerAutoload.php';

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception;

$current_list = array("lorelle","kadirgamar","arrow","orion","woolimlanka","nippon","luna-tower","thotalagala","altair","arrowlibrary","ashraf","ashrafnew","invoke","lavana","leia","lorelle","lioc","liocdev","lorellenew","openmanthri","opentapes","rates","rsq","signature","site","visualretale","srhr","smitratest","smitra","lioccontent","lioccopy","liocold","global","echelon","arrowlive");

$url = 'http://www.test.oddly.co/robots.txt';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
$get_list = curl_exec($curl);
curl_close($curl);
echo $get_list.'<br><br>';
$updated_list = explode('Disallow: /',$get_list);
unset($updated_list[0]);
$updated_list = array_values($updated_list);
array_walk($updated_list,'trim_array_element');
$m = "";
foreach ($current_list as  $value) {
    if(in_array($value, $updated_list))
    {
        $var = true;
        //$value." is okay";
        //echo '<br>';
    }
    else
    {   
        $var = false;
        //echo $value." Not There";
        //echo '<br>';
        break;
    }
}
define('GUSER', 'tech@oddly.co'); // GMail username
define('GPWD', 'Oddly@2016');
define('SMTPUSER', 'tech@oddly.co'); // sec. smtp username
define('SMTPPWD', 'Oddly@2016'); // sec. password
define('SMTPSERVER', 'smtp.gmail.com'); // sec. smtp server

if($var==true)
{
    $headers = 'Content-type: text/html; charset=iso-8859-1' . '\r\n'; 
    $message = '<html><body>';
    $message .= '<p>';
    $message .= '<?php 
                    foreach ($current_list as  $value) {
                        echo "<span style="color:green">". $value. " &#x2714 "."</span>";
                        echo "<br>"; 
                    }
                ?>';
    $message .= '</p>';
    $message .= '</body></html>';
    $subj = 'Robot.txt is OKAY!';
    $to2 = 'falak@oddly.co';
    $from = 'tech@oddly.co';
    $name = 'ODDLY TECH';

}   
else
{
    $msg = $value.' is not in the list. Check that!';
    $subj = 'Robot.txt is not OKAY!';
    $to2 = 'falak@oddly.co';
    $from = 'tech@oddly.co';
    $name = 'ODDLY TECH';

}

//send mail

if (smtpmailer($to2, $from, $name, $subj, $message,$headers)
        ) {
        echo 'Message send via Gmail';
    } else {
        if (!smtpmailer($to2, $from, $name, $subj, $msg)) {
            if (!empty($error)) echo $error;
        } else {
            echo 'Yep, the message is send (after doing some hard work)';
        }
    }

// This function is for Trim the array!

function trim_array_element(&$list)
{
    $list = preg_replace('/\// ', ' ', $list);
    $list = ereg_replace('[[:space:]])+', ' ',  trim($list));
}

//send email function

function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) { 
    global $error;
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true; 
    if ($is_gmail) {
        $mail->SMTPSecure = 'ssl'; 
        $mail->Host = 'ssl://smtp.gmail.com';
        $mail->Port = 465;  
        $mail->Username = GUSER;  
        $mail->Password = GPWD;   
    } 
    else {
        $mail->Host = SMTPSERVER;
        $mail->Username = SMTPUSER;  
        $mail->Password = SMTPPWD;
    }        
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo;
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
}
?>

</body>
</html>

我试过这段代码。但它显示错误。我想通过电子邮件发送HTML文本。但它并没有得到真正的HTML文本。我输入了$message = something。它是打印那个东西而不是HTML文本。它应该发送HTML文本文件,但它不发送。

1 个答案:

答案 0 :(得分:0)

我相信您错误地使用$ headers参数,因为您的函数具有不同的签名

if (smtpmailer($to2, $from, $name, $subj, $message,$headers)

function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) {