发送电子邮件与pdf附件 [英] send email with pdf attachment

查看:170
本文介绍了发送电子邮件与pdf附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP附件的php中的 mail()函数发送电子邮件。
我在本地机器上运行脚本。我在 php.ini 中设置了smtp ip。
我可以发送完整的文本电子邮件,但附件我收到以下错误:



警告:mail()[功能。邮件]:SMTP服务器响应:503命令行C /

任何人都可以告诉我有什么问题吗?



这是我的代码:

 <?php 
//下载fpdf类(http://fpdf.org)
require('./ pdf / fpdf.php' );

// fpdf object
$ pdf = new FPDF();

//生成一个简单的PDF(更多信息,请参见http://fpdf.org/en/tutorial/)
$ pdf-> AddPage();
$ pdf-> SetFont(Arial,B,14);
$ pdf->单元格(40,10,这是一个pdf示例);

//电子邮件(更改以下数据)
$ to = $ _GET ['send'];
$ from =info@asaltechd.com;
$ subject =发送电子邮件与pdf附件;
$ message =< p>请参阅附件。< / p>;

//发送混合内容需要一个随机哈希
$ separator = md5(time());

//回车类型(我们使用PHP的行尾常量)
$ eol = PHP_EOL;

//附件名称
$ filename =example.pdf;

//编码数据(以正确的格式放置附件)
$ pdfdoc = $ pdf->输出(,S);
$ attachment = chunk_split(base64_encode($ pdfdoc));


// main header(multipart mandatory)
$ headers =From:。$ from。$ eol;
$ headers。=MIME-Version:1.0$ eol;
$ headers。=Content-Type:multipart / mixed; boundary = \。$ separator。\$ eol。$ eol;
$ headers。=Content-Transfer-Encoding:7bit$ eol;
$ headers。=这是一个MIME编码的消息。$ eol。$ eol;

// message
$ headers。= - 。$ separator。$ eol;
$ headers。=Content-Type:text / html; charset = \iso-8859-1\$ eol;
$ headers。=Content-Transfer-Encoding:8bit$ eol。$ eol;
$ headers。= $ message。$ eol。$ eol;

//附件
$ headers。= - 。$ separator。$ eol;
$ headers。=Content-Type:application / octet-stream; name = \$ filename。\$ eol;
$ headers。=Content-Transfer-Encoding:base64$ eol;
$ headers。=Content-Disposition:attachment$ eol。$ eol;
$ headers。= $ attachment。$ eol。$ eol;
$ headers。= - 。$ separator。 - ;

//发送消息
mail($ to,$ subject,,$ headers);

?>


解决方案

附件不在标题中!他们应该只声明MIME标头:

  // main header(multipart mandatory)
$ headers =From: $从$ EOL。;
$ headers。=MIME-Version:1.0$ eol;
$ headers。=Content-Type:multipart / mixed; boundary = \。$ separator。\$ eol; // see below
$ headers。=Content-Transfer-Encoding:7bit$ eol;


// message
$ msg = - 。$ separator。$ eol;
$ msg。=Content-Type:text / html; charset = \iso-8859-1 \$ eol;
$ msg。=Content-Transfer-Encoding:8bit$ eol。$ eol;
$ msg。= $ message。$ eol。$ eol;

//附件
$ msg。= - 。$ separator。$ eol;
$ msg。=Content-Type:application / octet-stream; name = \$ filename。\$ eol;
$ msg。=Content-Transfer-Encoding:base64$ eol;
$ msg。=Content-Disposition:attachment$ eol;
$ msg。= $ attachment。$ eol;
$ msg。= - 。$ separator。 - 。$ eol;

//发送消息
mail($ to,$ subject,$ msg,$ headers);

另请注意,您不应该在标题内连续2个行终止 - SMTP使用空行作为标题与身体之间的分隔符。



此外,EOL不应该是您的操作系统的默认值 - 它应该是SMTP定义的EOL序列 - 即CR + LF


I'm trying to send an email using the mail() function in php with a pdf attachment. I'm running the script on localmachine. I set up the smtp ip in php.ini. I can send a text email perfectly but with an attachment I get the following error:

Warning: mail() [function.mail]: SMTP server response: 503 Unexpected command or sequence of commands in C:\AppServ\www\PhpProject1\CV-Generator\testemail2.php on line 55

Can anyone tell me what's wrong please?

Here is my code:

<?php
// download fpdf class (http://fpdf.org)
require('./pdf/fpdf.php');

// fpdf object
$pdf = new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");

// email stuff (change data below)
$to = $_GET['send']; 
$from = "info@asaltechd.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "example.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));


// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
mail($to, $subject, "", $headers);

?>

解决方案

The attachment doesn't go in the headers! They should only declare the MIME headers:

// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; // see below 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;


// message
$msg = "--".$separator.$eol;
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $message.$eol.$eol;

// attachment
$msg .= "--".$separator.$eol;
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment".$eol;
$msg .= $attachment.$eol;
$msg .= "--".$separator."--".$eol;

// send message
mail($to, $subject, $msg, $headers);

Note also that you should NEVER have 2 consecutive line terminations within the headers - SMTP uses a blank line as the seperator between headers and the body.

Also, the EOL should NOT be the default on your operating system - it should be the EOL sequence as defined by SMTP - i.e. CR+LF

这篇关于发送电子邮件与pdf附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆