Jump to content



Photo

PHP File Form Attachment script?


  • Please log in to reply
22 replies to this topic

#16 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 04 August 2014 - 02:38

Ok, for #2:

if (!mail($emailTo, $subject, $message, "From: ".$emailFrom)) {
    echo "<p>Error: mail function failed to send email!</p>";
}

For #3, you need to add the correct headers (not just the from field), format the message part of the email correctly and actually include an encoded copy of the file in it.

You can see an example of how to do this correctly here: http://www.excellent...bmit-using-php/

 

In the example linked above, note:

  • They're adding additional headers - mime version, content type and boundary
  • They're adding all of the correct and necessary bits of meta data into the message body. (Understand that this is not stuff that the recipient will see when they view the email, not unless the recipient views it in raw source mode, or whatever their mail client calls it. Think of viewing an email in source mode like seeing the html source behind an html webpage).
  • They're encoding a copy of the file correctly with the base64_encode() and chunk_split() functions and then adding that output as part of the message.

 

To add additional text content to the message, from a brief look at the code linked above, I think the correct place would be between the 'Multipart Boundary above message' and 'Encoding file data' sections.

 

I added what you asked, didn't work, I then went to the site, copied the whole php from scratch and redid the form. didn't work. (as in doesn't send emails or anything)




#17 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 6
  • Joined: 25-March 04
  • Location: England, UK

Posted 04 August 2014 - 17:27

I added what you asked, didn't work, I then went to the site, copied the whole php from scratch and redid the form. didn't work. (as in doesn't send emails or anything)

 

Okay, so was it sending emails before or not? When I asked earlier what exactly wasn't working, you said attachments weren't working, which seemed to suggest that emails were getting through; now you seem to be suggesting that no emails have been getting through at all... which is it?

 

If you're not getting any emails through at all with the code you've tried so far, try the stripped down example code below. Replace the $to address below with your own email address, save it as a separate script, and run it. If this doesn't work, (and you've checked your spam folder haven't you??), then your problem isn't anything to do with the php code. Either you've not got the necessary mail software installed on your server; it's not configured correctly or you don't have access; or perhaps the message is getting sent but then getting blocked somewhere.

<?php

$to = 'me@example.com'; // <-- REPLACE THIS WITH YOUR EMAIL ADDRESS!!!
$subject = 'test';
$message = 'test message';
$headers = 'From: webmaster@example.com';

if (mail($to, $subject, $message, $headers)) {
    echo "sending mail suceeded!";
} else {
    echo "sending mail failed!";
}

?>


#18 shozilla

shozilla

    Neowinian Senior

  • Tech Issues Solved: 7
  • Joined: 11-January 09

Posted 04 August 2014 - 17:42

If you can not get it to work, then go to the control panel and install the mail form for your website..

 

then modify the config file that fits your needs such as email address, subject, etc.

 

If you want the attachment, then add this part to your script.

 

If you are not able to get it to work, hire someone to get it to work for you.



#19 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 04 August 2014 - 23:03

 

Okay, so was it sending emails before or not? When I asked earlier what exactly wasn't working, you said attachments weren't working, which seemed to suggest that emails were getting through; now you seem to be suggesting that no emails have been getting through at all... which is it?

 

If you're not getting any emails through at all with the code you've tried so far, try the stripped down example code below. Replace the $to address below with your own email address, save it as a separate script, and run it. If this doesn't work, (and you've checked your spam folder haven't you??), then your problem isn't anything to do with the php code. Either you've not got the necessary mail software installed on your server; it's not configured correctly or you don't have access; or perhaps the message is getting sent but then getting blocked somewhere.

 

 

 

In my original post, with the php that I posted, the email was sent but file attachment was not, so I did what you suggested, I added the

 

if (!mail($emailTo, $subject, $message, "From: ".$emailFrom)) {     echo "<p>Error: mail function failed to send email!</p>"; }

 

then I added the headers. once I did that, it did not send mail nor the attachment.



#20 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 6
  • Joined: 25-March 04
  • Location: England, UK

Posted 05 August 2014 - 00:05

In my original post, with the php that I posted, the email was sent but file attachment was not, so I did what you suggested, I added the

 

if (!mail($emailTo, $subject, $message, "From: ".$emailFrom)) {     echo "<p>Error: mail function failed to send email!</p>"; }

 

then I added the headers. once I did that, it did not send mail nor the attachment.

 

When you say the email was sent, just to be absolutely clear, did you receive it? Forget the attachment stuff for a moment; first we need to be absolutely clear about whether you're actually able to receive emails sent from php code running on your server. If not, then the problem isn't with the php code, and we need to start looking elsewhere. If you can receive emails but not those with attachments, then either there's a mistake in the php code you're running, or the email is getting blocked by something somewhere for some reason.

 

The php mail function, as I mentioned before, hands the email over to another piece of software which will actually perform the delivery. This mail delivery software may or may not accept the email handed over to it. When the php mail function attempts to hand the email over to it, the mail delivery software will respond to tell the php mail function whether it was accepted or not. Once this exchange is completed, the mail function returns true if the message was accepted by the mail delivery software, and false if not. Since there is never a guarantee that the mail delivery software will accept the message, it is thus a good idea to always check the return value of the mail function and print an success/failure message for the user as necessary.

 

Understand that even if the mail delivery software accepts the email and thus the php mail function returns true, resulting in your code probably informing the user that the message was sent, the php mail function knows nothing about what will happen to the message between the mail delivery software and the recipient's inbox. It may very well fail to reach it's destination.

 

To move forward with helping you solve the problems you're facing here, it would be really helpful to have very clear answers to the following questions:

  1. Have you actually received an email in your inbox sent from php code you've run? Forget attachments, just a plain simple email. If not, make sure you try with as little code as possible, i.e. the code in my previous post.
  2. If the answer to #1 is no, does mail() return true or false? (Otherwise ignore).
  3. If the answer to #1 is yes, try the attachment sending code again and tell use whether mail() returns true or false. (Otherwise ignore).


#21 OP HardSide

HardSide

    Neowinian

  • Joined: 14-October 05

Posted 05 August 2014 - 01:44

yes I can receive an email from my original php code.

no I cannot receive an email using the code that you provided.



#22 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 6
  • Joined: 25-March 04
  • Location: England, UK

Posted 05 August 2014 - 02:13

yes I can receive an email from my original php code.

no I cannot receive an email using the code that you provided.

 

Oh, well that's odd. Did my code print a success message but you never received it, or did it print a fail message because the mail delivery software rejected it?

Try my code with the from address set to what you used in your own code... Perhaps there's a restriction in place on what that domain can be?

 

We do need to figure out what's causing this problem, but since you can indeed receive an email with some code, we know that you've got a fully functional and complete setup for mail delivery available to you; i.e. you're not trying to do something like use the php mail function on a server without mail delivery software, and we know that the path between the mail delivery software on your server and your inbox is working. So it's helpful to know that.



#23 tim_s

tim_s

    Default

  • Joined: 07-January 13
  • OS: OSX (Macbook Pro i7), Windows 7 (Gaming), Gentoo
  • Phone: iPhone 5s

Posted 31 August 2014 - 16:01

Hi,

While I am sure it is possible, generally you will need to "stream" the data to a file storage for the email server to pull from. The file needs to be intact before sending through the email system.

*good luck*