Send Email To Multiple (Each Line Email Address) and Print - php

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);
?>

Related

Using PHP for email file in HTML

I've been having problems using php in my html form. While it will send, the $_POST variables are empty when I try to grab them in the php file. Any ideas on what I could be doing wrong?
My HTML code:
<form class="submitAMessage" name="Submit a Message" method="post" action="sendresults.php">
<div>
<h4>Submit a Message:</h4>
<label for="name">Name:<br><span class="required"></span></label>
<input type="text" id="name" name="name" placeholder="Your name" required="required" />
</div>
<div> <br>
<label for="email">Email Address:<br><span class="required"></span></label>
<input type="email" id="email" name="email" placeholder="your#email.com" required="required" />
</div>
<div> <br>
<label for="message">Message:<br><span class="required"></span></label>
<textarea id="message" name="message" placeholder="Write your message here." required></textarea>
</div>
<div>
<input type="submit" id="submit" name="submit" formmethod="POST" value="Submit" />
</div>
</form>
My php file:
<?php
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Results from Contact Form';
$emailfrom = 'noreply#website.com';
// Your email address. This is where the form information will be sent.
$emailadd = 'website#gmail.com';
// Where to redirect after form is processed.
$url = 'http://www.website.com/main.html';
// Makes all fields required. If set to '1' no field can not be empty.
// If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Results from Form:\n\n";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$line = '
';
mail($emailadd, $subject, $text.$name.$line.$email.$line.$message, 'From: '.$emailfrom.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
The only thing that sends in the email is:
Results from Form:
Any help is appreciated, thanks in advance!
You need to pass the headers into the mail function which is option.
Here is the functions all parameters
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$to Receiver, or receivers of the mail.
$subject Subject of the email to be sent.
$message Message to be sent.
$additional_headers this is the optional headers which is used for the mail options
you can to set the following values in headers.
// header configuration for to send the HTML mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
$additional_parameters The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail
You should use the header in mail function. Add following code in in your code too.
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
mail($emailadd, $subject, $text.$name.$line.$email.$line.$message,$header, 'From: '.$emailfrom.'');
Good luck.

PHP contact form sends message through senders address

Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.

How do you use PHP to send form data as a message?

I need to have the visitor of my website send form data (such as a contact form to my email via PHP. How will I be able to do this?
With GET/POST query and using tag
html forms / w3schools
for example
.html page:
<form method="GET" action="send.php">
<input name="fieldname" type="text" placeholder="You text here...">
<input type="submit" value="Submit">
</form>
send.php
<?php
if (isset($_GET['fieldname']) {
// you code here..
}
example send email by function mail()
about mail() function on php.net
$from = 'fromemailsend#mail';
$to = 'emailtosend#mail';
$subject = 'your subject';
$message = 'your<br>message<br>in html code';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= 'To: Author <' .$to . ' >' . "\r\n";
$headers .= 'From: '.$author.' <'.$from.'>' . "\r\n";
mail($to, $subject, $message, $headers);

$_POST - results to be sent in a email

<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.

how to change default mailed by : address in php mail()

i have the following code
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Domain Name <domain#domain.com>' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
and it send mail correctly but when i see details in the email in gmail ...
it shows
from Domain Name
to myemail#myemail.com
date Tue, May 25, 2010 at 12:41 PM
subject my subject here
mailed-by mars.myhostingcompany.net
while i want to show my own address in mailed by section so that it should be mydomain.com instead of mars.myhostingcompany.net
There are two types of sender (From), the MIME header sender and the envelope sender.
You send the MIME sender with in the headers in the 4th parameter of the mail() function. You are doing this fine.
The enveloper sender (the one you can send when sending email through sendmail or a sendmail compatible wrapper) with the -f flag, is set in the 5th mail() parameter, additional_parameters, in the format you'd pass it on the command line: -femail#address.tld.
So your mail function would end up looking like this:
mail($to, $subject, $body, $headers, "-fdomain#domain.com");
I take it you're on shared hosting so the reason it shows your hosts email address is because when you configure PHP there is a setting called "sendmail_from" which is a default address to send mail through in the event that no address is provided in your code.
You appear to be specifying the proper headers in your code so I can only think of one possibility (that I can't test from this computer). Try removing the < > around your email address - it may be trying to read that as HTML and so you have nothing. This can occur on Windows machines because PHP itself parses the custom headers and not the MTA (message transfer agent) and PHP treats any < > as HTML.
I realize that it doesn't look as professional (since the email client won't show the name when it receives the email) but if you're running from a Windows machine there's very little else you can do unless you switch to an alternative mail package.
$subject = "Subject Here";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: domain#domain.com' . "\r\n";
$to = $email;
$body = '
My Message here
';
mail($to, $subject, $body, $headers);
//FORM PAGE:
<form method="POST" action="mailer.php">
<p>Please feel free to contact me on the form below or my direct email address: jkench#jasonkench.co.uk<br>
<br><br>
<br>
<br>
<br>
</p>
<table width="327" border="0">
<tr>
<td width="102">Name:</td>
<td width="215"><input type="text" name="name" size="19"></td>
</tr>
<tr>
<td>Company:
<label for="company"></label></td>
<td><input type="text" name="company"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" size="19"></td>
</tr>
<tr>
<td>Telephone No:
<label for="telephone"></label></td>
<td><input type="text" name="telephone"></td>
</tr>
</table>
<p><br>
Enquiry:<br>
<textarea rows="9" name="message" cols="65"></textarea>
<br>
<br>
<input type="submit" value="Submit" name="submit">
</p>
</form>
//PHP MAILER PAGE
<?php
if(isset($_POST['submit'])) {
//SEND TO
// Send the completed form to the below email address:
$to = "myemail#mydomain.co.uk";
//SUBJECT
// Subject of the email form:
$subject = "Jason Kench - Web Developer";
//NAME
//POST the details entered into the name box
$name = $_POST['name'];
//COMPANY NAME
//
$company = $_POST['company'];
//EMAIL
//
$email = $_POST['email'];
//TELEPHONE NUMBER
//
$telephone = $_POST['telephone'];
//MESSAGE/ENQUIRY
$message = $_POST['message'];
//Headers from a online site may help not sure
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//FROM EMAIL ADDRESS:
// Additional headers to change the FROM EMAIL ADDRESS
$headers .= 'From: Web-Contact-Form#mydomain.co.uk' . "\r\n";
// BODY
// This is the body of the message that will be sent to my email address with their details.
$body = "
You have received a message from the online contact form at http://www.jasonkench.co.uk\n
Details Below: \n \n
From: $name\n
Company: $company\n
$headers
Email Address: $email\n
Telephone No: $telephone\n
Message: $message\n";
// FORM SENT
// This will alert the customer the form has been successfully sent.
echo "Your details have been sent, I will contact you within 48 hours.";
// Use the mail function to email the following variables to my $to email address.
mail($to, $subject, $body, $headers);
} else {
// Display error message if there is a problem.
echo "Sorry there seems to be a problem. Please email me direct at: $to thank you.";
}
?>

Categories