FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problem with attaching file (from a form) to email in PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Problem with attaching file (from a form) to email in PHP [message #172661] Thu, 24 February 2011 02:22 Go to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
Hi All,

I'm fairly new to PHP, and need some help with a script that I'm
trying to write, to send infos from a form by email (with a file
attched).

I've managed my way to write a script that sends the email in plain
text, but when I tryied to modify it, it seemed that I can't be able
to find the proper way to add the attachment to the email.

Basically, I have a form, that gets all pertinent infos (emails,
subject, ... and I just added the file browser, so the user can attach
the file (pictures) from his local drive).

here is the code I have right now...

If you could help me... it's been 2 weeks that I try to find the
solution, but every new solution I find is not working... I saw some
people use php mailer, or expertmailer (xpm4), but I can't seem to
understand, because t is not working either... Hellllp... pleaaaaasee

<?php

$UploadedFile_name=$_FILES["UploadedFile"]["name"];
$UploadedFile_type=$_FILES["UploadedFile"]["type"];
$UploadedFile_size=$_FILES["UploadedFile"]["size"];
$UploadedFile_temp=$_FILES["UploadedFile"]["tmp_name"];


// MAIL SUBJECT

$subject = $_POST['Sujet'] . " from site XYZ";
$subject = str_replace("\'", "'", $subject);



// MAIL ADDRESS

$to = "System Administrator <JohnDoe(at)gmail(dot)com>";

$nom = str_replace("'", "''", $_POST['Nom']);
$from = $nom . " <" . $_POST['Email'] . ">";



// HTML Message

$message= '


<table cellspacing="0" cellpadding="8" border="0" width="400">
<tr>
<td colspan="2"></td>
</tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Produit</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Produit'].", " . $_POST['Portions'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Date demandée</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'. $_POST['Date'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>'.$_POST['Livraison'].'</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Adresse'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Commentaires</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Commentaires'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Nom</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Nom'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
<tr bgcolor="#eeeeee">
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Téléphone</strong></td>
<td style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Telephone'].'</td>
</tr>
<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>

<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px;
color:#333333;"><strong>Commentaires</strong></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" style="font-family:Verdana, Arial; font-size:11px;
color:#333333;">'.$_POST['Commentaires'].'</td>
</tr>

<tr><td colspan="2" style="padding:0px;"><img src="images/
whitespace.gif" alt="" width="100%" height="1" /></td></tr>
</table>

';


// TXT Message


$message = $_POST['Commentaires'] . "\n\n\n\n";
$message .= $nom . "\n";
$message .= $_POST['Telephone'] . "\n\n";
$message = str_replace("\'", "'", $message);
$message = str_replace('\"', '"', $message);




// MAIL HEADERS with attachment

$fp = fopen($UploadedFile_temp, "rb");
$file = fread($fp, $UploadedFile_size);

$file = chunk_split(base64_encode($file));
$num = md5(time());



//Normal headers

$headers = "From: " . $from . "\r\n";
$headers .= "CC: " . $from. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";

// This two steps to help avoid spam (apparently....)

$headers .= "Message-ID: <".gettimeofday()." TheSystem@".
$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";

$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";

// Attachment headers

$headers .= "Content-Type:".$UploadedFile_type." ";
$headers .= "name=\"".$UploadedFile_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$UploadedFile_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";


// SEND MAIL

$sent = mail($to, $subject, $message, $headers);
fclose($fp);

?>
Re: Problem with attaching file (from a form) to email in PHP [message #172664 is a reply to message #172661] Thu, 24 February 2011 04:02 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/23/2011 9:22 PM, Cameleon wrote:
> Hi All,
>
> I'm fairly new to PHP, and need some help with a script that I'm
> trying to write, to send infos from a form by email (with a file
> attched).
>
> I've managed my way to write a script that sends the email in plain
> text, but when I tryied to modify it, it seemed that I can't be able
> to find the proper way to add the attachment to the email.
>
> Basically, I have a form, that gets all pertinent infos (emails,
> subject, ... and I just added the file browser, so the user can attach
> the file (pictures) from his local drive).
>
> here is the code I have right now...
>
> If you could help me... it's been 2 weeks that I try to find the
> solution, but every new solution I find is not working... I saw some
> people use php mailer, or expertmailer (xpm4), but I can't seem to
> understand, because t is not working either... Hellllp... pleaaaaasee
>

Answered in alt.php.

Please learn to crosspost instead of multiposting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172668 is a reply to message #172661] Thu, 24 February 2011 10:42 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 24-02-11 03:22, Cameleon wrote:
> Hi All,
>
> I'm fairly new to PHP, and need some help with a script that I'm
> trying to write, to send infos from a form by email (with a file
> attched).
>
> I've managed my way to write a script that sends the email in plain
> text, but when I tryied to modify it, it seemed that I can't be able
> to find the proper way to add the attachment to the email.
>
> Basically, I have a form, that gets all pertinent infos (emails,
> subject, ... and I just added the file browser, so the user can attach
> the file (pictures) from his local drive).
>
> here is the code I have right now...
>
> If you could help me... it's been 2 weeks that I try to find the
> solution, but every new solution I find is not working... I saw some
> people use php mailer, or expertmailer (xpm4), but I can't seem to
> understand, because t is not working either... Hellllp... pleaaaaasee
>

What is not working?
Do you get errors? and what kind of errors?


--
Luuk
Re: Problem with attaching file (from a form) to email in PHP [message #172699 is a reply to message #172668] Sat, 26 February 2011 14:56 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
Sorry about that.. was so stuck in my problem that I forgot to say
what it was.

I've continued working on it, and managed to make it work... half.

I've rewrote the code, and now I'm able to successfully send the
email, but I can only see the attachment when using gmail. If I red
the message using the Mac "Mail" program, there is no trace of the
attachment (neither as attached file, neither inline in the message
body), and if I read it using MS Outlook, The attachment is there with
the proper file size, but when I try to read it, it says No preview
available or file is empty. Also, interresting thing is that if I
take the message that I received in gmail, and forward it using gmail,
I can now read it properly with both Mail and Outlook.

I kind of highly suspect that there is a problem with the heder or
something like this, but can't see waht.

Thanks for your help, here is the parts of the code that deals with
the sending...


// ATTACHENMENT INFOS
$UploadedFile_name = $_FILES["UploadedFile"]["name"];
$UploadedFile_type = $_FILES["UploadedFile"]["type"];
$UploadedFile_size = $_FILES["UploadedFile"]["size"];
$UploadedFile_temp = $_FILES["UploadedFile"]["tmp_name"];


// MAIL SUBJECT
$subject = $_POST['Sujet'] . " from website";
$subject = str_replace("\'", "'", $subject);


// MAIL ADDRESSES
$to = "Webmaster <Master(at)Website(dot)com>";

$name = str_replace("'", "''", $_POST['Name']);
$From = $name . " <" . $_POST['Email'] . ">";

//Initial Headers
$boundary = md5(date('r', time()));

$headers = "From: ".$From."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=" . $boundary .
"\r\n";
$headers .= "MIME-Version: 1.0\r\n";



//First Content Type (TXT)
$message = "\r\n\r\n--" . $boundary . "\r\n"; // MAKE SURE there's
only ONE(1) "\r\n" between the above boundry and the first header
below (Content-Type)

$message .= "Content-Transfer-Encoding: quoted-printable\r\n";
$message .= "Content-type: text/plain; charset=\"UTF-8\"\r\n\r\n";


$message .= $MsgTXT;


If ($UploadedFile_name != "" ) {

//Second Content Type (ATTACHEMENT)
$message .= "\r\n\r\n--" . $boundary . "\r\n";

$message .= "Content-Disposition: in-line; filename=
$UploadedFile_name\r\n";
$message .= "Content-type: ".$UploadedFile_type."; x-unix-mode=0755;
name=\"$UploadedFile_name\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";

$message .= base64_encode(file_get_contents($_FILES["UploadedFile"]
["tmp_name"])); // Attachement Encoding

$message .= "\r\n\r\n--" . $boundary . "--"; // <- This indicates
the end of the boundries. Notice the additional "--" after the
boundry's value.
}

// Send the email using "mail()".
$MailSent = @mail($to, $subject, $message, $headers);
Re: Problem with attaching file (from a form) to email in PHP [message #172701 is a reply to message #172699] Sat, 26 February 2011 15:23 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 26-02-11 15:56, Cameleon wrote:
> Sorry about that.. was so stuck in my problem that I forgot to say
> what it was.
>
> I've continued working on it, and managed to make it work... half.
>
> I've rewrote the code, and now I'm able to successfully send the
> email, but I can only see the attachment when using gmail. If I red
> the message using the Mac "Mail" program, there is no trace of the
> attachment (neither as attached file, neither inline in the message
> body), and if I read it using MS Outlook, The attachment is there with
> the proper file size, but when I try to read it, it says No preview
> available or file is empty. Also, interresting thing is that if I
> take the message that I received in gmail, and forward it using gmail,
> I can now read it properly with both Mail and Outlook.
>
> I kind of highly suspect that there is a problem with the heder or
> something like this, but can't see waht.
>
> Thanks for your help, here is the parts of the code that deals with
> the sending...
>
>

Something is wrong with the way you build the email, and gmail is nice
enough to repair it for you when you forward the message...

Read this: http://en.wikipedia.org/wiki/MIME
and compare what you read with wat you do in the code, than you'll find
the answer.

I begin to wonder why i never had this problem.....
It must be because i dont send mail from PHP... ;)

--
Luuk
Re: Problem with attaching file (from a form) to email in PHP [message #172702 is a reply to message #172701] Sat, 26 February 2011 15:54 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
Thanks Luuk, I'll have alook... but in the mean time, I don't get
what you mean by your last sentence... is it a bad thing to send mail
by php? is there a better way?

Thanks
Re: Problem with attaching file (from a form) to email in PHP [message #172703 is a reply to message #172702] Sat, 26 February 2011 16:07 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 26-02-11 16:54, Cameleon wrote:
> Thanks Luuk, I'll have alook... but in the mean time, I don't get
> what you mean by your last sentence... is it a bad thing to send mail
> by php? is there a better way?
>
> Thanks

I just meant to say that an emailmessage can be complex, if you are
trying to format it in HTML, and attach somethings...

SO, this one time when i needed to send a message, with an attachment i
decided to do it in plain-text, with a bash script, using 'mailx'...

--
Luuk
Re: Problem with attaching file (from a form) to email in PHP [message #172707 is a reply to message #172703] Sat, 26 February 2011 19:36 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/26/2011 11:07 AM, Luuk wrote:
> On 26-02-11 16:54, Cameleon wrote:
>> Thanks Luuk, I'll have alook... but in the mean time, I don't get
>> what you mean by your last sentence... is it a bad thing to send mail
>> by php? is there a better way?
>>
>> Thanks
>
> I just meant to say that an emailmessage can be complex, if you are
> trying to format it in HTML, and attach somethings...
>
> SO, this one time when i needed to send a message, with an attachment i
> decided to do it in plain-text, with a bash script, using 'mailx'...
>

Which is why I use phpmailer. It's much better than trying to send by hand.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172719 is a reply to message #172707] Sun, 27 February 2011 15:04 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
On Feb 26, 2:36 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 2/26/2011 11:07 AM, Luuk wrote:
>
>> On 26-02-11 16:54, Cameleon wrote:
>>> Thanks Luuk, I'll have  alook...  but in the mean time, I don't get
>>> what you mean by your last sentence... is it a bad thing to send mail
>>> by php? is there a better way?
>
>>> Thanks
>
>> I just meant to say that an emailmessage can be complex, if you are
>> trying to format it in HTML, and attach somethings...
>
>> SO, this one time when i needed to send a message, with an attachment i
>> decided to do it in plain-text, with a bash script, using 'mailx'...
>
> Which is why I use phpmailer.  It's much better than trying to send by hand.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Thanks Jerry,

you are right about using phpmailler... but I tried it and couldn't
find much unformations, andnwas not able to use it properly... do you
have any documentation or code example to do something like I'm trying
to?
Re: Problem with attaching file (from a form) to email in PHP [message #172720 is a reply to message #172719] Sun, 27 February 2011 15:18 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 27-02-11 16:04, Cameleon wrote:
> On Feb 26, 2:36 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Which is why I use phpmailer. It's much better than trying to send by hand.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Thanks Jerry,
>
> you are right about using phpmailler... but I tried it and couldn't
> find much unformations, andnwas not able to use it properly... do you
> have any documentation or code example to do something like I'm trying
> to?

http://phpmailer.worxware.com/index.php?pg=examples

--
Luuk
Re: Problem with attaching file (from a form) to email in PHP [message #172731 is a reply to message #172720] Sun, 27 February 2011 18:41 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 10:18 AM, Luuk wrote:
> On 27-02-11 16:04, Cameleon wrote:
>> On Feb 26, 2:36 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>>> Which is why I use phpmailer. It's much better than trying to send by hand.
>>>
>>
>> Thanks Jerry,
>>
>> you are right about using phpmailler... but I tried it and couldn't
>> find much unformations, andnwas not able to use it properly... do you
>> have any documentation or code example to do something like I'm trying
>> to?
>
> http://phpmailer.worxware.com/index.php?pg=examples
>

http://www.lmgtfy.com/?q=phpmailer+examples

All kinds of good examples - including the first one being the link Luuk
posted.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172732 is a reply to message #172707] Sun, 27 February 2011 18:43 Go to previous messageGo to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
On Sat, 26 Feb 2011 14:36:58 -0500, Jerry Stuckle wrote:


> Which is why I use phpmailer. It's much better than trying to send by
> hand.

Hmmm, I tried discussing with you before, but it almost always ended up
in religious war. Let me try again. Why do you prefer phpmailer over PEAR
Mail? I have to admit, I use PEAR whenever possible, because it is a part
of PHP and no separate installation is necessary.



--
http://mgogala.byethost5.com
Re: Problem with attaching file (from a form) to email in PHP [message #172738 is a reply to message #172732] Sun, 27 February 2011 19:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 1:43 PM, Mladen Gogala wrote:
> On Sat, 26 Feb 2011 14:36:58 -0500, Jerry Stuckle wrote:
>
>
>> Which is why I use phpmailer. It's much better than trying to send by
>> hand.
>
> Hmmm, I tried discussing with you before, but it almost always ended up
> in religious war. Let me try again. Why do you prefer phpmailer over PEAR
> Mail? I have to admit, I use PEAR whenever possible, because it is a part
> of PHP and no separate installation is necessary.
>
>
>

You didn't get into a religious war with me - unless you started it.

I like phpmailer because it's more flexible, does more things, and is
better supported.

The PEAR classes are no more or less part of PHP than any other
externally-written code. There is nothing special about them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172751 is a reply to message #172731] Sun, 27 February 2011 23:57 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
On Feb 27, 1:41 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 2/27/2011 10:18 AM, Luuk wrote:
>
>> On 27-02-11 16:04, Cameleon wrote:
>>> On Feb 26, 2:36 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>>>> Which is why I use phpmailer.  It's much better than trying to send by hand.
>
>>> Thanks Jerry,
>
>>> you are right about using phpmailler... but I tried it and couldn't
>>> find much unformations, andnwas not able to use it properly...  do you
>>> have any documentation or code example to do something like I'm trying
>>> to?
>
>> http://phpmailer.worxware.com/index.php?pg=examples
>
> http://www.lmgtfy.com/?q=phpmailer+examples
>
> All kinds of good examples - including the first one being the link Luuk
> posted.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Tnak you all... seems very promising.

a last question... if it says

require_once '../class.phpmailer.php'

does that mean that I copy this file in the root directory of my
server (where the main index.htm file reside, and do I have to
copy all other phpmailer files too...
Re: Problem with attaching file (from a form) to email in PHP [message #172752 is a reply to message #172738] Mon, 28 February 2011 00:20 Go to previous messageGo to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
On Sun, 27 Feb 2011 14:07:53 -0500, Jerry Stuckle wrote:

> The PEAR classes are no more or less part of PHP than any other
> externally-written code. There is nothing special about them.

Pear is distributed with PHP, many PEAR and PECL modules make it into the
language itself. Examples are SPL and PHAR. On the plus side, I checked
phpmailer and I like it. You are right, I don't need to ignore
E_DEPRECATED with phpmailer as I need to do with PEAR::Mail. The class
looks good.



--
http://mgogala.byethost5.com
Re: Problem with attaching file (from a form) to email in PHP [message #172756 is a reply to message #172751] Mon, 28 February 2011 01:16 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 6:57 PM, Cameleon wrote:
> On Feb 27, 1:41 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>> On 2/27/2011 10:18 AM, Luuk wrote:
>>
>>> On 27-02-11 16:04, Cameleon wrote:
>>>> On Feb 26, 2:36 pm, Jerry Stuckle<jstuck...@attglobal.net> wrote:
>>>> > Which is why I use phpmailer. It's much better than trying to send by hand.
>>
>>>> Thanks Jerry,
>>
>>>> you are right about using phpmailler... but I tried it and couldn't
>>>> find much unformations, andnwas not able to use it properly... do you
>>>> have any documentation or code example to do something like I'm trying
>>>> to?
>>
>>> http://phpmailer.worxware.com/index.php?pg=examples
>>
>> http://www.lmgtfy.com/?q=phpmailer+examples
>>
>> All kinds of good examples - including the first one being the link Luuk
>> posted.
>>
>
> Tnak you all... seems very promising.
>
> a last question... if it says
>
> require_once '../class.phpmailer.php'
>
> does that mean that I copy this file in the root directory of my
> server (where the main index.htm file reside, and do I have to
> copy all other phpmailer files too...

In the example given, that would be relative to the file including
phpmailer. Since the example is in the root directory of your web
server, the phpmailer class file would be one level below the root
directory.

Remember - examples are meant to be exactly that - EXAMPLES. They
provide guidance, but are NOT necessarily correct for your specific
installation. They often need to me modified as necessary.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172757 is a reply to message #172752] Mon, 28 February 2011 01:18 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/27/2011 7:20 PM, Mladen Gogala wrote:
> On Sun, 27 Feb 2011 14:07:53 -0500, Jerry Stuckle wrote:
>
>> The PEAR classes are no more or less part of PHP than any other
>> externally-written code. There is nothing special about them.
>
> Pear is distributed with PHP, many PEAR and PECL modules make it into the
> language itself. Examples are SPL and PHAR. On the plus side, I checked
> phpmailer and I like it. You are right, I don't need to ignore
> E_DEPRECATED with phpmailer as I need to do with PEAR::Mail. The class
> looks good.
>
>
>

No, PEAR and PECL are NOT distributed with PHP. The classes are
available from the PHP site, but they are still not part of the PHP
code, and are written by external developers.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172764 is a reply to message #172732] Mon, 28 February 2011 10:23 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Mladen Gogala wrote:
> On Sat, 26 Feb 2011 14:36:58 -0500, Jerry Stuckle wrote:
>
>
>> Which is why I use phpmailer. It's much better than trying to send by
>> hand.
>
> Hmmm, I tried discussing with you before, but it almost always ended up
> in religious war. Let me try again. Why do you prefer phpmailer over PEAR
> Mail? I have to admit, I use PEAR whenever possible, because it is a part
> of PHP and no separate installation is necessary.
>
>
>
The reason I did it by hand using he standard tools - which is hard,
but not that hard, is that way I can correct any bugs in it.

All too often using someone else's library means you include far more
code of uncertain quality into the mix.

When it gets to the point - and it has - that one mail sent on one
system works in 8 out of ten email clients, but not the 9th and tenth,
whilst the same code works perfectly on another system so near identical
as to be head scratchingly obscure, you have to go looking very hard to
find out why.
Re: Problem with attaching file (from a form) to email in PHP [message #172776 is a reply to message #172757] Mon, 28 February 2011 21:17 Go to previous messageGo to next message
Deadly Dirk is currently offline  Deadly Dirk
Messages: 2
Registered: January 2011
Karma: 0
Junior Member
On Sun, 27 Feb 2011 20:18:31 -0500, Jerry Stuckle wrote:

> No, PEAR and PECL are NOT distributed with PHP. The classes are
> available from the PHP site, but they are still not part of the PHP
> code, and are written by external developers.

Look at the configure file. You have to specify "without pear" if you
don't want PEAR included. I configured my PHP like this:
../configure' '--with-apxs2=/usr/local/bin/apxs' '--enable-sigchild' '--
disable-pdo' '--without-sqlite' '--without-mysql' '--with-config-file-
path=/usr/local/lib/php' '--without-oci8' '--with-pgsql=/usr/pgsql-9.0'

PEAR was included. I am not sure what are you using, but PEAR comes with
PHP. My PHP is brand new:
php -v
PHP 5.3.5 (cli) (built: Jan 6 2011 16:42:33)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[mgogala@lpo-postgres-01 ~]$





--
The missionaries go forth to Christianize the savages -
as if the savages weren't dangerous enough already.
Re: Problem with attaching file (from a form) to email in PHP [message #172780 is a reply to message #172756] Tue, 01 March 2011 00:58 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
Thanks Jerry... can I use variables, like I did below... it keeps
telling me invalid address??



=======

// MAIL SUBJECT
$subject = $_POST['Sujet'] . " from site xyz";
$subject = str_replace("\'", "'", $subject);


// MAIL ADDRESSES

$nom = str_replace("'", "''", $_POST['Nom']);
$NameFrom = $nom;
$EmailFrom = "<" . $_POST['Email'] . ">";

$NameTo = "Test User";
$EmailTo = "<Tests(at)xyz(dot)com>";



.....



require_once '../class.phpmailer.php';

$mail = new PHPMailer(true);



try {
$mail->AddReplyTo($EmailFrom, $NameFrom);
$mail->AddAddress($EmailTo, $NameTo);
$mail->SetFrom($EmailFrom, $NameFrom);
$mail->AddReplyTo($EmailFrom, $NameFrom);
$mail->Subject = $subject;
$mail->AltBody = $message;
$mail->MsgHTML(file_get_contents('message.html'));
$mail->AddAttachment($UploadedFile_name);
$mail->Send();
echo "Message Sent OK<p></p>\n";
}
Re: Problem with attaching file (from a form) to email in PHP [message #172781 is a reply to message #172780] Tue, 01 March 2011 01:07 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/28/2011 7:58 PM, Cameleon wrote:
> Thanks Jerry... can I use variables, like I did below... it keeps
> telling me invalid address??
>
>
>
> =======
>
> // MAIL SUBJECT
> $subject = $_POST['Sujet'] . " from site xyz";
> $subject = str_replace("\'", "'", $subject);
>
>
> // MAIL ADDRESSES
>
> $nom = str_replace("'", "''", $_POST['Nom']);
> $NameFrom = $nom;
> $EmailFrom = "<" . $_POST['Email'] .">";
>
> $NameTo = "Test User";
> $EmailTo = "<Tests(at)xyz(dot)com>";
>
>
>
> ....
>
>
>
> require_once '../class.phpmailer.php';
>
> $mail = new PHPMailer(true);
>
>
>
> try {
> $mail->AddReplyTo($EmailFrom, $NameFrom);
> $mail->AddAddress($EmailTo, $NameTo);
> $mail->SetFrom($EmailFrom, $NameFrom);
> $mail->AddReplyTo($EmailFrom, $NameFrom);
> $mail->Subject = $subject;
> $mail->AltBody = $message;
> $mail->MsgHTML(file_get_contents('message.html'));
> $mail->AddAttachment($UploadedFile_name);
> $mail->Send();
> echo "Message Sent OK<p></p>\n";
> }
>

Among other things, you need to do a LOT more sanitizing of your $_POST
fields. NEVER trust input from the user!

And look at the examples again. You do not place <..> around the address.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172782 is a reply to message #172781] Tue, 01 March 2011 02:33 Go to previous messageGo to next message
Cameleon is currently offline  Cameleon
Messages: 7
Registered: February 2011
Karma: 0
Junior Member
Thanks Jerry,

thanks for the advice, but there is a lot more sanitizing done (found
that already...), I just simplified the code for the forum. Worked
for the <> ... stupid me, need new glasses!

one last (I hope) question if you still want...

1) seems it doesn't want to access the file that I want to attached
(posted from the form... see code below)



// ATTACHENMENT INFOS
$UploadedFile_name = $_FILES["UploadedFile"]["name"];
$UploadedFile_type = $_FILES["UploadedFile"]["type"];
$UploadedFile_size = $_FILES["UploadedFile"]["size"];
$UploadedFile_temp = $_FILES["UploadedFile"]["tmp_name"];


ive tried:

$mail->AddAttachment(file_get_contents($UploadedFile_name));

and

$mail->AddAttachment($UploadedFile_name);

with no success...
Re: Problem with attaching file (from a form) to email in PHP [message #172783 is a reply to message #172782] Tue, 01 March 2011 02:58 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 2/28/2011 9:33 PM, Cameleon wrote:
> Thanks Jerry,
>
> thanks for the advice, but there is a lot more sanitizing done (found
> that already...), I just simplified the code for the forum. Worked
> for the<> ... stupid me, need new glasses!
>
> one last (I hope) question if you still want...
>
> 1) seems it doesn't want to access the file that I want to attached
> (posted from the form... see code below)
>
>
>
> // ATTACHENMENT INFOS
> $UploadedFile_name = $_FILES["UploadedFile"]["name"];
> $UploadedFile_type = $_FILES["UploadedFile"]["type"];
> $UploadedFile_size = $_FILES["UploadedFile"]["size"];
> $UploadedFile_temp = $_FILES["UploadedFile"]["tmp_name"];
>
>
> ive tried:
>
> $mail->AddAttachment(file_get_contents($UploadedFile_name));
>
> and
>
> $mail->AddAttachment($UploadedFile_name);
>
> with no success...
>
>

Look at the data in your $_FILES array:

print_r($_FILES);

Note that the file is initially stored using the tmp_name, not the file
name (in case of conflicts). So until you actually move the file with
move_uploaded_file() (which you need to do on some OS's anyway), the
file won't exist under the permanent name.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problem with attaching file (from a form) to email in PHP [message #172787 is a reply to message #172782] Tue, 01 March 2011 09:44 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Cameleon wrote:
> Thanks Jerry,
>
> thanks for the advice, but there is a lot more sanitizing done (found
> that already...), I just simplified the code for the forum. Worked
> for the <> ... stupid me, need new glasses!
>
> one last (I hope) question if you still want...
>
> 1) seems it doesn't want to access the file that I want to attached
> (posted from the form... see code below)
>
>
>
> // ATTACHENMENT INFOS
> $UploadedFile_name = $_FILES["UploadedFile"]["name"];
> $UploadedFile_type = $_FILES["UploadedFile"]["type"];
> $UploadedFile_size = $_FILES["UploadedFile"]["size"];
> $UploadedFile_temp = $_FILES["UploadedFile"]["tmp_name"];
>
>
> ive tried:
>
> $mail->AddAttachment(file_get_contents($UploadedFile_name));
>
> and
>
> $mail->AddAttachment($UploadedFile_name);
>
> with no success...
>
>
This *may* be another instance of an issue I found where a third party
program is invoked to access the temporary file. And that is, that the
scope of the temporary file only exists within PHP itself. It s not
written to disk or to the operating system.

I solved this by copying the temporary file using copy() and then
invoking (in my case) an SQL LOAD_FILE command on the _copy_.

Given that the php mailer class should exist entirely within PHP, this
should not be an issue in this case, but it may be if it invokes some
other program to process the attached file.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: SOCIAL NETWORK FOR PHP PLATFORM..
Next Topic: HTML select field where the OPTION text is of different colors
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Oct 20 23:26:50 GMT 2024

Total time taken to generate the page: 0.02230 seconds