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

如何使用php发送设计好的html电子邮件,php,html,Php,Html,每当我尝试以电子邮件的形式发送它时,它都会以字符串的形式发送,而不是以带有设计的HTML文件的形式发送,我如何将其设置为HTML $mail = " <div class="dsng" > testing </div> " ; @mail($email, "Welcome", $mail); 试试这个: <?php $to = 'test@email.com'; $subject = 'Your S

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

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

"

            ;
        @mail($email, "Welcome", $mail);
试试这个:

<?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.';

}

?>

我会简化我的生活并使用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;
}
}

要以HTML格式发送电子邮件,您需要添加适当的标题。但看起来您也在尝试应用CSS类,在这种情况下,您需要在电子邮件中包含CSS,并尽可能复制CSS
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;
}
}