This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 5 years ago.
after I tried to get an answer to my question here, I try it with a new thread.
Little explanation:
I have a Contact-Form with 180 input fields + calculation for every field.
I want to send all the fields + the calculated value with e-mail.
Thats just an example of one of the 180 rows which i need to send with mail:
<form id="wohnzimmer" method="post" action="mailto:myemail#mymail.com">
<div style="clear: left;">
<div class="text">
DIV TEXT - ANY ARTICLE
</div>
<div class="restText">
<input id="w0" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="ARTICLENAME" /> = <span class="raumeinheitenErgebnis" id="w0g"> CALCULATED NUMBER </span>
</div>
</div>
// 179 more input-fields will follow here!!
<input type="submit" value="Send Email" />
My Calculation is working, so i just need help to send all my content via mail.
My question is:
How can i send the mail without Outlook or Thunderbird (i think i need php)
with the following Content:
Name of the article , the Number from the Input field (w0) + the calculated number (w0g)?
I hope anyone has an answer for me.
Thanks in advance.
You need to create a PHP file to handle these "server-side" actions for you. Then set the action attribute of your HTML form to this PHP page. When the HTML is submitted, the 180 input fields are then all POSTed to the PHP page inside a variable called $_POST. You can then work on that data to create the string you want and finally use the mail() function (or perhaps a pre-built emailer package that gives you a bit more control) to actually send that email.
Your new HTML
<form id="wohnzimmer" method="post" action="send_email.php">
Note:
You say you want to get the name of the article, w0 and w0g, but you have only put w0 inside an input. Only inputs, textareas and selects will be sent to the PHP script. You will need to change your HTML to make sure they are all gathered. I'd suggest using array syntax to do this:
<input type="hidden" name="article0" value="ARTICLENAME" />
<input id="w0" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="w0" /> = <input type="text" class="raumeinheitenErgebnis" name="w0g" id="w0g"> CALCULATED NUMBER </input>
<input type="hidden" name="article1" value="ARTICLENAME" />
<input id="w1" type="text" class="raumeinheitInput_x4 inputWidth" value="0" name="w1" /> = <input type="text" class="raumeinheitenErgebnis" name="w1g" id="w1g"> CALCULATED NUMBER </input>
I'm making some presumptions here about your data but that should make sense. You may want to write a PHP loop to output the data if you can. Also it might help you to use HTML input arrays to simplify things a bit.
The PHP
You'd end up with something like this very rough example:
<?php
$myString = "";
for ($x=0;$x<180;$x++) {
$tempString = $_POST['article' . $x] . $_POST['w' . $x] . $_POST['w' . $x . 'g'];
// don't forget to sanitize this data!!
$myString .= sanitize_however_you_want($tempString);
}
// now email
mail('myemail#mymail.com', 'Email Subject', $myString, 'From: you#yoursite.com' '-fyou#yoursite.com');
Read more about posting forms here: Dealing with forms
Read more about sending email here: The mail() function
Read more about HTML input arrays in this stack question
you can use a mail() (http://php.net/manual/en/function.mail.php ) but i think is better if you use a PHPMailer library (https://github.com/PHPMailer) is
very simple
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Related
I have searched all around but cannot find a specific answer. I have a form there the user can put in MULTIPLE attachments. I am using PHPMailer to send the submitted form. I have looked everywhere and it seems people who explain how to upload multiple files but NOT from user input. THANK YOU!.
My attachment upload button name is "attachments"
if (isset($_POST["submit"])) {
$optionOne = $_POST['optionOne'] ;
$tick = $_REQUEST['tick'] ;
$results = $_REQUEST['results'] ;
$option = $_POST['option'] ;
$option = $_POST['requirements'] ;
$mail = new PHPMailer;
//Server settings
$path = 'upload/' . $_FILES["attachments"]["name"];
move_uploaded_file($_FILES["attachments"]["tmp_name"], $path);
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isSendmail(); // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bonnie'; // SMTP username
$mail->Password = 'bonnie'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('bonniethompson12345#gmail.com', 'Mailer');
$mail->addAddress('bonniethompson12345#gmail.com'); // Add a recipient
$mail->isHTML(true);
$mail->AddAttachment($path); // Set email format to HTML
$mail->Subject = 'Form submission';
$mail->Body = 'This course is identified in my Work Plan and Learning Agreement: $optionOne \n \n I am attending this session because: $tick \n \n What would you like to achieve as a result of your attendance: $results \n \n Do you require adjustments or additions to the session delivery to support your participation: $option \n \n Please provide details of your requirments: $requirements</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->AddAttachment('Logo.png');
if(!$mail->send()) {
echo "Failure to send";
} else {
echo "Message has been sent successfully";
}
}
<p>Please upload any supporting documentation to support your registration request </p>
<div class="browse-button">
<input type="file" name="attachments" multiple="multiple"></input>
</div>
The thing you're missing is the naming of the input element; you need to use array naming to make it handle multiple files correctly, so build it like this:
<input type="file" name="attachments[]" multiple="multiple">
The important bit is the [] at the end of the element name, which means that the multiple values will get submitted as an array. When you receive a submission from that, try a var_dump($_FILES) so you can see what you've got. You should also check the return value of move_uploaded_file() rather than assuming it works. Also you don't need a closing </input> tag if you're using HTML5.
The multiple file upload example provided with PHPMailer demonstrates exactly what you're asking, and includes the error checking I mentioned.
It's also covered in the PHP docs. Look harder next time!
I am new to php and currently doing a project
I stored data in database by using a form.
I have a mail function, when on clicking the submit button, a mail is send to reputed email id. I am using phpmailer library.
This is my mail function
class mail
{
public function sendMail(){
require 'vendor/autoload.php';
$mail = new PHPMailer(); // Passing `true` enables exceptions or null without exception
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xyz#gmail.com'; // SMTP username
$mail->Password = 'xyz123'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('xyz#gmail.com', 'xyz');
$mail->addAddress('abc#gmail.com', 'abc');
$mail->addCC('zxc#gmail.com');
//Attachments
$mail->addAttachment('lob.png', 'sample.png'); // Add attachments
// Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Report';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if (!$mail->send()) {
return "Error sending message" . $mail->ErrorInfo;
} else {
return "Message sent!";
}
}
}
This is my code to store data into database as send.php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$quality = $_POST['quality'];
$sql = "INSERT INTO user(name,age,quality) VALUES('$name','$age','$quality')";
}
This is my html form
<form class="quality" method="POST" action="send.php">
<label for="name">Name</label>
<input type = "text" class = "form-quality" name="name" value="" / >
<label for="age">Age</label>
<input type="text" class ="form-quality" name="age" value="" / >
<label for="quality">Quality</label>
<input type="ratio" class ="form-quality" name="quality" value="E" / >
<input type="ratio" class ="form-quality" name="quality" value="P" / >
</form>
1) How to get the data's from database ?
2) In mail function previously sending emails to one or two id's, but i need as when i select "value = E" from ratio button , it should send to one email and if i select "value= P" it should send to another email based on user selected values stored in the database
Anyone with a best reply will be much more needed help for me
Assuming that when you said you are doing a project is within an education environment and not for professional purpose:
When you get the data from you form on if(isset($_POST['submit'])) send those variables to the sendMail function as parameters, then inside the function you can make a variable using your parameters to build a well formed message that you can send in your $mail->Body .
Inside the function you can also check if what you recieved on your radio-button input was "E" or "P" and give different values (receivers email) and pass it to your addAddress.
If this is not a school project you MUST be very carefully on what you send or you insert to the db. Validate everything you recieve on your form before sending and go ahead and check the link in the comments about SQL injection.
This should've been a comment insetead of a reply but i don't have enough reputation to comment so sorry about that.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have created a form that allows people to send an email from a website. The HTML code (see attached) calls a PHP script (see attached), and the email is supposed to be sent. The webpage displays the message "Email successfully sent", but I never actually received the email (it's not in spam either).
I have reached out to my hosting service (awaiting reply) to check whether PHP is supported or not. In the meantime, I would like to ensure that my code has no errors.
HTML:
<form action="message.php" method="post">
<fieldset>
<p>Name <span class="requiredAsterisk">*</span></p>
<input name="name"/>
<p>Email <span class="requiredAsterisk">*</span></p>
<input name="email"/>
<p>Message <span class="requiredAsterisk">*</span></p>
<textarea name="message"></textarea>
</fieldset>
<fieldset>
<input class="sendMessage w3-large" type="submit" value="Send Message"/>
</fieldset>
</form>
PHP:
<?php
$header = 'From: ' .$_POST['name'] ."\r\n" .'Reply-to: ' .$_POST['email'] ."\r\n" .'X-Mailer: PHP/' .phpversion();
if (mail("email#mail.com", "Email from website", $_POST['message'], $header)) {
echo ("<p>Email successfully sent</p>");
} else {
echo ("<p>Email failed</p>");
}
?>
Thanks in advance
A good alternative to the original mail() function in PHP would be something like PHPMailer. As stated in the github page:
the vast majority of code that you'll find online that uses the mail()
function directly is just plain wrong! Please don't be tempted to do
it yourself - if you don't use PHPMailer, there are many other
excellent libraries that you should look at before rolling your own -
try SwiftMailer, Zend_Mail, eZcomponents etc.
PHPMailer is super easy to set up and get going. This is the basic syntax to send an email:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net'); // Add a recipient
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
(I would probably comment this, but my rep is too low) You should use
$message = wordwrap($_POST['message'], 70, "\r\n"); so your message follows the documentation on mail(). Besides this, your code seems to be fine. As you mentioned, it is probably related to your hosting provider, as it is common place for them to block the mail function. Also see this question on it for some extras which may the cause of your problem.
Hi I have this PHP script that I found on a blog
<?php
if(isset($_POST['submit'])) {
$to = "youremail#gmail.com";
$subject = "Forms";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";
}
?>
Which is run when the following is executed in HTML
<form method="POST" action="mailer.php">
Your Name<br>
<input type="text" name="name" size="19"><br>
<br>
Your Email<br>
<input type="text" name="email" size="19"><br>
<br>
Message<br>
<textarea rows="9" name="message" cols="30"></textarea>
<br>
<br>
<input type="submit" value="Submit" id="submitBTN" name="submit">
</form>
According to the blog all i have to do is put the html and php files onto my web server (Which i don't have so i can't test this). Will it send an email to the email specified in $to ? I've never used PHP but this doesn't really makes sense how it can just email someone once its on the web. Thanks for an explanation/if this script would work straight up!
This should theoretically work, but you will have to configure PHP to use your email server. I would recommend using something like PHPMailer to send email, that is what I always do. PHPMailer allows you to specify your IMAP/POP3 email server host, username & password, and email port, in the same manner that your email client does.
Here is a link to information about using Gmail in PHPMailer.
This snippet (taken from the PHPMailer readme) shows how to configure your server:
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
Now, you can configure header information for where the email is from, and who it is going to:
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
You can even add attachments to the email:
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
Finally, we put the subject and body into the email:
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
Now, we send the email:
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You will need to setup a mail server on your server in order to send emails.
Not sure where you need to specify this though for PHP to use..
Take a look here: php mail setup in xampp
(Xampp mentioned in the link is a program that acts as the server so that people can look at your webpages online)
Xampp: https://www.apachefriends.org/index.html
I did the same task long time ago.
I accomplished this trough XAMPP (php and apache) and gmail.
Here you can find a video with the complete explanation.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I have a very simple contact form and a very simple php mail script. But when I tried to send it to the specified email address, I did not receive the test mail.
Below are my codes:
HTML Form
<div class="form" >
<form action="mail.php" method="post">
Email: <input type="text" name="email" size="38"><br>
詢問主旨:<input type="text" name="subject" size="36"><br>
<div class="queryTitle">
詢問內容
</div>
<br>
<textarea name = "message" rows="6" cols="37">
</textarea>
<br>
<input name = "submitted" type="submit" value="傳送">
</form>
</div>
Php script:
<?php
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From: ".$_REQUEST['email']."\r\n"; //Site name
// Change this to your email address you want to form sent to
$to = "verymeanguy2#gmail.com";
$subject = $_REQUEST['subject'];
$message = $from." ".$_REQUEST['message'];
mail($to,$subject,$message,$from);
}
}
?>
Could it be that Gmail blocked my mail? If so, how can I devise a script that can send mails to the popular mails?
Thanks in advance!
Jason
PS: I am hosting mine on heliohost's free Stevie server, if that accounts for something.
lolka_bolka: yes, mail() was called and adding an echo in front of mail function prints 1. And it's not in spam. Len_D: it did print anything, so I think mail was called and returned true. Anthony: how do I ensure that?
I just want to report back my testing. It seems that Gmail blocks Yahoo mail address sender for some reason. When the header is a Gmail address or even a bogus made-up address, Gmail can receive no problem. Yahoo mail on the other hand can receive mail with no problem at all. Anyone can shed a light on that?
When mail returns true (or 1), it means it did correctly what it tried to do. But doesn't always mean it sent mail at all.
the mail() funciton uses "sendmail" to actually send the email.
If sendmail is configured to send mail, then that's what will happen (it everything went ok).
However, by defult, sendmail stores mail in the server. It "simulates" a real sendmail.
From my personal experience, I recommend PHPMailer. It's easy to use, it's much easier to check for errors (check if the email was sent, and if not, get infomation about the problem).
Example:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->From = "email#domain.com";
$mail->FromName = "auto";
$mail->AddAddress("example#example.net", "Name of example user"); //send to....
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = "=?UTF-8?B?".base64_encode(stripslashes($asunto))."=?=";
$mail->Body = stripslashes($mensaje);
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
return true;
} else {
echo "Message has been sent";
return false;
}
Hope it helped :)