如何使用php发送设计的html电子邮件

时间:2017-03-02 17:00:05

标签: php html

每当我尝试将其作为电子邮件发送时,它会将其作为字符串发送,而不是作为带有设计的HTML文件发送,如何将其作为HTML?

        $mail = "
<div class="dsng" >
testing 
</div>

"

            ;
        @mail($email, "Welcome", $mail);

结果:

 "<div class="dsng" >testing </div>" 

我想要的是什么:

testing

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php

$to = 'test@email.com';

$subject = 'Your Subject';

$from = 'peterparker@email.com';



// To send HTML mail, the Content-type header must be set

$headers  = 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";



// Create email headers

$headers .= 'From: '.$from."\r\n".

    'Reply-To: '.$from."\r\n" .

    'X-Mailer: PHP/' . phpversion();



// Compose a simple HTML email message

$message = '<html><body>';

$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';

$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';

$message .= '</body></html>';



// Sending email

if(mail($to, $subject, $message, $headers)){

    echo 'Your mail has been sent successfully.';

} else{

    echo 'Unable to send email. Please try again.';

}

?>

答案 1 :(得分:0)

我会简化我的生活并使用phpmailer

https://github.com/PHPMailer/PHPMailer

这是我实际发送电子邮件的课程,请注意要添加到您信息中的地方。

Class Notification{

private $Mail;

public function __construct(){
    global $Mail;
    $this->Mail = $Mail; 
} 

/**
 * [sendNotification description]
 * @param  string $email     
 * @param  string $name      
 * @param  string $recipient 
 * @param  string $subject   
 * @param  string $body      
 * @return [type]          
 */
public function sendNotification($email = '', $name = '', $recipient = '', $subject = '', $body = '') {

    if($email != '')
    {
        $email = htmlentities($email, ENT_QUOTES);
        $options    = '';                                               //for sanitizing email address

        if(filter_var($email, FILTER_SANITIZE_EMAIL, $options) !== FALSE
                && filter_var($email, FILTER_VALIDATE_EMAIL) !== False 
                && preg_match('/@.+\./', $email))
        {



                //$Mail->SMTPDebug = 3;                                 // Enable verbose debug output

                $this->Mail->isSMTP();                                          // Set Mailer to use SMTP
                $this->Mail->Host = 'your mail server';                         // Specify main and backup SMTP servers
                $this->Mail->SMTPAuth = true;                                   // Enable SMTP authentication
                $this->Mail->Username = 'your username, usually email';               // SMTP username
                $this->Mail->Password = 'your password';                    // SMTP password
                $this->Mail->SMTPSecure = 'ssl';                                // Enable TLS encryption, `ssl` also accepted
                $this->Mail->Port = 465;                                        // TCP port to connect to

                $this->Mail->From = $email;
                $this->Mail->FromName = $name;
                $this->Mail->addAddress($recipient, 'Your Domain');         // Recipient
                //$Mail->addAddress('ellen@example.com');               // Name is optional
                $this->Mail->addReplyTo($email, $name);
                //$Mail->addCC('cc@example.com');
                //$Mail->addBCC('bcc@example.com');

                //$Mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
                //$Mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
                $this->Mail->isHTML(true);                                      // Set email format to HTML

                $this->Mail->Subject = $subject;
                $this->Mail->Body    = $body;
                $this->Mail->AltBody = $body;

                if(!$this->Mail->send()) {
                    return False;
                } else {
                    return true;
                }

        }
        else
        {
            return False;
        }
    }
    else
    {
        return False;
    }

    return False;
}
}