Sending the File Uploaded with the Form by Attaching it to the Email

Note: The class may have been updated since I wrote this article. It may contain some changes.

While working on my first website, I received a request to send two images and information submitted in a form via email. I knew the standard email function, but I didn't know how to attach and send uploaded files. So, I decided I'd better look for a class for this and asked Google. I saw the PhpMailer class in the first line, downloaded it, and incorporated it into the site.

After adding the class files to the site files, it will be enough to add the following codes.

require &#039;PHPMailerAutoload.php&#039;; //You write the files according to where you put them. $mail = new PHPMailer; $mesajmetni=&#039; &#039;; //Normally, this variable didn&#039;t exist, but I preferred to pass the data received with the form to this variable. $mail-&gt;setLanguage(&#039;tr&#039;, &#039;/directory/&#039;); //Ensures that error messages are in Turkish. $mail-&gt;CharSet=&#039;SET NAMES UTF8&#039;; // We solve the possible Turkish character problem by setting the mail&#039;s character set to UTF-8. $mail-&gt;isSMTP(); // We select the settings as SMTP. $mail-&gt;Host = &#039;smtp1.example.com;smtp2.example.com&#039;; // We write the SMTP server information. $mail-&gt;SMTPAuth = true; // We enable SMTP authentication. To disable it, we must change true to false. $mail-&gt;Username = &#039;jswan&#039;; // SMTP username $mail-&gt;Password = &#039;secret&#039;; // SMTP password $mail-&gt;SMTPSecure = &#039;tls&#039;; // TLS encryption is already enabled by standard. SSL also accepts. $mail-&gt;From = &#039;from@example. com&#039;; // We write the email address we will send from. $mail-&gt;FromName = &#039;Mailer&#039;; // We write the sender name. $mail-&gt;addAddress(&#039;josh@example. net&#039;, &#039;Josh Adams&#039;); // We write the name to send the email. The name is completely optional. $mail-&gt;addAddress(&#039;ellen@example. com&#039;); $mail-&gt;addReplyTo(&#039;info@example. com&#039;, &#039;Information&#039;); //We use this to determine where the email will be sent when you click Reply. $mail-&gt;addCC(&#039;cc@example. com&#039;); //We use this to add the email address to the CC. $mail-&gt;addBCC(&#039;bcc@example. com&#039;); $mail-&gt;WordWrap = 50; //How many characters should Word Wrap be? I didn&#039;t know either, so you can ask uncle Google. $mail-&gt;addAttachment(&#039;/var/tmp/file.tar.gz&#039;); //We need to write the location of the file to be attached. If you moved the file in $_FILES //to somewhere, write that location. I directly wrote $_FILES[&#039;tmp_name&#039;] without moving it. //This way, it is deleted from the server when the process is completed. But if you do this, use the following //naming. Because it will send with a bin extension otherwise. $mail-&gt;addAttachment(&#039;/tmp/image.jpg&#039;, &#039;new.jpg&#039;); // Here, you have the option to rename the uploaded image. Optional. $mail-&gt;isHTML(true); // We approve sending the email in HTML format. $mail-&gt;Subject = &#039;Here&#039;; // The title of the email. $mail-&gt;Body = &#039;body&#039; <b>in bold!</b>&#039;; // Mail content. I put my $messagetext variable here directly. $mail-&gt;AltBody = &#039;body&#039;; // Here we write what should be displayed if the recipient doesn&#039;t support HTML images. if(!$mail-&gt;send()) { echo &#039;Message could not be sent.&#039;; // Here it checks whether the mail sending failed. If it fails, what will be written on the screen is shown. echo &#039;Mailer Error: &#039; . $mail-&gt;ErrorInfo; // Here it prints the error information returned by the class. exit; // It does exit, but I recommend removing it. Because any code after this point will not work. } echo &#039;Message has been sent&#039;; // If you don&#039;t remove exit, this will naturally work when the above doesn&#039;t work. // But I thought it would be more appropriate to print this using else.

Yes, you can easily send the uploaded file automatically by attaching it to your email. That's all I have for now. Take care.

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments