How to send an HTML email using SMTP in PHP

后端 未结 6 1615
醉梦人生
醉梦人生 2021-01-14 19:56

I\'ve been able to send an email using SMTP in PHP, but when I try to change the Content Type to HTML, the email doesn\'t get delivered. This is the code I\'m trying to use:

6条回答
  •  走了就别回头了
    2021-01-14 20:36

    I did some research, then I made my own code to send mail with HTML formatting using SMTP authentication. See here:

    
      
        Name:
        '.$senderName.'
      
      
        Company:
        '.$_GET['txtCompany'].'
      
      
        Phone:
        '.$_GET['txtphone'].'
      
      
        E-mail:
        '.$senderEmail.'
      
      
        URL:
        '.$url.'
      
      
        Massage:
        '.$msg.'
      
    ';
    
     $from = $senderName."<".$senderEmail.">";
     $to = "Contact ManagerHR";
     $subject = "Hi!";
     $host = "XXX.host.com";
     $username = "username@host.com";
     $password = "*****";
     /* MIME-Version should be "1.0rn" and Content-Type should be "text/html; charset=ISO-8859-1rn" to send an HTML Email */
    $headers = array ('MIME-Version' => '1.0rn',
            'Content-Type' => "text/html; charset=ISO-8859-1rn",
            'From' => $from,
            'To' => $to,
            'Subject' => $subject
         );
    $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'auth' => true,
         'username' => $username,
         'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
     if (PEAR::isError($mail)) {
       echo("

    " . $mail->getMessage() . "

    "); } else { header('Location: '.$url); // redirect to url where from inquiry made //echo("

    Message successfully sent!

    "); } ?>

提交回复
热议问题