English 中文(简体)
发送电子邮件至 pdf附文
原标题:send email with pdf attachment
  • 时间:2011-07-20 10:58:04
  •  标签:
  • php
  • email
  • pdf

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:

www.un.org/Depts/DGACM/index_chinese.htm 员工和管理当局协调会服务器反应:C:AppServwwwPhp Project1CV-Generator estemail2中的503个未预见指挥或指挥顺序。 php on line 55

谁能告诉我什么错误?

我的守则是:

<?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);

?>
问题回答

attachment头指 t! 他们只应当宣布监察和监察部负责人:

// 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);

请注意,您的负责人应连续2次终止雇用合同,员工和管理当局协调会使用空白线作为负责人与本机构之间的保管人。

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

电子邮件:

require_once( ../lib/swiftMailer/lib/swift_required.php );

...


$body="Dear $fname,

Please find attached, an invoice for the period $startDate - $endDate

Best regards,

Mr X";

$message = Swift_Message::newInstance( Subject goes here )
    ->setFrom(array($email => "no-reply@mydomain.com"))
    ->setTo(array($email => "$fname $lname"))
    ->setBody($body);
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));

$result = $mailer->send($message);

我建议你使用PHP 邮件发送来自你的PHP的电子邮件。 我在很多不同的组合中都非常成功地使用了这一方法。 该组拥有处理编码、附件、定制头盔、发送邮件等所有必要方法。





相关问题
Angle brackets in php

I want to store angle brackets in a string in PHP because i want to eventually use mail() to send an HTML email out. The following is the code that doesn t seem to work. while(...) { $msg .= "<...

authlogic auto_register feature using my options

I have auto registration working with authlogic using gaizka s version of authlogic_openid which I found on Github since pelle s original addition of the feature seemed to cause issues. http://...

Zend 邮件问题,涉及外国char子+ com子

泽斯德邮局在名称被定为具有外国性质(如“保”)和 com(”)的物品时,就放弃了一种例外(因为邮局(邮局)退回假)。 重新提出以下守则。

How to track an email in Java?

How I can track an email? I m using java on the server side for sending emails. I want to track whether it is delivered , opened, etc... How I can do that ?

Web Link in a mail is not rendering as link in yahoo

string from = "abc@gmail.com"; string to = "xyz@gmail.com,xyz@yahoo.co.in"; string password="abcxyz"; MailMessage mail = new System.Net.Mail.MailMessage(); mail.To.Add(to); mail.From = new ...

SharePoint - Approaching Website Storage Limit Email

How can i go about changing the distribution list as well as the email text for the email that goes out to site collection admin when a site collection approaches it s size limit? Thanks for your ...

How to create an email mailing list

Im creating a coming soon page for a website im developing, and im adding an option for the user to enter their email address so we can email them when the site is up. How do I do this?

CCNet email does not include MSBuild results

We re using CCNet 1.4.4.83 but when an MSBuild task fails, we don t get the MSBuild results (i.e. missing file or whatever reason the compile failed) in the email notification. I do see the build ...

热门标签