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.
Related
OK, so I have done a substantial amount of research over the last several days and am stumped. I have XAMPP and PHP Mailer and I know they are both working correctly locally.
I also have an HTML form that I know is working correctly using Hostgator web hosting. This HTML form calls a .php file named send_email.php which sends the form contents to my email.
My questions are:
What is the new .php code (roughly speaking) to send this HTML form on my local host (XAMPP) using PHP Mailer?
How do I call the new .php file in my HTML file?
What is the file tree structure that I need for both this HTML form and the new .php file which PHP Mailer will use to send it?
Please note: I've changed some of details in my code so I'm not including my actual email address and person info.
Here is my form:
<!-- THE SUBMISSION FORM -->
<div class="container">
<form id="contact" action="send_email.php" method="post">
<h3>Apply Today!</h3>
<h4></h4>
<fieldset>
<input placeholder="Full Name" type="text" name="name" tabindex="1" required autofocus>
</fieldset>
<fieldset>
<input placeholder="Telephone Number" type="tel" name="telephone" tabindex="2" required>
</fieldset>
<fieldset>
<input placeholder="Email Address" type="email" name="email" tabindex="3" required>
</fieldset>
<fieldset>
<input placeholder="Subject" type="text" name="subject" tabindex="4" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your Message Here...." name="message" tabindex="5" required></textarea>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending" tabindex="6">Submit</button>
</fieldset>
</form>
</div>
Here is the send_email.php file I am using (which I know works)
<?php session_start();
if(isset($_POST['submit'])) {
$from = "sender#gmail.com";
$to = "receiver1#gmail.com";
$to2 = "receiver2#gmail.com";
$to3 = "receiver3#gmail.com";
$subject = "NEW LEAD!";
$message =
"|---------BEGIN TRANSMISSION----------|" . PHP_EOL .
PHP_EOL . "The person that contacted you is: ". $_POST['name'] .
PHP_EOL . "E-mail: " . $_POST['email'] .
PHP_EOL . "Telephone: " . $_POST['telephone'] .
PHP_EOL . "Subject: " . $_POST['subject'] .
PHP_EOL . "Message: " . $_POST['message'] . PHP_EOL .
PHP_EOL . "|---------END TRANSMISSION----------|";
$headers = "From:" . $from;
echo "Thank you for contacting us. We will will be in touch.<br/>Go to <a href='/index.php'>Home Page</a>";
mail($to, $subject, $message, $headers);
mail($to2, $subject, $message, $headers);
mail($to3, $subject, $message, $headers);
} else {
echo "You must write a message. </br> Please go to <a href='/index.html'>Home Page</a>";
}
?>
Here is my index.php file in my PHPMAILER folder (which I know works).
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
//$mail->Host = gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'sender#gmail.com'; //SMTP username
$mail->Password = 'sender-password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('receiver1#gmail.com', 'Mailer');
$mail->addAddress('receiver2#gmail.com', 'Dylan');
//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. Mailer Error: {$mail->ErrorInfo}";
}
This is my location for PHP Mailer on XAMPP:
c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/phpmailer
This is the location for my HTML form and send_email.php :
c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/WEBSITE
You should not have to change the code at all; it should work equally well wherever it's run from. So long as you're using a sending mechanism that your hosting provider allows, e.g. that they don't block outbound SMTP, it should work fine. Have you tried it?
Aside from that, your file structure looks a bit unusual. Composer installs PHPMailer for you and automatically loads it when you ask for it, so composer is also responsible for where it is stored; you don't need to make your own phpmailer folder. If you're hosting a single site, it's probably not necessary to put things in subfolders of your web root, so you can use a flatter structure like:
c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/
index.php
send_email.php
vendor/
composer/
phpmailer
If you are hosting multiple sites you would typically have multiple folders within .../htdocs/, each of which will have a similar internal structure.
You have 0 knowledge of php
FIRST OF ALL, When you use .php files you can use html in it
You think this is a joke ?
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
//$mail->Host = gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'sender#gmail.com'; //SMTP username
$mail->Password = 'sender-password'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('receiver1#gmail.com', 'Mailer');
$mail->addAddress('receiver2#gmail.com', 'Dylan');
//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. Mailer Error: {$mail->ErrorInfo}";
}
This is copy and pasting to a new LEVEL !
$mail->setFrom('receiver1#gmail.com', 'Mailer');
$mail->addAddress('receiver2#gmail.com', 'Dylan');
You need to set it up
THE WHOLE CODE IS WRONG!
Go to Home Page";
mail($to, $subject, $message, $headers);
mail($to2, $subject, $message, $headers);
mail($to3, $subject, $message, $headers);
} else {
echo "You must write a message. Please go to Home Page";
}
?>
Last of all
This is my location for PHP Mailer on XAMPP:
c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/phpmailer
This is the location for my HTML form and send_email.php :
c:/Users/dylan/.bitnami/stackman/machines/xampp/volumes/root/htdocs/WEBSITE
YOU PUT PHPMAILER IN THE DIRECTORY OF YOU WEB PAGE!!!!!!!1
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;
}
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 :)
I have used following settings in joomla -adminpanel- global configuration-server
Mailer :Sendmail
Mail from :user
From Name :sales#user.com
Sendmail Path :/usr/sbin/sendmail
SMTP Authentication :No
SMTP Security :none
SMTP Port :25
SMTP Username:
SMTP Password :
SMTP Host :localhost
I have used form in site
<form action="email.php" method="post" name="emailForm" id="emailForm" class="form- validate">
in email.php i used php mailer as follows
<?php
require_once("class.phpmailer.php");
$mail = new PHPMailer();$mail->CharSet = 'UTF-8';
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$message = $_REQUEST['text'] ;
// Enable encryption, 'ssl' also accepted
$mail->From = 'sales#example.com ';
$mail->FromName = 'Techzo';
$mail->addAddress('ccccc#example.in'); // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Enquiry /Contact form';
$mail->Body = 'Name: $name\nEmail: $email\n\n$message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
I have searched and worked accordingly but not in use.As am new to these coding please help me to understand this issue
This happens when sendmail is not installed on your server (sendmail should be under /usr/sbin ). You will need to install sendmail or use an alternative, such as gmail (you can use gmail using the phpmailer class.
Thanks for all your suggestions.I have sorted out the problem a day before.
Problem is sendmail option is not available in that server, I have tried for SMTP with wrong port no(25) previously.587 is port no for the mail server.
BY USING SMTP with all their configuration settings i done through it.