How to Get Email and Their Attachments from PHP

How to get email and their attachments from PHP

What MTA are you using? If you use postfix + maildrop you can create a filtering rule that pipes certain messages through a PHP script that then handles the incoming mails. (google for maildrop and xfilter).

PHP - Get multiple email attachments

Here

if($name){
echo 'Attachment:' . ' ' . $name . '</br>';
echo 'Path Saved: ' . ' ' . STYLESHEETPATH . '/email_attachments/' .$name . '</br>';
echo 'Download Link: ' . ' ' . '<a href="' . $download_link . '"> Download</a>' . '</br>';
}

You are printing just one attachment (the last that left from the foreach iteration), you need to wrap that in a foreach like

foreach ($attachments as $key => $attachment) {
$name = $attachment['name'];
$contents = $attachment['attachment'];
// Put your code to print here.
}

Php Get Email Attachment

The form you are completing needs to have enctype="multipart/form-data" and needs to have method="post" for upload to work.

like this:

<form method="post" action="" enctype="multipart/form-data" id="myform" onsubmit="return checkForm(this)";>
<input type="file" name="filename" />
// ...........
</form>

After that in your send mail you can get the file with $_FILES['filename']['tmp_name'], notice that filename has to be same as input's name:

$attachment = chunk_split(base64_encode(file_get_contents($_FILES['filename']['tmp_name'])));

for function

<script type="text/javascript">
function checkForm(form){
// process fieds
return checkNextFunction(form);
}

function checkNextFunction(form){
form.action = 'myurl.php';
return true;
}
</script>

Check JSFiddle.

Send attachments with PHP Mail()?

I agree with @MihaiIorga in the comments – use the PHPMailer script. You sound like you're rejecting it because you want the easier option. Trust me, PHPMailer is the easier option by a very large margin compared to trying to do it yourself with PHP's built-in mail() function. PHP's mail() function really isn't very good.

To use PHPMailer:

  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

Now, sending emails with attachments goes from being insanely difficult to incredibly easy:

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

It's just that one line $email->AddAttachment(); -- you couldn't ask for any easier.

If you do it with PHP's mail() function, you'll be writing stacks of code, and you'll probably have lots of really difficult to find bugs.

Extract attachments when piping mails to PHP

You'll probably need do the following:

  1. Write a PHP script that's executable at the CLI (by adding a #! declaration at the top of the script that points to the PHP binary, then settings its executable permissions).

  2. Get that script to read the raw email from php://stdin (file_get_contents is easiest)

  3. Get that script to decode the mail in to parts, using something like PEAR::Mail::Mime::Decode or I think there's a handy Zend Framework component).

  4. Read the attachment and subject from the decoded message, and store as normal

  5. exit(0) at the end to tell the CLI that it was a clean exit - any other exit() status could cause a bounced email.



Related Topics



Leave a reply



Submit