如何在PHP中使用SMTP发送HTML电子邮件

如何在PHP中使用SMTP发送HTML电子邮件,php,html,email,smtp,Php,Html,Email,Smtp,我已经能够在PHP中使用SMTP发送电子邮件,但是当我尝试将内容类型更改为HTML时,电子邮件无法发送。这是我试图使用的代码: require_once "Mail.php"; $from = "FPM <forms@fpmofames.com>"; $from_name = "FPM"; $host = "localhost"; $username = "username"; $password = "password";

我已经能够在PHP中使用SMTP发送电子邮件,但是当我尝试将内容类型更改为HTML时,电子邮件无法发送。这是我试图使用的代码:

    require_once "Mail.php";

    $from = "FPM <forms@fpmofames.com>";
    $from_name = "FPM";

    $host = "localhost";
    $username = "username";
    $password = "password";

    $subject = "Subject";
    $message = "Message";

    $to = "<example@example.com>";
    $headers = array ('From' => $from,
        'To' => $to,
        'Subject' => $subject,
        'MIME-Version' => '1.0',
        'Content-Type' => "text/html; charset=ISO-8859-1"
    );

    $smtp = Mail::factory('smtp',
        array ('host' => $host,
            'auth' => true,
            'username' => $username,
            'password' => $password));

    $mail = $smtp->send($to, $headers, $message);
require_once“Mail.php”;
$from=“FPM”;
$from_name=“FPM”;
$host=“localhost”;
$username=“username”;
$password=“password”;
$subject=“subject”;
$message=“message”;
$to=“”;
$headers=数组('From'=>$From,
'至'=>$至,
“主题”=>$Subject,
“MIME版本”=>“1.0”,
“内容类型”=>“文本/html;字符集=ISO-8859-1”
);
$smtp=Mail::工厂('smtp',
数组('host'=>$host,
“auth”=>正确,
“用户名”=>$username,
“密码”=>$password));
$mail=$smtp->send($to、$headers、$message);

如果我从标题中去掉'Content-Type'参数,它会很好地发送消息。我不知道为什么添加它会导致问题。

问题很可能在于Mail类,但因为我们不知道您使用的是什么Mail类,所以很难回答。如果您还没有这样做,我真的会考虑使用PHPMailer:

我做了一些研究,然后编写了自己的代码,使用SMTP身份验证发送HTML格式的邮件。请看这里:

<?php 
require_once "Mail.php";
$url = $_GET['baseUrl']; // source url
$success = false;
$senderName = isset( $_GET['txtname'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_GET['txtname'] ) : "";
$senderEmail = isset( $_GET['txtemail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_GET['txtemail'] ) : "";
$msg = isset( $_GET['txtDesc'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_GET['txtDesc'] ) : "";
$body = '<table width="400" border="0">
  <tr>
    <th scope="col">Name:</th>
    <th scope="col">'.$senderName.'</th>
  </tr>
  <tr>
    <th scope="col">Company:</th>
    <td scope="col">'.$_GET['txtCompany'].'</td>
  </tr>
  <tr>
    <th scope="row">Phone:</th>
    <td>'.$_GET['txtphone'].'</td>
  </tr>
  <tr>
    <th scope="row">E-mail:</th>
    <td>'.$senderEmail.'</td>
  </tr>
  <tr>
    <th scope="row">URL:</th>
    <td>'.$url.'</td>
  </tr>
  <tr>
    <th scope="row">Massage:</th>
    <td>'.$msg.'</td>
  </tr>
</table>';

 $from = $senderName."<".$senderEmail.">";
 $to = "Contact ManagerHR<info@aequitas-infotech.com>";
 $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("<p>" . $mail->getMessage() . "</p>");
  } else {
      header('Location: '.$url); // redirect to url where from inquiry made
   //echo("<p>Message successfully sent!</p>");
  }
 ?>

您应该通过mime对象创建邮件正文。梨会在那里处理的。 例:


您可以分两步完成

步骤1:将代码放入文件中:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

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

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$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
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

使用PHP中的SMTP将HTML电子邮件发送到2或3封不同的电子邮件
工作100%

<?php

require_once "Mail.php";

$host = "ssl://smtp.gmail.com";
$username = "example@gmail.com";
$password = "password";
$port = "465";

$to  = '1-to-example@example.com' . ', '; 
$to  = '2-to-example@example.com' . ', '; 
$to  = '3-to-example@example.com' . ', '; 

$email_from = "from-example@example.com";
$email_subject = "Your Subject";

$email_body = "**<html> <body>**"; 
$email_body .= "<strong> Your HTML code </strong>";
$email_body .= "**</body></html>**";


$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => 
$email_subject, 'Reply-To' => $email_address , 'MIME-Version' => '1.0', 'Content-Type' => "text/html; charset=ISO-8859-1");

$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));

$mail = $smtp->send($to, $headers, $email_body);

?>

将内容类型写入标题的第一行,如下所示

$to = "<example@example.com>"; $headers = array ('Content-Type' => "text/html; charset=ISO-8859-1", 'From' => $from, 'To' => $to, 'Subject' => $subject, 'MIME-Version' => '1.0' ); 
$to=”“$headers=array('Content-Type'=>“text/html;charset=ISO-8859-1”,'From'=>$From',To'=>$To',Subject'=>$Subject',MIME Version'=>'1.0');

这对我有用。

这如何回答有关
内容类型的问题?此外,您应该使用
=
连接电子邮件地址。
$to = "<example@example.com>"; $headers = array ('Content-Type' => "text/html; charset=ISO-8859-1", 'From' => $from, 'To' => $to, 'Subject' => $subject, 'MIME-Version' => '1.0' );