$from = "from#domain.com";
$to = "myemail#email";
$subject ="Contact Form";
$message ="<p>Item Name: $itemname.</p>
<p>Brand: $brandname.</p>
<p>Description: $description.</p>
<p>Availability: $availability.</p>
<p>Contact Person: $contact</p>
<p>Email: $email</p>
<p>Location: $location</p>
";
$boundary = md5(time());
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from."\r\n";
$headers .= "Reply-To: ".$from."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Transfer-Encoding: 7bit"."\r\n\r\n";
$body .= "This is a MIME encoded message."."\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $message ."\r\n";
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: application/octet-stream; name=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";
$body .= $encoded_content ."\r\n";
if (#mail( $to, $subject, $body, $headers)) {
$resultMessage = '<div class="alert alert-success">Thank You. Your message has been sent</div>';
} else {
$resultMessage = '<div class="alert alert-danger">Sorry Please Try Again.</div>';
}
}
This is my form PHP code. It is used by my logged in users so users usually signup by choosing username>email>password and register then they log in and see this contact form. so I want that when they submit a form , PHP should send their Username and Email. How can I do that?
Related
while sending a mail using PHP mail() without attachment working fine.
But while sending with attachment sending Failed
please help me.
Whats wrong in my code?
Thanks in advance.
My HTML code is shown below
index.html
<html>
<head>
<title>Mail</title>
</head>
<body>
<form method="POST" action="mail.php" enctype="multipart/form-data">
<input type="text" name="fromName">
<input type="email" name="fromEmail">
<input type="file" name="fileAttach">
<input type="submit" name="submit">
</form>
</body>
</html>
The following is my Mail code
mail.php
<?php
if (isset($_POST['submit'])) {
/* $mailto = $_POST["mailTo"];*/
$mailto = "to#gmail.com";
$from_mail = $_POST["fromEmail"];
$replyto = $_POST["fromEmail"];
$from_name = $_POST["fromName"];
/*$message = $_POST["message"];*/
$message="This is message part";
/*$subject = $_POST["subject"];*/
$subject ="this is subject part";
$filename = $_FILES["fileAttach"]["name"];
$content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
// You add html "Content-type: text/html; charset=utf-8\n" or for Text "Content-type:text/plain; charset=iso-8859-1\r\n" by I.khan
$header .= "Content-type:text/html; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
// User Message you can add HTML if You Selected HTML content
$header .= "<div style='color: red'>" . $message . "</div>\r\n\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n"; // For Attachment
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
if (mail($mailto, $subject, $message, $header)) {
echo "<script>alert('Success');</script>"; // or use booleans here
} else {
//echo mysqli_error();
echo "<script>alert('Failed');</script>";
}
}
Is there anything to add or change in the code?
I am tyring to have a contact form on my website having option for visitor to send attachment with their information. I tried but couldn't succeed. The problem i am having is that the file is sent to the email but the form data and email subject is not sent with the attachment.
HTML:
<form name="form1" enctype="multipart/form-data" method="post" action="do_send.php">
<label>
<input type="text" name="name" id="name" />
</label>
<label>
<input type="text" name="age" id="age" />
</label>
<label>
<input type="email" name="email" id="email" />
</label>
<label>
<input type="file" name="my_file" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>
do_send.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
if($_POST && isset($_FILES['my_file']))
{
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
# Mail headers should work with most clients (including thunderbird)
$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From:".$email."\r\n";
$headers .= "Subject:".$subject."\r\n";
$headers .= "Reply-To: ".$email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $MESSAGE_BODY."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
if ($_POST["email"]!='') {
$ToEmail = 'info#mywebiste.com';
$EmailSubject = 'Website contact form';
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Age: ".$_POST["age"]."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $headers) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
return true;
} else{
echo "<script> alert('Temporary problem, try again!');
window.location='index.html'</script>";
}
}
?>
Kindly point out where i am doing mistake. I am in learning stage please help me solving this issue. Please guide me where to do amendment in the do_send.php so that the form data and email subject is also sent with the attachment.
<?php
$nom = $_POST['lastName'];
$prenom = $_POST['firstName'];
$from_email = $_POST['email']; //sender email
if($_POST && isset($_FILES['my_file']))
{
$recipient_email = 'youremail#exemple.com'; //recipient email
$subject = 'Inscription Etudiant'; //subject of email
$message = 'This is body of the message'; //message body
$message = 'Nom: '.$nom."\n";
$message .= 'Prenom: '.$prenom."\n";
$message .= 'Email: '.$from_email;
//get file details we need
$file_tmp_name = $_FILES['my_file']['tmp_name'];
$file_name = $_FILES['my_file']['name'];
$file_size = $_FILES['my_file']['size'];
$file_type = $_FILES['my_file']['type'];
$file_error = $_FILES['my_file']['error'];
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
die('upload error');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("YADIStudio");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$sentMail = #mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.php';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to youremail#exemple.com');
window.location = 'index.php';
</script>
<?php
}
}
?>
<--! YADI Studio Agency Copyright 2015 -->
<?php
//if there is post
if(isset($_POST) && !empty($_POST)){
//if there is attachment
if(!empty($_FILES['attachment']['name'])){
//store some variables
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['tmp_name'];
$file_type = $_FILES['attachment']['type'];
//get the extension of the file
$base = basename($file_name);
$extension = substr($base, strlen($base)-4,strlen($base));
//only these file type will be allowed
$allow_extensions = array(".doc","docx",".pdf",".zip",".png");
//check if file is allowes
if(in_array($extension,$allow_extensions)){
//mail essentials
$from = $_POST['email'];
$to = "sampleemail#gmail.com";
$replyto = $to;
$subject = "email with attachment";
$message = "this is a random message";
//things you need
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
//standard mail headers
$header = "From " . $from . "\r\n";
$header .= "Reply-To: " . $replyto . "\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declairing we hav e multiple parts of message like text
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format. \r\n";
//plain text part
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
//file attachment
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content. "\r\n\r\n";
//send mail
if(mail($to, $subject, "", $header)){
echo "Mail Sent";
}
else
{
echo "Failed";
}
}
else{
echo "file type not allowed";
}
}
else{
echo "no file posted";
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="text" name="email" value="from" />
<br>
<input type="file" name="attachment" />
<br>
<input type="submit" value="Send Mail" />
</form>
</body>
</html>
Can't find the bug to my code, I am not getting the link to my attachment.. T_T please help. I am not good in boundary.. T_T I tried using phpmailer but can't get it to work, is there any document to read on how to set it up? I really just wanted to make a simple form with attach button for applicant to sent their resumes..
Everything after $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; should be in a $body, not a header.
I would get rid of this: $header .= "This is a multi-part message in MIME format. \r\n";
After $content you actually need to put another line with the mime separator.
$body .= $content . "\r\n" . '--' . $uid . '--';
And finally
if (mail($to, $subject, $body, $header)) {...
My code for sending email
<?php
$to = 'piyu.mulay#gmail.com';
$subject = 'Website Change Request';
$headers = "From: chintamani.mulay#gmail.com \r\n";
$headers .= "CC: chintamani.mulay#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="../1.jpg">';
$message .= '</body></html>';
echo mail($to, $subject, $message, $headers);
?>
When i put link in <img src="http://weber-steinach.de/files/339349"> it works well but when I put path of file which is in my localserver I won't show and Image in email.
Finally I come to know how to do this. Here is the source link from where I come to know
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: youremail#host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<html>
<head>
<style> p {color:green} </style>
</head>
<body>
A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>
</html>'."\n\n".
$bound;
$file = file_get_contents("http://localhost/a/img/1.jpg");
$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;
echo mail($to, $subject, $message, $headers) ;
?>
I know there is lots of information everywhere about this subject but I seem to be going around in circles and would greatly appreciate any help. I am trying to send a simple html email with some inline css. The email does not seem to arrive...
<?php
// Email the user their activation link
$to = "name#website.com";
$from = "me#website.com";
$subject = 'Test HTML Email';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message sent from my website
<br>
<br>
Login in to your account
<br>
<br>
</div>';
mail($to, $subject, $message, $headers);
?>
rewrite this code:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
to this:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
Try to add this :
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n" .
'X-Mailer: PHP/' . phpversion();