Ich muss ein PDF mit der Post schicken, ist das möglich?
$to = "xxx";
$subject = "Subject" ;
$message="Example message with <b>html</b>";
$headers="MIME-Version: 1.0" . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxx <xxx>' . "\r\n";
mail($to,$subject,$message,$headers);
Was vermisse ich?

DEZA
Ich stimme @MihaiIorga in den Kommentaren zu – verwende das PHPMailer-Skript. Sie klingen, als würden Sie es ablehnen, weil Sie die einfachere Option wollen. Vertrauen Sie mir, PHPMailer ist die bei weitem einfachere Option im Vergleich zu dem Versuch, es selbst mit dem integrierten PHP zu tun mail()
Funktion. PHPs mail()
Funktion ist wirklich nicht sehr gut.
So verwenden Sie PHPMailer:
- Laden Sie das PHPMailer-Skript hier herunter: http://github.com/PHPMailer/PHPMailer
- Extrahieren Sie das Archiv und kopieren Sie den Ordner des Skripts an einen geeigneten Ort in Ihrem Projekt.
- Fügen Sie die Hauptskriptdatei hinzu —
require_once('path/to/file/class.phpmailer.php');
Jetzt wird das Versenden von E-Mails mit Anhängen von wahnsinnig schwierig zu unglaublich einfach:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject="Message Subject";
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach="PATH_OF_YOUR_FILE_HERE";
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
return $email->Send();
Es ist nur diese eine Zeile $email->AddAttachment();
– einfacher geht es nicht.
Wenn du es mit PHP machst mail()
-Funktion schreiben, werden Sie haufenweise Code schreiben, und Sie werden wahrscheinlich viele wirklich schwer zu findende Fehler haben.

Pragnesh Chauhan
Du kannst es mit folgendem Code versuchen:
$filename="myfile";
$path="your path goes here";
$file = $path . "https://stackoverflow.com/" . $filename;
$mailto = 'mail@mail.com';
$subject="Subject";
$message="My message";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: name <test@test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
Bearbeiten 14. Juni 2018
zur besseren Lesbarkeit bei einigen E-Mail-Anbietern
$body .= $eol . $message . $eol . $eol;
und
$body .= $eol . $content . $eol . $eol;

Simon Möchele
Für PHP 5.5.27 Sicherheitsupdate
$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$file_name = basename($file);
// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
// message & attachment
$nmessage = "--".$uid."\r\n";
$nmessage .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$nmessage .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$nmessage .= $message."\r\n\r\n";
$nmessage .= "--".$uid."\r\n";
$nmessage .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$nmessage .= $content."\r\n\r\n";
$nmessage .= "--".$uid."--";
if (mail($mailto, $subject, $nmessage, $header)) {
return true; // Or do something here
} else {
return false;
}

Matthäus Johnson
Swiftmailer ist ein weiteres einfach zu verwendendes Skript, das automatisch davor schützt E-Mail-Injektion und macht Anhänge zum Kinderspiel. Ich rate auch dringend davon ab, das integrierte PHP zu verwenden mail()
Funktion.
Benutzen:
- Herunterladen Swiftmailerund platzieren Sie die
lib
Ordner in Ihrem Projekt
- Fügen Sie die Hauptdatei mit ein
require_once 'lib/swift_required.php';
Fügen Sie jetzt den Code hinzu, wenn Sie eine E-Mail senden müssen:
// Create the message
$message = Swift_Message::newInstance()
->setSubject('Your subject')
->setFrom(array('webmaster@mysite.com' => 'Web Master'))
->setTo(array('receiver@example.com'))
->setBody('Here is the message itself')
->attach(Swift_Attachment::fromPath('myPDF.pdf'));
//send the message
$mailer->send($message);
Weitere Informationen und Optionen finden Sie im Swiftmailer-Dokumentation.

Basith
Um eine E-Mail mit Anhang zu senden, müssen wir den multipart/mixed MIME-Typ verwenden, der angibt, dass gemischte Typen in der E-Mail enthalten sein werden. Darüber hinaus möchten wir den multipart/alternative MIME-Typ verwenden, um sowohl die Klartext- als auch die HTML-Version der E-Mail zu senden. Sehen Sie sich das Beispiel an:
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject="Test email with attachment";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--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="attachment.zip"
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";
?>
Wie Sie sehen können, ist das Senden einer E-Mail mit Anhang einfach zu bewerkstelligen. Im vorherigen Beispiel haben wir einen mehrteiligen/gemischten MIME-Typ und darin einen mehrteiligen/alternativen MIME-Typ, der zwei Versionen der E-Mail angibt. Um unserer Nachricht einen Anhang hinzuzufügen, lesen wir die Daten aus der angegebenen Datei in einen String, codieren ihn mit base64, teilen ihn in kleinere Stücke auf, um sicherzustellen, dass er den MIME-Spezifikationen entspricht, und fügen ihn dann als Anhang hinzu.
Genommen von Hier.
Das funktioniert für mich. Es hängt auch mehrere Anhänge an. leicht
<?php
if ($_POST && isset($_FILES['file'])) {
$recipient_email = "recipient@yourmail.com"; //recepient
$from_email = "info@your_domain.com"; //from email using site domain.
$subject = "Attachment email from your website!"; //email subject line
$sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
$sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['file'];
//php validation
if (strlen($sender_name) < 4) {
die('Name is too short or empty');
}
if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email');
}
if (strlen($sender_message) < 4) {
die('Too short message! Please enter something');
}
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("specialToken$4332"); // boundary token to be used
if ($file_count > 0) { //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $sender_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($sender_message));
//attachments
for ($x = 0; $x < $file_count; $x++) {
if (!empty($attachments['name'][$x])) {
if ($attachments['error'][$x] > 0) { //exit script and output error if we encounter any
$mymsg = array(
1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3 => "The uploaded file was only partially uploaded",
4 => "No file was uploaded",
6 => "Missing a temporary folder");
die($mymsg[$attachments['error'][$x]]);
}
//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];
//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
$body .= "--$boundary\r\n";
$body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
$body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
}
}
} else { //send plain email otherwise
$headers = "From:" . $from_email . "\r\n" .
"Reply-To: " . $sender_email . "\n" .
"X-Mailer: PHP/" . phpversion();
$body = $sender_message;
}
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
}
?>
Keine der obigen Antworten funktionierte für mich aufgrund ihres angegebenen Anhangsformats (application/octet-stream
). Verwenden application/pdf
für beste Ergebnisse mit PDF-Dateien.
<?php
// just edit these
$to = "email1@domain.com, email2@domain.com"; // addresses to email pdf to
$from = "sent_from@domain.com"; // address message is sent from
$subject = "Your PDF email subject"; // email subject
$body = "<p>The PDF is attached.</p>"; // email body
$pdfLocation = "./your-pdf.pdf"; // file location
$pdfName = "pdf-file.pdf"; // pdf file name recipient will get
$filetype = "application/pdf"; // type
// creates headers and mime boundary
$eol = PHP_EOL;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_$semi_rand";
$headers = "From: $from$eolMIME-Version: 1.0$eol" .
"Content-Type: multipart/mixed;$eol boundary=\"$mime_boundary\"";
// add html message body
$message = "--$mime_boundary$eol" .
"Content-Type: text/html; charset=\"iso-8859-1\"$eol" .
"Content-Transfer-Encoding: 7bit$eol$eol$body$eol";
// fetches pdf
$file = fopen($pdfLocation, 'rb');
$data = fread($file, filesize($pdfLocation));
fclose($file);
$pdf = chunk_split(base64_encode($data));
// attaches pdf to email
$message .= "--$mime_boundary$eol" .
"Content-Type: $filetype;$eol name=\"$pdfName\"$eol" .
"Content-Disposition: attachment;$eol filename=\"$pdfName\"$eol" .
"Content-Transfer-Encoding: base64$eol$eol$pdf$eol--$mime_boundary--";
// Sends the email
if(mail($to, $subject, $message, $headers)) {
echo "The email was sent.";
}
else {
echo "There was an error sending the mail.";
}
9932200cookie-checkAnhänge mit PHP Mail() versenden?yes
Zum Versenden eines Anhangs mit
mail()
Funktion ist viel schwieriger als Sie erwarten, versuchen Sie es aus Zeitgründen zu verwenden PHPMailer– Mihai Iorga
6. September 2012 um 13:38 Uhr
Oder könntest du einfach darauf verlinken?
– Benutzer849137
6. September 2012 um 13:39 Uhr
@mihai lorga Erfordert das nicht eine serverseitige Installation? Wenn es ohne Erweiterungen oder Plugins möglich ist, muss ich wissen, wie.
– Benutzer1537415
6. September 2012 um 13:39 Uhr
Schnelle Google-Suche – webcheatsheet.com/php/send_email_text_html_attachment.php
– Kennzeichen
6. September 2012 um 13:40 Uhr
@ChristianNikkanen, es ist nur ein gut eingestelltes Skript. Es hat auch viele Funktionen, die schwer zu erreichen sind. Warum das Rad neu erfinden? Es verwendet keine zusätzlichen Plugins.
– Mihai Iorga
6. September 2012 um 13:42 Uhr