Search
 
SCRIPT & CODE EXAMPLE
 

PHP

phpmailer send attachment

$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);
Comment

send attachment in mail php

    $filename = 'myfile';
    $path = 'your path goes here';
    $file = $path . "/" . $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 = "
";

    // 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() );
    }
Comment

php email attachment and message

$my_file = "somefile.zip";
$my_path = "/your_path/to_the_attachment/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
Comment

how to get attachments to emails php

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

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

PHP | Send Attachment With Email

if($_POST['button'] && isset($_FILES['attachment']))
{
 
    $from_email         = 'sender@abc.com'; //from mail, sender email address
    $recipient_email    = 'recipient@xyz.com'; //recipient email address
     
    //Load POST data from HTML form
    $sender_name    = $_POST["sender_name"] //sender name
    $reply_to_email = $_POST["sender_email"] //sender email, it will be used in "reply-to" header
    $subject        = $_POST["subject"] //subject for the email
    $message        = $_POST["message"] //body of the email
     
 
    /*Always remember to validate the form fields like this
    if(strlen($sender_name)<1)
    {
        die('Name is too short or empty!');
    }
    */
     
    //Get uploaded file data using $_FILES array
    $tmp_name    = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server
    $name        = $_FILES['my_file']['name'];  // get the name of the file
    $size        = $_FILES['my_file']['size'];  // get size of the file for size validation
    $type        = $_FILES['my_file']['type'];  // get type of the file
    $error       = $_FILES['my_file']['error']; // get the error (if any)
 
    //validate form field for attaching the file
    if($file_error > 0)
    {
        die('Upload error or No files uploaded');
    }
 
    //read from the uploaded file & base64_encode content
    $handle = fopen($tmp_name, "r");  // set the file handle only for reading the file
    $content = fread($handle, $size); // reading the file
    fclose($handle);                  // close upon completion
 
    $encoded_content = chunk_split(base64_encode($content));
 
    $boundary = md5("random"); // define boundary with a md5 hashed value
 
    //header
    $headers = "MIME-Version: 1.0
"; // Defining the MIME version
    $headers .= "From:".$from_email."
"; // Sender Email
    $headers .= "Reply-To: ".$reply_to_email."
"; // Email address to reach back
    $headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type
    $headers .= "boundary = $boundary
"; //Defining the Boundary
         
    //plain text
    $body = "--$boundary
";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1
";
    $body .= "Content-Transfer-Encoding: base64

";
    $body .= chunk_split(base64_encode($message));
         
    //attachment
    $body .= "--$boundary
";
    $body .="Content-Type: $type; name=".$name."
";
    $body .="Content-Disposition: attachment; filename=".$name."
";
    $body .="Content-Transfer-Encoding: base64
";
    $body .="X-Attachment-Id: ".rand(1000, 99999)."

";
    $body .= $encoded_content; // Attaching the encoded file with email
     
    $sentMailResult = mail($recipient_email, $subject, $body, $headers);
 
    if($sentMailResult )
    {
       echo "File Sent Successfully.";
       unlink($name); // delete the file after attachment sent.
    }
    else
    {
       die("Sorry but the email could not be sent.
                    Please go back and try again!");
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: laravel redis cache 
Php :: [ERROR] InvalidArgumentException: Wrong file in C:xampphtdocsmagento2.4libinternalMagentoFrameworkImageAdapterGd2.php:64 Stack trace 
Php :: php check year and month is between two dates 
Php :: transaction laravel 
Php :: php add to array 
Php :: how to use custome functions in laravel 
Php :: session start php 
Php :: laravel collection partition 
Php :: extend woocommerce user fields edit-account 
Php :: alert for empty input in php 
Php :: php list *files 
Php :: sort an array in php manually 
Php :: insert into database with seeder 
Php :: validate either one field is required in laravel 
Php :: yii1 refresh cache schema 
Php :: active page in laravel 
Php :: php extract number from string without comma 
Php :: if home else php wordpress 
Php :: find substring php 
Php :: laravel rule unique where 
Php :: how to trim text php 
Php :: database, counts,php, 
Php :: php epoch conversion 
Php :: php echo html and variable 
Php :: wordpress change slug programmatically 
Php :: download npm package 
Php :: carbon date time laravel 
Php :: if statement php 
Php :: this php 
Php :: Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini): max_execution_time = 300Fix 504 Gateway Timeout using Nginx 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =