简体   繁体   中英

How I can send HTML emails with attachments?

I am using this code to email:

$email="abc@gmail.com";
$to = $email;
$subject = "Test mail";
$message1 = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head></head>
  <body>
    <div  style="width:80%;">
      <table style="margin: 10px 20px 10px 20px;">
        <tr><td>Hello!'."  ".'</td></tr>';
        $message1.=$mailmessage;
        $message1.=
          '<tr><td>Please find attachments for dailyreport.</td></tr>
<tr><td><br>' . "Thanks and Regards with ". '</td></tr>
    </table></div>
  </body>
</html>';
$from = "admin@feed.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";  
if(mail($to,$subject,$message1,$headers)) {
  "mail sent";
} else {
  "email does not exist";
}

To send attachments I need to change the content type to Content-Type: multipart/mixed;\\n . But the HTML context will change. How can I attach a file to this html?

If you want to write your own script, checkout this link

But I strongly recommend you to use custom library such as PHPMailer http://code.google.com/a/apache-extras.org/p/phpmailer/

PHPMailer is a PHP email transport class featuring file attachments, SMTP servers, CCs, BCCs, HTML messages, word wrap, and more. Sends email via sendmail, PHP mail(), QMail, or directly with SMTP. Support for additional transports, such as SMS, MMS will be forthcoming

You should try this code for header information

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\""; 

您有一些用于电子邮件附件的特殊标题,请放心,我们有一个名为sendMail的php类,可以下载并学习@ PHP sendMail类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM