Sending email with attachments in PHP, only show characters no file attached
I have this problem in my web page, I need开发者_StackOverflow社区 to send a file via email to someone, the problem that I have is that the mail arrives without an attachment and a lot of strange characters, I leave you my code:
$boundary='Datos Adjuntos';
$boundary.=" ".md5(time());
//Cabeceras del email
$headers ="From: Example <name@example.com>\r\n";
$headers .= "Reply-To: <name@example.com>\r\n";
// $headers .="MIME-Version: 1.0\r\n";
$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\n";
$body="--". $boundary ."\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n\n";
$archivo=file_get_contents($dir."/".$handle->file_dst_name);
$archivo=chunk_split(base64_encode($archivo));
//Escritura del archivo adjunto
$body .= $body .$ContenidoString. "--" .$boundary. "\n";
//Content-Type: application/msword; name=\"nombre_archivo\"\r\n
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$handle->file_dst_name."\"\r\n\n$archivo";
$body = $body . "--" . $boundary ."--";
Correo::Enviar("OPERACION","name@example.com", $body,$headers);
The $ContentString
is the html of the email, I use the upload class to upload the file to the server and then send it, I leave you a piece of the email that I receive:
This is after all the other things lik e## e name and ## e content of the email.
--Datos Adjuntos 1c436ca78c5925e7096267f0eae3a7d3 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="9cbdf187_3251_42e5_aeaa_84df343a227d_4.pdf" JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxMTA4MTYxNTEyNDIt MDUnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjMuMTY4NC1nIFwod3d3LnBk
While you would be better to use a pre-built library as people are saying, if for some reason you can't/don't want to, you can try this:
EDITED!!
// Make a MIME boundary
$boundary = 'Datos_Adjuntos_'.md5(time());
// Message headers
$headers = "From: Example <name@example.com>\r\n";
$headers .= "Reply-To: <name@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n";
// For email clients that don't understand MIME messages
$body = "This is a multi-part message in MIME format. If you can read this, something went wrong or your mail client doesn't understand MIME messages.";
// HTML content
$body .= "\r\n--$boundary\r\n";
$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n\r\n";
$body .= $ContenidoString;
// Attachment
$body .= "\r\n--$boundary\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: application/msword\r\n";
$body .= "Content-Disposition: attachment; filename=\"$handle->file_dst_name\"\r\n\r\n";
$body .= chunk_split(base64_encode(file_get_contents("$dir/$handle->file_dst_name")));
// Finish the MIME message
$body .= "\r\n--$boundary--";
// (Presumably) send the email
Correo::Enviar("OPERACION","name@example.com",$body,$headers);
My suggestion would be to use SwiftMailer http://swiftmailer.org/ This php package will save you a lot of time, and make your life really easier for sending mails. Here is how your code would look like :
require_once 'lib/swift_required.php';
$message = Swift_Message::newInstance()
->setSubject('Your subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself')
->addPart('<q>Here is the message itself</q>', 'text/html')
->attach(Swift_Attachment::fromPath('my-document.pdf'))
;
And you would be done ! Give it a try, I use it all the time
精彩评论