<textarea rows="4" cols="70" name="filename" id="result" style="background:#B0D2D7;
width:100%;overflow:auto;resize:none"
readonly><?php echo $_POST['filename']; ?></textarea>
Hi all, this is a snippet of code I'm using. What I'm struggling with is how to send
the $_POST results that get displayed on the next page into an email using PHP email.
The results are not displayed in a text box as such on the next page but displayed more like a print_pr into a PHP form.
Any help would be great!
To move data across into another page via POST, the easiest way is to wrap your textarea in a form and add a submit button:
<form action="?" method="post">
<textarea name="filename"></textarea>
<input type="submit" />
</form>
<?php
if(isset($_POST["filename"]))
{
echo $_POST["filename"];
}
?>
You also made a vague reference to sending an email, which can be done using the mail() function.
<?
$g_mail = "mail#domain.com";
$s_name = "Some name";
$to = "Receiver <receiver#domain.com>";
$subject = "Some subject";
$message= "HTML codes here. Write anything you want including " . $_POST['data'];
$header = "From: $s_name <".$g_mail.">\n";
$header .= "Reply-To: $s_name <".$g_mail.">\n";
$header .= "Return-Path: $s_name <".$g_mail.">\n";
$header .= "Delivered-to: $s_name <".$g_mail.">\n";
$header .= "Date: ".date(r)."\n";
$header .= "Content-Type: text/html; charset=iso-8859-9\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Importance: Normal\n";
$header .= "X-Sender: $s_name <".$g_mail.">\n";
$header .= "X-Priority: 3\n";
$header .= "X-MSMail-Priority: Normal\n";
$header .= "X-Mailer: Microsoft Office Outlook, Build 11.0.5510\n";
$header .= "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869\n";
mail($to, $subject, $message, $header);
?>
This is the mail script I generally use. It will not be marked as spam on most mail providers aswell.
Related
<?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)) {...
I am not good at PHP but still trying to create a test script so that i can learn it. I am referring to w3schools and i don't know how good or bad i am.
I need some changes to be made to what script i just created.
<?php
$to = $_POST['EmailList'];
$subject = $_POST['EmailSubject'];
$message = $_POST['EmailBody'];
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <admin#admin.com>' . "\r\n";
mail($to,$subject,$message,$headers);
print_r($headers)
?>
<html>
<center>
<form method="post">
<br><strong>PHP Email Sender</strong><br><br><br>
Email List<br>
<textarea name="EmailList" placeholder="email#email.com (New Email Each Line)" rows="20" cols="50"></textarea><br><br>
Subject<br>
<input type="text" name="EmailSubject" placeholder="Your Subject Goes Here"><br><br>
Body<br>
<textarea name="EmailBody" placeholder="Write your content (HTML Accepted)" rows="20" cols="50"></textarea><br><br>
<input type="submit" value="Submit!">
</form><br><br>
</center>
</html>
I want some help so that i can send email's to different email's but each email would be in a different line and i would not be using a comma(,). I want the script to generate the comma(,) by its own and carry each email from a new line.
For example i entered 10 emails i need each email to be printed and a message saying sent besides that.
Please let me know if this is possible. I just need some help.
This is tested:
<?php
$subject = $_POST['EmailSubject'];
$message = $_POST['EmailBody'];
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <admin#admin.com>' . "\r\n";
$emailList = explode("\n",$_POST['EmailList']);
if(count($emailList) > 0){
foreach($emailList as $to){
$to = trim($to);
$sent = mail($to,$subject,$message,$headers);
if ($sent){
echo "<p>Sent: $to</p>";
}
else{
echo "<p>Not Sent: $to</p>";
}
}
}
else{
echo "<p>No email addresses</p>";
}
print_r($headers);
?>
I'm having some problems with my contact page.
Here are the parts:
<form method="post" action="mail.php">
<input name="nome" type="text" style="width: 265px;" placeholder="Nome e Cognome">
<input name="mail" type="email" style="width: 263px;" placeholder="E-mail">
<textarea name="messaggio" placeholder="Messaggio"></textarea>
<button type="submit" name="invia" style="margin-left: 0; margin-top: 10px;">Invia</button>
</form>
and..
<?php
$to = "mail";
$subject = "Modulo proveniente dal sito www.miosito.it";
$body = "Contenuto del modulo:\n\n";
$body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n";
$body .= "Email: " . trim(stripslashes($_POST["mail"])) . "\n";
$body .= "Messaggio: " . trim(stripslashes($_POST["messaggio"])) . "\n";
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
if(#mail($to, $subject, $body, $headers)) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
?>
First of all.. if I click on the submit button with all blank the email is sent blank.
Even if I make some errors and I get the else message the email is sent.. blank obviously.
I'm going crazy.. I'm making this website for free for a friend but I'm a graphic designer not a web developer. Never again! :D
Help!! Thank you very much.
put this line in your headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Before sending mail you have to validate all the fields whether they are empty or not
Like this
if( trim($_POST["nome"]) != "" )
{
// send mail
$isMailSend = mail($to, $subject, $body, $headers);
}
{
//show error
}
if( $isMailSend ) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
This
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
Should be,
$headers = "From: Valle srl <info#vallesrl.com>";
$headers. = "Content-Type: text/html; charset=iso-8859-1\n";
you can't use type="email",there should be type="text"
I am making a PHP form which allow users to upload attachment and send it to my e-mail. I have been searching for quite a long time to make it.
And, finally, I found this. http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/. It works fine.
However, when I modify it myself (change the fields), something doesn't go well.
<?php
$location=$_POST['location'];
$name_ha=$_POST['name_ha'];
$name_person=$_POST['name_person'];
$email=$_POST['email'];
$date_sent=$_POST['date_sent'];
$date_completed=$_POST['date_completed'];
$date_received=$_POST['date_received'];
$to="admin#admin.com" . "$email";
$message="to";
//*** Uniqid Session ***//
$sid = md5(uniqid(time()));
$header = "";
$header .= "From: ".$_POST["name_ha"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$sid."\n";
$header .= "Content-type: text/html; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $message."\n\n";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$FilesName = $_FILES["fileAttach"]["name"];
$Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$header .= "--".$sid."\n";
$header .= "Content-Type: application/octet-eam; name=\"".$FilesName."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$FilesName."\"\n\n";
$header .= $Content."\n\n";
}
$flgSend = #mail($to,"A new file for you!",null,$header); // # = No Show Error //
if ($flgSend)
{
echo "Mail sent.";
}
?>
The files I downloaded from shotdev.com and the one I modified are being hosted on the same server and under the same folder.
But, for the one I have modified, the email is sent before the attachment is uploaded (~ 45% of the uploading process) if the file size is greater than 1MB. The email I received, there is no attachment and no information of sender ($name_ha).
On the other hand, for the files downloaded on shotdev.com, the email will only be sent after the attachment is uploaded completely no matter how big it is.
Is there any error of the script, or something is missing, causing such an occurrence? Your time and help is much appreciated.
First, in code that you showed as example you have error on this line:
$to="admin#admin.com" . "$email";
Change it to:
$to="admin#admin.com, " . "$email";
Notice comma that you are missing... It needed to be there for every new receivers email you want to add on that particular way...
Also I have tested code on my server, everything works well.
I have 100 mbps upload and tried file that is 4.5 MB and it works.
So maybe there is a problem with your upload speed and allowed execution time of php script that you are calling to send this email.
Try to add the following just after <?php in php file that you setted in HTML form as action="something.php":
set_time_limit(0);
That means that there is no liimt in execution time of script, change 0 to desired number of seconds...
Also remove # before #mail() function if there are errors you need to see them, hiding them will not do any good for you.
EDIT:
I have altered your code so that it does check if, firstly if there is file, if not sends mail without attachment. And if there is file it checks if it is encoded to base64 and splited and in that case it send the mail with attachment...
<?php
$location=$_POST['location'];
$name_ha=$_POST['name_ha'];
$name_person=$_POST['name_person'];
$email=$_POST['email'];
$date_sent=$_POST['date_sent'];
$date_completed=$_POST['date_completed'];
$date_received=$_POST['date_received'];
$FilesName = $_FILES["fileAttach"]["name"];
$to = "admin#mail.com," . "$email";
$message = "to";
$sid = md5(uniqid(time()));
$header = "";
$header .= "From: ".$_POST["name_ha"]."<".$_POST["email"].">\nReply-To: ".$_POST["email"]."";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$sid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$sid."\n";
$header .= "Content-type: text/html; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 7bit\n\n";
$header .= $message."\n\n";
if($_FILES["fileAttach"]["name"] != "") {
$Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$header .= "--".$sid."\n";
$header .= "Content-Type: application/octet-stream; name=\"".$FilesName."\"\n";
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$FilesName."\"\n\n";
$header .= $Content."\n\n";
}
if (strlen($FilesName) > 0) {
if ($Content) {
$flgSend = mail($to,"Here is that file",null,$header);
}
else {
echo "problem with file...";
}
}
else {
$flgSend = mail($to,"Here is that file",null,$header);
}
if ($flgSend) {
echo "Mail sent.";
}
?>
HERE IS HTML FOR IT:
<html>
<head>
</head>
<body>
<form method="post" action="sender.php" enctype="multipart/form-data" >
location: <input type="text" name="location" />
<br />
name_ha: <input type="text" name="name_ha" />
<br />
name_person: <input type="text" name="name_person" />
<br />
email: <input type="text" name="email" />
<br />
date_sent: <input type="text" name="date_sent" />
<br />
date_completed: <input type="text" name="date_completed" />
<br />
date_received: <input type="text" name="date_received" />
<br />
file: <input type="file" name="fileAttach" />
<br />
<input type="submit" value="submit" />
</form>
</body>
</html>
I am passing a hidden variable from HTML to a PHP page. In the PHP page I want to use that variable in my form.
<?php
$newVar=trim($_POST['newVar']);
$subject = "new var is ---> $newVar";
?>
I am not able to use $newVar in any if statement - it shows blank. If I try to echo $subject, it shows the value of $newVar but when I try to echo the value of $subject in any if statement it does not show me the value of $newVar.
My Code is:
html markup is:
New Variable
<form method="post" name="sendVar" action="test.php" style="display:none;">
<input type="hidden" name="newVar" value="">
<input type="submit" value="Send form!">
</form>
php:
<?php
$newVar=trim($_POST['newVar']);
$subject = "new var is ---> $newVar";
?>
if(isset($_POST['submit'])) {
if(!isset($hasError)) {
$from_add = "test#test.com";
$emailTo = 'shruti#example.com';
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
mail($emailTo, $subject, $headers);
// HERE in $subject.. value of newVar is not displaying
$emailSent = true;
}
$from_add = "do-not-reply#example.com";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
}
?>
You seem to have ended a PHP code section with ?> and not started a new one with <?php - although I shall assume that this is simply a copy/paste error since you are not complaining that you can see your PHP source code in the browser.
The actual problem you have here is that you are checking if $_POST['submit'] is set, but it never will be because you didn't name any of your form controls submit, so none of the code in the if block will ever be executed.
In your HTML, change:
<input type="submit" value="Send form!">
...to:
<input type="submit" name="submit" value="Send form!">
...and it should work.