PhpMailer – Sending a File Uploaded with a Form as an Attachment to an Email

PhpMailer bu işlemler için oluşturulmuş oldukça kolay bir classtır.

PhpMailer is a very simple class created for these operations.

Hello everyone, friends.

It's been a long time. I haven't been able to write or post anything on my page. For a long time, I've been extremely busy and unmotivated. Besides the stress at work, I was also busy with my PHP course and the website I was building, leaving me with no time. Now that my course is finished and I have more time, I'll continue writing.

My first course was in HTML and CSS. I received my certification and then went into design. But I have to say, I enjoy web programming more. My recently completed course is my PHP & MySQL course. I just emailed my project assignment. While I have this opportunity, I'm just starting my journey into PHP and web programming, and I thought that writing about the discoveries I've made and the challenges I've encountered along the way would be both a resource for me to reference later and a guide for others. Today, I've created my IT category.

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 'PHPMailerAutoload.php';
 //You write the files according to where you put them.
$mail = new PHPMailer; $messagetext=" ";
 //Normally this variable did not exist, but I preferred to transfer the data coming with the form to this variable.
$mail->setLanguage('tr', '/directory/');
 //Ensures that error messages are in Turkish.
 $mail->CharSet='SET NAMES UTF8';
 // We solve the possible Turkish character problem by assigning the character set of the mail to utf-8.
$mail->isSMTP();
 // We select the settings as SMTP.
 $mail->Host = 'smtp1.example.com;smtp2.example.com';
 // We write SMTP server information.
 $mail->SMTPAuth = true;
 // We enable SMTP authentication. To disable it, we need to change true to false.
 $mail->Username = 'jswan';
 // SMTP username
 $mail->Password = 'secret';
 // SMTP password
 $mail->SMTPSecure = 'tls';
 // TLS encryption is already enabled by standard. SSL is also accepted.
$mail->From = 'from@example.com';
 //We write which e-mail address we will send from.
 $mail->FromName = 'Mailer';
 // We write the sender name.
 $mail->addAddress('josh@example.net', 'Josh Adams');
 // We write the name to send the email to. The name is completely optional.
 $mail->addAddress('ellen@example.com'); $mail->addReplyTo('info@example.com', 'Information');
 //We use this to determine where the email will be sent when you click Reply.
 $mail->addCC('cc@example.com');
 // We use this to add an email address to CC.
 $mail->addBCC('bcc@example.com'); $mail->WordWrap = 50;
 // Word Wrap should contain how many characters. I didn't know either, you should ask Uncle Google.
 $mail->addAttachment('/var/tmp/file.tar.gz');
 // We need to write where the file to be added is. If you moved the file in $_FILES to another location, write that location. I directly wrote $_FILES['tmp_name'] without moving it. //This way, it is deleted from the server when the process is completed. But if you do this, use the name given below. // Because it will send it with a bin extension otherwise.
 $mail->addAttachment('/tmp/image.jpg', 'new.jpg');
 // Here you have the option to rename the uploaded file. Optional.
 $mail->isHTML(true);
 // We approve sending the email in html format.
$mail->Subject = 'Here';
 // Title of the mail.
 $mail-&gt;Body = &#039; body <b>in bold!</b>';
 // Mail content. I put my $mesajmetni variable here directly.
 $mail->AltBody = 'body';
 // Here we write what should be displayed if the receiving side does not support html display.
if(!$mail->send()) { echo 'Message could not be sent.';
 // Here, it checks whether the email sending was unsuccessful. If it is unsuccessful, the screen displays what will be written.
 echo 'Mailer Error: ' . $mail->ErrorInfo;
 // Here it prints the error information returned by the class.
 exit;
 // It does exit, but I recommend you delete it. Because any code after this point will not work.
 } echo 'Message has been sent';
 // If you don't delete Exit, this will naturally work when the above doesn't work. //But I thought it would be 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.

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
ali

Mr. Erhan
Can you reach me?

ali

Thank you for your information, good work.