Jump to content

Emailing file attachment


Cyberspace

Recommended Posts

I want to upload a file in a form and send it by email, i have managed to send an email with information entered for a form but i want to be able to upload a file and send it aswell, can anyone help me do this or give me some advice.

 

The code i have that sends out a basic email is:

 

<?php 
$error_stat = 0; 
$name_message = '';
$email_message = '';
$enquiry_message = '';

if (isset($_POST['submit'])) { 

$name = $_POST['name']; 
$email = $_POST['email']; 
$enquiry = $_POST['enquiry']; 


//Error checking 




//Username check) 
if (empty($name)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$name_message = '*Please enter a name*';
} 

//Email check) 
if (empty($email)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter an email address
$email_message = '*Please enter your email address*';
}

//Check format of email address entered
else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)){
$error_stat = 1; 		 
//Set the message to tell the user to enter a valid email address
$email_message = '*Invalid Email Address*';
}

// Job Description check)  
if (!$enquiry) { 
//Set the error_stat to 1, which means that an error has occurred 
    $error_stat = 1; 

//Set the message to tell the user to enter a username 
    $enquiry_message = '*Please enter an enquiry*'; 
} 


//Then, only run the query if there were no errors (if $error_stat still equals 0) 
if ($error_stat == 0) { 

    echo "<h3>Enquiry was Successful</h3>"; 
   echo "<p>Thankyou</p>"; 
  	echo "<a href=\"index.php\">Back to main page</a>";
  	
   

require_once('class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = 'ssl://smtp.gmail.com'; // SMTP servers
$mail->FromName = '######';
$mail->AddAddress('#######@#######.co.uk');
$mail->Subject = '########';
$mail->Body = "Enquiry from:\n\nname: $name\nEmail: $email\nenquiry: $enquiry\n\n";  
$mail->Send();
}
}

//Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
if (!isset($_POST['submit']) || $error_stat == 1) { 


?> 

 

 

I would really appreciate some help with this.

 

Thanks in advance

 

 

Link to comment
https://forums.phpfreaks.com/topic/100201-emailing-file-attachment/
Share on other sites

No, i like making my own classes, for the eduction and knowing exactly what's happening to my data. I'm also fairly familiar with methods used to attack scripts such as these, so I'm fairly confident in my ability to prevent them.

 

I know several people who use this class though, and it works well.

Try using a pre-made mail class to make your life a little easier

 

http://phpmailer.codeworxtech.com/

 

Hey i used the phpmailer site and tried a quick example and managed to get the email sending an attachment, it sends the file "contact.rar", im now wondering how i can add a file using a form to input the file to be sent.

 

<?php 
$error_stat = 0; 
$name_message = '';
$email_message = '';
$enquiry_message = '';

if (isset($_POST['submit'])) { 

$name = $_POST['name']; 
$email = $_POST['email']; 
$enquiry = $_POST['enquiry']; 


//Error checking 




//Username check) 
if (empty($name)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$name_message = '*Please enter a name*';
} 

//Email check) 
if (empty($email)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter an email address
$email_message = '*Please enter your email address*';
}

//Check format of email address entered
else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)){
$error_stat = 1; 		 
//Set the message to tell the user to enter a valid email address
$email_message = '*Invalid Email Address*';
}

// Job Description check)  
if (!$enquiry) { 
//Set the error_stat to 1, which means that an error has occurred 
   $error_stat = 1; 

//Set the message to tell the user to enter a username 
   $enquiry_message = '*Please enter an enquiry*'; 
} 





//Then, only run the query if there were no errors (if $error_stat still equals 0) 
if ($error_stat == 0) { 

   echo "<h3>Enquiry was Successful</h3>"; 
  echo "<p>Thankyou</p>"; 
 	echo "<a href=\"index.php\">Back to main page</a>";
 	
  

require_once('class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = 'ssl://smtp.gmail.com'; // SMTP servers
$mail->FromName = '#####.com';
$mail->AddAddress('#######@yahoo.co.uk');
$mail->Subject = '######';
$mail->AddAttachment("c:/contact.rar", "contact.rar");  // optional name
$mail->Body = "Enquiry from:\n\nname: $name\nEmail: $email\nenquiry: $enquiry\n\n";  
$mail->Send();
}
}

//Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
if (!isset($_POST['submit']) || $error_stat == 1) { 


?> 

Simply combine the logic. Include a file field, check your enctype attrib in your form is correct, then when the class asks for the location of a file to attach (AddAttachment method, i believe), give it your $_FILES['userfile']['tmp_name'] variable :)

Simply combine the logic. Include a file field, check your enctype attrib in your form is correct, then when the class asks for the location of a file to attach (AddAttachment method, i believe), give it your $_FILES['userfile']['tmp_name'] variable :)

 

Is this complicated to do, im new to php, still learning  :( would really appreciate any help with the code on how to do this

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.