我在我的网页上有这个问题,我需要通过电子邮件发送一个文件给某人,问题是我的邮件没有附件和很多奇怪的字符,我给你留下我的代码:
$boundary='Datos Adjuntos';
$boundary.=" ".md5(time());
//Cabeceras del email
$headers ="From: Example <name@example.com>rn";
$headers .= "Reply-To: <name@example.com>rn";
// $headers .="MIME-Version: 1.0rn";
$headers .="Content-Type: multipart/mixed; boundary="$boundary"rnn";
$body="--". $boundary ."n";
$body .= "Content-Type: text/html; charset=ISO-8859-1rnn";
$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"rn
$body .= "Content-Transfer-Encoding: base64rn";
$body .= "Content-Disposition: attachment; filename="".$handle->file_dst_name.""rnn$archivo";
$body = $body . "--" . $boundary ."--";
Correo::Enviar("OPERACION","name@example.com", $body,$headers);
$ContentString
是邮件的html,我使用upload类将文件上传到服务器然后发送,我给你留下了我收到的邮件的一段:这是在所有其他东西之后,比如电子邮件的名称和内容。
—Datos Adjuntos 1c436ca78c5925e7096267f0eae3a7d3 Content-Transfer-Encoding: base64 Content-Disposition: attachment;文件名="9cbdf187_3251_42e5_aeaa_84df343a227d_4.pdf" jvberi0xljkjdp0zoekmsawig9iago8paovq3jlyxrpb25eyxrlkeq6mjaxmta4mtyxnteyndit mdunmdankqovq3jlylyxrvcihqrezzagfyccaxljmumty4nc1nifwod3d3lnbk
虽然您最好像人们所说的那样使用预构建的库,但如果由于某些原因您不能/不想这样做,您可以尝试:
编辑! !
// Make a MIME boundary
$boundary = 'Datos_Adjuntos_'.md5(time());
// Message headers
$headers = "From: Example <name@example.com>rn";
$headers .= "Reply-To: <name@example.com>rn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: multipart/mixed; boundary="$boundary"rnrn";
// 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 .= "rn--$boundaryrn";
$body .= "Content-Type: text/html; charset="ISO-8859-1"rnrn";
$body .= $ContenidoString;
// Attachment
$body .= "rn--$boundaryrn";
$body .= "Content-Transfer-Encoding: base64rn";
$body .= "Content-Type: application/mswordrn";
$body .= "Content-Disposition: attachment; filename="$handle->file_dst_name"rnrn";
$body .= chunk_split(base64_encode(file_get_contents("$dir/$handle->file_dst_name")));
// Finish the MIME message
$body .= "rn--$boundary--";
// (Presumably) send the email
Correo::Enviar("OPERACION","name@example.com",$body,$headers);
我建议使用SwiftMailerhttp://swiftmailer.org/这个php包将为您节省大量的时间,并使您的生活更容易发送邮件。下面是你的代码:
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'))
;
你就完成了!试试吧,我一直在用