Okay all, I have a form that I want users to fill out. When They click submit, I want the contents of this form to be put into a message, and I want the contents to be added to an excel file. Then I want the message that is sent to the recipient to have an attachment of that excel file. You will see partial mime code, I have no Idea how to use it, that was my attempt at figuring this out... Heres what I have so far:
I guess the main question is more specifically, what in the code is making the contents the user submits, automatically go into an excel file and attach itself to the email?
$date = $_POST['date'];
$org =$_POST['Org'];
$activity =$_POST['activ'];
$dofevent = $_POST['dateoe'];
$money= $_POST['amountreq'];
$name =$_POST['name'];
$email =$_POST['email'];
$phone =$_POST['pnumber'];
$dateneeded =$_POST['datenb'];
//bottom
$semester =$_POST['semester'];
$question7a =$_POST['7a'];
$question7b =$_POST['7b'];
$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers .= "From: SAF Request Form ";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
//$headers .= " boundary=\"".$mime_boundary."\"";
$subject .="SAF Request Form";
//$message .= "This is a multi-part message in MIME format.\r\n";
//$message .= "\r\n";
//$message .= "--".$mime_boundary."\r\n";
//$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
//$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message ="this is where I put my message";
mail ("20valvesofturbo#gmail.com",$subject,$message, $headers);
if( mail( "20valvesofturbo#gmail.com", $subject, $message, $headers ) ) {
echo "<p>The email was sent.</p>";
}
else {
echo "<p>There was an error sending the mail.</p>";
}
// start of xcel spreadsheet and header-->
$reg = "../data/eforms/saf/reg.xls";
$ft = fopen($reg,'a');
fwrite($ft,"header information");
// start of information submitted by user
fwrite($ft, $org);
fwrite($ft,"user submitted data");
fclose($ft);
//adds all the data
// individual excell sheet
$reg = "../data/eforms/saf/names/$org.xls";
$ft = fopen($reg,'w+');
fwrite($ft,"blablabla");
fclose($ft);
?>
what this does is it submits the data and sends a message, but it sends the data in an unknown file format as an attachment. If you open it in excel, it has the contents of the message...
Thanks!
For generating MIME encoded messages, I suggest that you use the Pear::Mail_Mime package ( http://pear.php.net/package/Mail_Mime). As for generating an Excel file, you can have a look at http://pear.php.net/package/Spreadsheet_Excel_Writer; depending on the complexity of the data to put into Excel, you could possibly also use just a CSV file
Related
I am sending a confirmation mail to user to confirm his account. The confirmation mail is styled using HTML and has an href element which points the users to a PHP file where I do the confirmation process. This href link also needs to have a PHP randomstring attached, the same randomstring is saved in Database and is also sent to user so that the cross-checking can be done in PHP once the user clicks on it.
<td align="center" style="margin:0;text-align:center">
<a href="http://aliencreative.wehubs.com/new_alien/email.php?code=<?php echo $randomString; ?>"
style="font-size:21px;line-height:22px;text-decoration:none;color:#ffffff;font-weight:bold;
border-radius:2px;background-color:#0096d3;padding:14px 40px;display:block;
letter-spacing:1.2px" target="_blank">Confirm Alien account now!</a></td>
The PHP code includes the above HTML as follows.
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
$htmlContent = file_get_contents("email_template.php");
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>
However, the PHP variable is't getting available in the link.
Please try this. Remove file_get_contents and use include with output buffer function. I think this will insert your variable to your template. Please check the below updated code. Anyway your method is not a secure. Please create more complicated random code
<?php
$randomString=time();
//$random="http://aliencreative.wehubs.com/new_alien/email.php?code=".$randomString;
echo $random;
$to = 'sample#gmail.com';
$subject = "Confirmation mail";
// Get HTML contents from file
ob_start();
include "email_template.php";
$htmlContent=ob_get_contents();
ob_end_clean();
// Set content-type for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: Alien creative control<alien#alien.com>' . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Some problem occurred, please try again.';
endif;
?>
I had reference this solution from other post and I tried modify the content to suit my requirement.
Here is the code that I implemented to send attachment file via mail using PHP.
$htmlbody = " Your Mail Contant Here.... You can use html tags here...";
$to = "foo#bar.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: foo#bar.com\r\nReply-To: foo#bar.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$path = $_FILES["cv"]["name"];
$attachment = chunk_split(base64_encode(file_get_contents($path)));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip;
name=\"$path\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
I able to get the attachment file but I cant open it.
It show me the error of "The application you chose ("") could not be found. Check the file name or choose another application."
Can anyone correct my error..?
It seems that the problem relies in the file name after download. The server did the job and sent the email and this is rather a client side problem.
Check the filename extension and make sure it is the same as the side that sent it, If not make sure u correct your PHP code naming function.
Make sure the application that reads that filetype is available on your PC.
Try to open the file on a different computer.
I've done some research on this issue but still can't find an answer that I understand. Basically I'm trying to add a Logo to the emails sent via my email PHP script. The code of my PHP script is:
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$sub = $_POST['Subject'];
$message = $_POST['Message'];
$Subject = "$sub";
$Sendto = "Omitted for privacy reasons";
mail($Sendto,$Subject,
"$message
Email: $email
Name: $name
"
,"From: $email,$name");
?>
How do I go about putting an image held on my server in Images/logo above the email:$email part of the message?
thanks
As a side question can anyone provide me with a guide or reading on how to beautify the message as its so plain
What you might do is to send the mail not as "text/plain", but as "text/html".
You e-mail content is then basically a static webpage and you can insert images to.
Example Code for Mail with HTML Content
$to = 'to#someone.com';
$subject = 'The Subject';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: other#someone.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
// this is the important header to set the type
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// now $message can be a static html page like:
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';
// with image embedded
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="http://i.stack.imgur.com/Qmw3Z.jpg?s=32&g=1" alt="AVTR" />';
$message .= '</body></html>';
Important: If you insert something into the message coming from POST, do not forget to escape it properly with htmlentities($_POST['somevar']) or striptags.
Then send mail:
mail($to, $subject, $message, $headers);
Answer to side-quest: "can anyone provide me with a guide or reading on how to beautify the message as its so plain". When you send the mail as HTML you might apply CSS styles to your HTML.
Starter tutorial: http://webdesign.tutsplus.com/articles/build-an-html-email-template-from-scratch--webdesign-12770
you might also google for "html email templates", download one and modify to your needs
I use PHP script to send email with multiple attachments, it works great for gmail, but in Microsoft Outlook i also see blank file ATT00010.txt (random numbers.) as attachment. And when i send email from outlook with multiple attachments as well it does not show no file like this.
I echo'ed output from email script and there is no such file in code. Can someone tell me how to remove this file from outlook?
Email script is below.
// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
// email fields: to, from, subject, and so on
$to = "mail#mail.com";
$from = "mail#mail.com";
$subject ="My subject";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
If you want something that's a little less of a pain to use and send attachments with, try Swift Mailer. (swiftmailer.org) I've been using it in my projects and it works great.
Here's an example:
$message = Swift_Message::newInstance()
->setSubject('Webinar Registration')
->setFrom(array('replyto#example.org' => 'From Name'))
->setTo(array('destination#example.org'))
->setBody($MESSAGE_TEXT)
;
$message->attach(Swift_Attachment::fromPath('SOME_FILE_PATH'));
$transport = Swift_SmtpTransport::newInstance('127.0.0.1', 25);
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Just my two cents.
Otherwise, I was going to mention what someone else already beat me to -- check the boundaries.
The last boundary line in a multipart/* message must have -- appended at the end, in addition to what all of the other boundary lines have. A consumer can use that to recognize the end of a message.
Apparently Outlook treats the absence of the correct ending as an indication that the message has been truncated, and then does the best it can to display whatever it did receive.
Its my second account, i actually found solution and don't need no library or class at all although i might turn this into class and add some stuff, its always better to have full understanding of process not just from, to, files etc fields.
Solution is simple here just replace last part with
if ($x == count($files)-1)
$message .= "--{$mime_boundary}--\r\n";
else
$message .= "--{$mime_boundary}\n";
\r is not necessary
if you just put -- it will use it many times inside loop IF checks if this is last loop.
I have been struggling with trying to send an email with an attachment using PHP. It used to work but the message body was scrambled. Now I have got the message body to work but the attachment corrupts. I used to use base64 encoding for the message body but now use 7bit. Can anyone tell me what I am doing wrong?
PS please do not tell me that I should be using a pre-made class to do this. I have tried several and they have all failed to work. If I do not overcome these problems I will never learn how to do it properly. Thanks
//define the receiver of the email
$to = 'a#something.co.uk';
//define the subject of the email
$subject = 'Your Disneyland Paris entry';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \n
$mime_boundary = "<<<--==+X[".md5(time())."]";
$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
$fileContent = chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));
$headers .= "From: info#blah.org.uk <info#blah.org.uk>"."\n";
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n";
$message .= "\n";
$message .= "--".$mime_boundary."\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n";
$message .= "\n";
$message .= "messagebody \n";
$message .= "--".$mime_boundary."" . "\n";
$message .= "Content-Type: application/octet-stream;\n";
$message .= " name=\"CTF-brochure.pdf\"" . "\n";
$message .= "Content-Transfer-Encoding: 7bit \n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"CTF_brochure.pdf\"\n";
$message .= "\n";
$message .= $fileContent;
$message .= "\n";
$message .= "--".$mime_boundary."--\n";
//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";
I could be wrong but I believe you will have to encode the PDF somehow, 7bit won't work as the PDF file will have content outside the range. Why not use base64 for the PDF?
I would suggest looking at phpmailer if you want to do complex email.
I know you've said about pre-built classes but there is a reason that people do this - why re-invent the wheel? I use SwiftMailer for projects - it couldn't be simpler. See this SwiftMailer example for 13 lines (including some blank ones) of how to create a message, add an attachment and send.
As to the resolution of your actual query, upvote to Josh's answer - I'd second changing the encoding and seeing how you get on. Have you tried getting an example email message which has an attachment that works, and examining the raw data?