Mail form with attachments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zorguz
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 9:08 am

Mail form with attachments

Post by zorguz »

Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have form and I made that it sends to my e-mail but I have big problem - how can I make that it sends attached image files? Maybe someone can really help me =] I am really just beginner (file field name is - "files" and there can be added 4 files) Here is the script:

Code: Select all

<?php

$mailto = 'my-email' ;

$subject = "Pas&#363;t&#299;jums" ;

$uself = 0;

// -------------------- Konfiguracijas beigas ---------------

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$phone = $_POST['phone'] ;
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$website = $_POST['website'] ;
$bio = $_POST['bio'] ;
$files = $_POST['files'] ;
$http_referrer = getenv( "HTTP_REFERER" );

$message =

	"Š&#299; zi&#326;a tika nos&#363;t&#299;ta no:\n" .
	"$http_referrer\n" .
	"------------------------------------------------------------\n" .
	"V&#257;rds: $name\n" .
	"Uzv&#257;rds: $uzvards\n" .
	"E-Pasts: $email\n" .
	"Kontaktt&#257;lrunis: $talrunis\n" .
	"Vecums: $age\n" .
	"------------------------- Koment&#257;ri -------------------------\n\n" .

	"\n\n------------------------------------------------------------\n" ;


if (empty($name) || empty($phone) || empty($age)) {
   echo 'L&#363;dzu ievadiet visus datus.';
}
else
if (isset($name) || isset($phone) || isset($age)) {
   echo 'Paldies. Zi&#326;ojums tika nos&#363;t&#299;ts.';
     
mail($mailto, $subject, $message,
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "Pas&#363;t&#299;jums");

exit; } 
?>

Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It's complicated. Attaching files to emails needs to be done in a specific way and even knowing the general idea will still get your emails blocked by some hosts.

http://www.swiftmailer.org/ will do it for you.
zorguz
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 9:08 am

Post by zorguz »

argh I still can't make anything :cry: I am so stupid in php...maybe someone can help me and tell how to work with that swift mailer because I don't understand... :oops:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Where does the attachment come from? Is it a file on the server or is it uploaded?
zorguz
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 9:08 am

Post by zorguz »

everyone can add image from their computer and that image with other information must be sent to my e-mail
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Well, it's just the same process as normal sending except you call addAttachment() with the uploaded file contents and details.

Code: Select all

<form action="mailer.php" enctype="multipart/form-data" method="post">
    Attach file: <input type="file" name="image" />
    <input type="submit" name="submit" value="Submit" />
</form>

Code: Select all

<?php

$swift = new Swift( .... );

if (!empty($_FILES["image"]) && !$_FILES["image"]["error"])
{
    $swift->addAttachment(
        file_get_contents($_FILES["image"]["tmp_name"]), $_FILES["image"]["name"], $_FILES["image"]["type"]);
}
else //file not uploaded

$swift->send( ... );
Here's a good place to start: http://swiftmailer.org/docs/tutorials/basics then http://swiftmailer.org/docs/tutorials/attachments

:)
zorguz
Forum Newbie
Posts: 4
Joined: Fri Dec 15, 2006 9:08 am

Post by zorguz »

okey is this right? (I can answer for you to this question - NO! :roll: ) and do I need to change something in swift.php or smtp.php or something else...??? and why I have this error lol I am really noob and just learning so please don't be angry...
Fatal error: Call to a member function start() on a non-object in /export/storage0/data/www/xxx/Swift.php on line 303

Code: Select all

<?php

require('Swift.php');
require('SMTP.php');

$files = $_POST['files'] ;

$swift = new Swift($connection);

$file = '$files';

$name = '$files';

//The emai, itself

$swift->addPart('Image');

//add the attachment
$swift->addAttachment(file_get_contents($file), $name, 'image/jpeg');
if ($swift->isConnected())
{
    $swift->send(
        '"To user" <xxx@e-apollo.lv>',

        '"Your name" <xxx@e-apollo.lv>',

        'Image attached'
    );
}

$swift = new Swift(new Swift_Connection_SMTP('smtp.apollo.lv'));
 
//Send the email

$swift->send('xxx@e-apollo.lv', 'sender@address.com', 'The subject', 'The body');

//Terminate the connection because it's just polite

$swift->close();
?>
Post Reply