如何从PHP发送电子邮件附件?

时间:2014-03-09 20:58:49

标签: php email webserver

我正在尝试使用以下代码将文本文件作为PHP脚本中的附件发送电子邮件:http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment

<?
$subject = 'Requested File';
$random_hash = md5(date('r', time()));
$headers = "From: email@email.com\r\nReply-To: email@email.com";
$headers .= "\r\nContent-Type: myltipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('path/test.txt')));

ob_start();
?>

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>   
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="test.txt"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?>

我不是PHP开发人员,使用它的经验非常有限,执行此脚本时我没有收到任何错误,但我也没有收到电子邮件...任何想法为什么?或者我应该具体看什么?

感谢您的任何提示!

编辑: 根据评论,我尝试使用SwiftMailer但我无法使用此代码使其工作:     $ message = Swift_Message :: newInstance();

// Give the message a subject
$message->setSubject('Your subject');

// Set the From address with an associative array
$message->setFrom(array('email@email.com' => 'From Name'));

// Set the To addresses with an associative array
$message->setTo(array('email@email.com', 'email@email.com' => 'Name'));

// Give it a body
$message->setBody('Here is the message itself');

// And optionally an alternative body
$message->addPart('<q>Here is the message itself</q>', 'text/html');

// Optionally add any attachments
$message->attach(Swift_Attachment::fromPath('path/test.csv'));

同样,此代码执行时没有任何错误,但没有发送电子邮件......我错过了什么?

1 个答案:

答案 0 :(得分:1)

该教程的文件附件有错误,我现在记得它,我从来没有修改过它。 (过去

以下是我自己图书馆的工作副本,欢迎您使用。

只需将test.txt的所有实例更改为您要附加的文件。

<html>
<head>
<title>Send file attachments using PHP</title>
</head>
<body>
<?php
  $to = "email@example.com";
  $subject = "This is the subject";
  $message = "This is the test message.";
  # Open a file
  $file = fopen( "test.txt", "r" );
  if( $file == false )
  {
     echo "Error in opening file";
     exit();
  }
  # Read the file into a variable
  $size = filesize("test.txt");
  $content = fread( $file, $size);

  # encode the data for safe transit
  # and insert \r\n after every 76 chars.
  $encoded_content = chunk_split( base64_encode($content));

  # Get a random 32 bit number using time() as seed.
  $num = md5( time() );

  # Define the main headers.
  $header = "From:email@example.com\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";

  # Define the message section
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";

  # Define the attachment section
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"test.txt\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"test.txt\"\r\n\n";
  $header .= "$encoded_content\r\n";
  $header .= "--$num--";

  # Send email now
  $retval = mail ( $to, $subject, "", $header );
  if( $retval == true )
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>
</body>
</html>