Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

I need an email script that will send an attachment

Links for php scripts

Moderators: macek, egami, gesf

I need an email script that will send an attachment

Postby LadyDee » Wed Apr 25, 2012 12:09 pm

I have a registration form and registration code separate from the form that checks validation. In this file I want to put an email option that will send email to the user regarding their input and attache an html file to this. These are the variables that are filled in: echo $_POST["username"] . "<br />" . $_POST["email"] . "<br />" . $_POST["password"];

Can someone please help with this. I have done the following and it is not working:

//start building the mail string
$name = $_POST['username']."\n";
$email = $_POST['email']."\n";
$message = $_POST['message']."\n";
//set up the mail
$recipient = "email";
$subject = "Registration to Lets Get Started";
$mailheaders = "From: webmaster@letsgetstarted.net63.net> \n";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('newaccountletter.html')));
//define the body of the message.
ob_start(); //Turn on output buffering

//send the mail
mail($recipient, $subject, $message, $mailheaders, $headers);

//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Wed Apr 25, 2012 2:01 pm

LadyDee,

Your script is flawed, but you knew that already.
Here's a list of things you have to fix, and then a list of suggestions you *should* fix.

First, the needs..
You have two mail commands. Neither of them contain the proper data that you need.
The first one kind of does. It has $recipient set, but recipient that is defined as of this line:

$recipient = "email";

Is _not_ a valid SMTP email address. Therefore, when your mailer tries to mail this out, you're getting an NDR. Obviously, your webserver can't see this as it's all done on the post transport layer, and since your webserver user probably doesn't have a valid shell or an email account, the email will die in a matter of days and nobody will be the wiser.

For further mail debugging, and if you have access to the log file, you can often find these errors by tailing the /var/log/maillog file.

So to fix this, you have $email defined as $_POST['email'], but you're not making sure that it's 1.) a valid email, and 2.) you're taking raw input from the user and using that to send email. MAJOR no-no. MAJOR.
So, the first step here, is to clean it.

To do so, simply write your definition out lik this..

$email = trim(strip_tags($_POST['email']));

Then verify that it's atleast a valid email address by either doing a preg_match, or creating a function to do that for you.. You can find plenty of regular expressions on the web, or self built functions to do this..

There's no need to redefine the recipient, so just keep $email, and drop $recipient..


Next, the $message must contain not only your email body, but the attachment itself. The way that attachments are done is that you add a header explaining where the attachments begin and end. You have $headers defined, as well as $mailheaders, both of which should just be a part of the $headers variable.

You could inevitably just send the entire message as HTML..

view php.net/mail for help on this one, it's pretty easy really..


so..
$myEmail = "changeme@myemail.com";
$email = trim(strip_tags($_POST['email']));
$subject = "Registration to Lets Get Started";
$message = file_get_contents("newaccountletter.html");
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$myEmail.'."\r\n";

if (mail($email,$subject,$message,$headers)) {
echo "Mail Successfully sent to $email.";
} else {
echo "Mail failed to send successfully. Please check your script or mail configuration and try again.";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 4:54 am

Thank you have not tried this yet because there seems to be an error on the following line:
$headers .= 'From: '.$myEmail.'."\r\n";

Also at the top I was checking for validation of the email with the following:

function checkEmail($email) {
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])
↪*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",
$email)){
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
return false;
}
return true;
}
return false;
echo '<br/><a href="index.php">Home</a>';
}

I added your check statement any way.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 5:12 am

I was able to get it to work by changing that line from:
$headers .= 'From: '.$myEmail.'."\r\n";
to
$headers .= 'From: letsgetstarted'."\r\n";

Thank you so much.
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 5:36 am

I just checked and the html did not get sent.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Thu Apr 26, 2012 5:45 am

you need a valid smtp address in the FROM header.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 5:52 am

When it get's emailed to me it comes from: letsgetstarted@srv13.000webhost.com
but I do not get the html attachment.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Thu Apr 26, 2012 6:01 am

Ok, maybe the file doesn't exist.

Lets try this..

replace this line
$message = file_get_contents("newaccountletter.html");


with this
if (is_file("newaccountletter.html")) {
$message = file_get_contents("newaccountletter.html");
} else {
$message = "Hello. I'm sorry, but I could not find the account letter document. Please check the spelling and the CaSe SenSiTiViTY of the fIlE spELLing. :) ";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 12:09 pm

I did not receive any message and still got a blank email.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 12:34 pm

Sorry I was wrong now I am not getting an email again.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Thu Apr 26, 2012 12:55 pm

Email is coming to me, server is just slow right now, but nothing still in the body, just a blank email with a link to the site.

LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Fri Apr 27, 2012 4:20 am

Hrmmm...

Let's try changing a few things to see if we can get at least something to come through..


$myEmail = "changeme@myemail.com";
$email = "ChangeMeToYourEmailAddress@somewhere.com"; // CHANGE THIS TO YOUR ADDR
$subject = "Registration to Lets Get Started";
$message = "<html><head></head><body><div><h1>Hello</h1></div></body></html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$myEmail.'."\r\n";

if (mail($email,$subject,$message,$headers)) {
echo "Mail Successfully sent to $email.";
} else {
echo "Mail failed to send successfully. Please check your script or mail configuration and try again.";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Fri Apr 27, 2012 5:04 am

Yes I did get the Hello
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Fri Apr 27, 2012 7:05 am

I added the following to my response page:

//start building the mail string
$email = trim(strip_tags($_POST['email']));
$myEmail = "webmaster@letsgetstarted.net63.net";
$subject = "Registration to Lets Get Started";
$message = "<html><head></head><body><div>
<center>
<pre>
Your New LessonShark Account


Congratulations on creating your Let's Get Started Account!

These are things you can do with your account.

Watch videos
Add videos
See pictures
Add pictures
Read about you and your policies
Read about others and their policies
See schedules
Set schdules
View bookings
View booking

Copyright © 2012 Let's Get Started, All rights reserved.
You are receiving this email because you asked to be notified
about updates on our website at letsgetstarted.net63.net.
</pre>
</center></div></body></html>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: letsgetstarted'."\r\n";
if (mail($email,$subject,$message,$headers)) {
echo "Mail Successfully sent to $email.";
} else {
echo "Mail failed to send successfully. Please check your script or mail configuration and try again.";
}
?>

I did receive all this information in the email that was sent to me.
This is the information that was in the html I was sending. How to I add the following to the email that is sent:

echo $_POST["username"] . "<br />" . $_POST["email"] . "<br />" . $_POST["password"];

Thank you
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Fri Apr 27, 2012 9:49 am

I wouldn't send a password in email.. ever.


But, if that's what you want..


$message .= "Email: $_POST[email]<br>Username: $_POST[username]<br/>Password: $_POST[password]";


notice the . (period) before the equals sign. This means (add to the text)
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Fri Apr 27, 2012 10:56 am

Thank you so much. I will not send the passwprd like you stated not to do.
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby LadyDee » Fri Apr 27, 2012 11:07 am

Well I did not get the user name in the mail sent.
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Mon Apr 30, 2012 4:26 am

is the username being sent to the script? is it a session variable or post/get variable? Pulling it from a db table?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Mon Apr 30, 2012 8:18 am

The username is not being sent. It is a post/get $message .= "Email: $_POST[email]<br>Username: $_POST[username]<br/>Password: $_POST[password]"; but at this stage the username, email and password have been inserted into the database.
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am

Re: I need an email script that will send an attachment

Postby egami » Mon Apr 30, 2012 8:27 am

Then you just pull that data from the db using whatever you have that you can associate the user with..
this example shows the username.. but it can be user_id, anything you store in either a session variable or a hidden form variable..


$query = "SELECT * FROM users WHERE username = '".trim(strip_tags($_POST['username']))."'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
} else {
echo "Not found";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: I need an email script that will send an attachment

Postby LadyDee » Mon Apr 30, 2012 12:22 pm

I got an email with everything in it but the username, password and email.
LadyDee
LadyDee
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 10, 2012 9:16 am


Return to PHP Scripts

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron