I have been trying to make this script where the user info from a form will be sent over to someone by mail and also the same information will save that exact information in a .txt file.
if (isset($_POST['enquire'])) {
//set variables
$name = $_POST['name'];
$mobile = $_POST['phone'];
$email = $_POST['email'];
$cust_message = $_POST['message'];
//set mail vairables
$to = "someone#example.com";
$subject = "Enquiry on ABC";
//message
$message = ' //proper HTML is used removed here to make this code shorter
<h1>New Enquiry on ABC!</h1>
<table cellspacing="0" style="border: 1px solid whitesmoke; max-width: 500px;width:50%;">
<tr> <th>Name:</th><td>'.$name.'</td> </tr>
<tr> <th>Email:</th><td>'.$email.'</td> </tr>
<tr> <th>Mobile:</th><td>'.$mobile.'</td> </tr>
<tr> <th>Message:</th><td>'.$cust_message.'</td> </tr>
</table>';
//set headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: enquiry#example.com" . "\r\n". "CC: someone_else#example.com";
//shoot the mail
mail($to,$subject,$message,$headers);
// write to file
$myfile = fopen("leads.txt", "a+") or die("Unable to open file!");
$txt = "Name: ".$_POST['name']."\r\n"."Email: ".$_POST['email']."\r\n"."Number: ".$_POST['phone']."\r\n";
fwrite($myfile, $txt);
fclose($myfile);
}
One weird thing is I am not receiving any email and nither I am seeing any error Log!
It was a Buggy HTML form.
So in the HTML has a submit button which did not have any name="" attribute. Hence isset() was not true and the script didn't work.
Thank you all for the taking time to answer it.
Related
i have created contact form but i am getting two problems
Reply-To mail is not going.
After Submitting the form page has to redirect to Home page.
For reference please find the attached image and code
Below is the PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name']; // Get Name value from HTML Form
$email_id = $_POST['email']; // Get Email Value
$mobile_no = $_POST['Mobile']; // Get Mobile No
$msg = $_POST['message']; // Get Message Value
$to = "somasekhar.n#vitalticks.com"; // You can change here your Email
$subject = "'$name' has been sent a mail"; // This is your subject
// HTML Message Starts here
$message ="
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>Name: </strong></td>
<td style='width:400px'>$name</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$email_id</td>
</tr>
<tr>
<td style='width:150px'><strong>Mobile No: </strong></td>
<td style='width:400px'>$mobile_no</td>
</tr>
<tr>
<td style='width:150px'><strong>Message: </strong></td>
<td style='width:400px'>$msg</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
// 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: New Contact Form <".$_POST["email"].">\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
$headers .= 'Cc: somumstr210#gmail.com' . "\r\n"; // If you want add cc
// $headers .= 'Bcc: somasekhar.n#vitalticks.com' . "\r\n"; // If you want add Bcc
$headers .= "Reply-To: ".$_POST["email"]."\r\n";
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
}
else{
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
?>
The mail function is deprecated and may not work right! I recommand phpmailer https://github.com/PHPMailer/PHPMailer
How do I make a redirect in PHP?
header("Location: path/to/file");
please check post variables
$variable = $_POST['variable-name'] ?? "default content if $_POST['variable-name'] is undefined";
EDIT: mail() is not deprecated but please use the PHPmailer because it's better
How can I upload to send multiple images via PHP form without using SMTP or login to a spesific Email Account?
My code only works with one input of File type and I want to use 4 inputs to be able to show all 4 images in the mail.
When I try to add more than one File input it gets 'Error in Email sending'.
What should i do?
Here is my code:
<form action="sendmail.php" method="post" name="mainform" enctype="multipart/form-data">
<table width="500" border="0" cellpadding="5" cellspacing="5">
<tr>
<th>Attach Your File</th>
<td><input name="attachment" type="file"></td>
</tr>
<tr>
<th>Attach Your File</th>
<td><input name="attachment2" type="file"></td>
</tr>
<tr>
<th>Attach Your File</th>
<td><input name="attachment3" type="file"></td>
</tr>
<tr>
<th>Attach Your File</th>
<td><input name="attachment4" type="file"></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
<?php
$to = $_POST['toEmail'];
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$subject = $_POST['fieldSubject'];
$message = $_POST['fieldDescription'];
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $fromName";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
?>
It's not very well to post a big bunch of code and ask for analyzing it. ;-)
Generally: Try to write the data stream you want to send to a file and then analyze it - manually or with some kind of mail tool (could even be a mail client which issue warnings). Try to construct manually a valid email with multiple attachments and then try to reproduce the format with your code.
In your case: I see an issue which is not directly related to "issue with multiple files" but an issue about the content type (thus I'm not writing a comment but an answer): charset iso-8859-1 should not be used with "7bit" but with "8bit" or "quoted-printable" encoding.
EDIT1: Fix bad english.
EDIT2: Read your code again. I don't see any \r for CRLF line endings.
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 trying to send a small batch of emails in a loop using PHP mail(). The script to send the emails works fine. There is, however, a slight glitch. Whilst the recipients all receive only one email, the first person on the list receives the email body ($MESSAGE_BODY) once, the second person gets the body twice and the third person gets it 3 times (and on it goes). I cannot for the life of me work out why it's doing it.
The form from which the emails are sent is:
<p>Message Text:
<br />
<textarea name="thebody" id="thebody" cols="65" rows="12"><?php echo $row_email['emailtext'];?></textarea>
<script type="text/javascript">CKEDITOR.replace( 'thebody' );</script>
</p>
<table >
<tr>
<th>Site</th>
<th>Email Address</th>
<th colspan="2">Email Now?</th>
</tr>
<?php
$b = 0;
$q = 1;
while ($row_selfdo = mysql_fetch_assoc($selfdo)) { ?>
<tr>
<td><?php echo $row_seldo[‘sitename’];?></td>
<td><input type="text" name="emailto[]" style="font-size:9px;" size="20" value="<?php echo $row_selfdo['eaddress']; ?>"/></td>
<td valign="middle">Yes:<input type="radio" name="emailnow[<?php echo $b;?>]" value="Yes" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?> /></td>
<td>No:<input name="emailnow[<?php echo $b;?>]" type="radio" value="No" checked="checked" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?>? /></td>
</tr>
<?php $b++; $q++; } ?>
</table>
And here's the script to send the mail
$numb = count($_POST['emailto']);
$num = $numb -1;
$subject=$_POST['subject'];
$thisrecipient = $_POST['emailto'];
$sendtothemnow = $_POST['emailnow'];
for ($a=0;$a<=$num;$a++) {
$emailthemnow = $sendtothemnow[$a];
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}//end email send loop
Any suggestions??
Many thanks in advance
On your first line where you use the message body, set it instead of appending it:
$MESSAGE_BODY = '<p>'.$_POST['thebody'].'</p>';
(The dot has been removed)
Only change the variable name which have conflict.
if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient;
$EmailSubject = $subject;
$mailheader = 'From: me#mydomain.com'."\r\n";
$mailheader .= 'Reply-To: me#mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY_FINAL=wordwrap($MESSAGE_BODY,70);
$mailsent= mail($ToEmail, $EmailSubject, $MESSAGE_BODY_FINAL, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}
I'm having some problems in using php mail function to send a html form's data. The mail function is working fine and I have no problem receiving the email when the form is submitted, but sometimes I got empty arrays in my email.
The code looks like:
<?php
error_reporting(E_ALL & ~E_NOTICE);
$email = $_POST['email'];
//check if value is set:
if (isset($_POST['submit'])){
$everyoneToppingList = substr(implode(', ', $_POST['everyone_platter_topping']), 0);
$everyoneCondimentList = substr(implode(', ', $_POST['everyone_platter_condiment']), 0);
}; //end isset($_POST['submit']
$len = strlen($email);
if ($len > 0)
{
$email_body = "Full Name: $title $fullname\n".
"Topping? - $everyoneToppingList\n".
"Condiment? - $everyoneCondimentList\n\n";
$email_to = "example#gmail.com";
$email_from = $_POST['email']; // required
$URL= "order.php";
header ("Location: $URL");
// create email headers
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email_from \r\n";
$headers .= "Cc: $email_from \r\n";
$headers .= "X-Mailer: PHP/". phpversion();
mail($email_to, $email_subject, $email_body, $headers);
} ?>
And the html part looks like:
<table style="vertical-align:top;" class="form platter bread" cellspacing="2">
<tr>
<span class="bold">
Choose Your Topping:
</span>
<span id="errorsDiv_everyone_platter_topping[]">
</span>
<td height="7" width="87" align="left" valign="middle">
<input type="checkbox" name="everyone_platter_topping[]" value="Lettuce" id="everyone_lettuce" />
<label for="everyone_lettuce">\
Lettuce
</label>
</td>
<td height="7" width="87" align="left" valign="middle">
<input type="checkbox" name="everyone_platter_topping[]" value="Tomatoes" id="everyone_tomatoes" />
<label for="everyone_tomatoes">
Tomatoes
</label>
</td>
</tr>
</table>
When I receive the email, sometimes it only shows "Topping? - , , , ," .
Really need help on this and any suggestions will be appreciated!!
Thank you!
I am sorry to write it over here, but comment section is not enough to mention the doubts regarding your question.
the elements in the form and the elements used in PHP code does not give clear picture of the question.
Also $URL= "order.php";
header ("Location: $URL"); does not make any sense, it will redirect and mail wont be sent anyways
If I make this subtle changes of
removing header code from the PHP code
adding the email input box and submit submit button in the html form
adding form outside the table with method POST
I simply got this code working by making above changes without any trouble.
I suggest you edit a question and ask again or do the suggested changes (of course with some tweaking from your side) and you will code be in working state.
First initialize your variables before using em.
$everyoneToppingList = null;
$everyoneCondimentList = null;
if (isset($_POST['submit'])){
$everyoneToppingList = substr(implode(', ', $_POST['everyone_platter_topping']), 0);
$everyoneCondimentList = substr(implode(', ', $_POST['everyone_platter_condiment']), 0);
};
$len = strlen($email);
if ($len > 0)
{
if (!is_null($everyoneToppingList) && !is_null($everyoneCondimentList))
{
// Send your mail.
}
}