How to send email with attachment in PHP
I have received requests from my readers to write an article on send email with attachment, so in this tutorial I am going to show you how to send email with attachment in PHP. We have already write a tutorial on send email with SMTP and PHP Mailer. In this tutorial we didn’t go to re-invent the complete code simply add attachment and proceed.
Also Read: Send Email with SMTP and PHP Mailer
I have used SMTP email using phpmailer (A full-featured email creation and transfer class for PHP) library.
PHP Code:
<?php require_once('PHPMailer_v5.1/class.phpmailer.php'); //library added in download source. $msg = 'Hello World'; $subj = 'test mail message'; $to = '[email protected]'; $from = '[email protected]'; $name = 'My Name'; echo smtpmailer($to,$from, $name ,$subj, $msg); function smtpmailer($to, $from, $from_name = 'Example.com', $subject, $body, $is_gmail = true) { global $error; $mail = new PHPMailer(); $mail->IsSMTP(); $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; $mail->Username = '[email protected]'; $mail->Password = '*******'; $mail->IsHTML(true); $mail->From="[email protected]"; $mail->FromName="Example.com"; $mail->Sender=$from; // indicates ReturnPath header $mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers //$mail->AddCC('[email protected]', 'CC: to phpgang.com'); $mail->Subject = $subject; $mail->Body = $body; $mail->AddAddress($to); if(!$mail->Send()) { $error = 'Mail error: '.$mail->ErrorInfo; return true; } else { $error = 'Message sent!'; return false; } } ?>
This is the simple code to send email to any one without attachment no you have to just add a single line and send this email with attachment.
File attachment:
$mail->AddAttachment($path); // $path: is your file path which you want to attach like $path = "reload.png";
Just using this line of code before $mail->Send() statement it will attach that file and send it. if you want to attach multiple files then add separate attachment method for each file.
If you want more option in attachment like name, MIME and file type use it like this.
$mail->AddAttachment($path,$name,$encoding,$type); // advance parameters for attachment.
To upload a file you can use our file upload tutorial and send that file in attachment.
I hope it helps please do share a with your friends circles and give us your feedback so we can improve our upcoming tutorials.
Tutorial Categories:
Hii
Hello
i encounter a problem that sending email with attachment via smtp and phpmailer.
but could not attach any files . The message shown by submitting “Cannot assess the file”
Could you specify more in statement PHP ” $mail->AddAttachment($path,$name,$encoding,$type); ” ?
$path refer to name= attachment01 , means to link $_FILES[attachment01]
Get confused about $path and $name , how i should set in this case correctly .
i would be grateful.
Simply send $path of file like this: $mail->AddAttachment(“images/logo.png”); // $path: is your file path which you want to attach like $path = “images/logo.png”;
Is not local path. Receive a alert “cannot access the files.” when user fill out the form with attachment , automatically sent to a designated email through smtp . but doesn’t include files.