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.
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:
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.
My code is this.
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$to = "myemail#gmail.com";
$subject = "This is subject";
$message = "This is simple text message.";
mail($to,$subject,$message);
echo "<br>Message sent successfully...";
?>
</body>
</html>
I know this is very simple but I am not able to solve it my try to help me
Is there a mailserver running on the machine that hosts this page? Most likely not because the code you show should work.
Now there are two options: Install a mailserver or use an additional PHP library to overcome this problem. I'm not gonna post a tutorial on how to install a mailserver so just Google it!
For the PHP library part, u could use PHPMailer. This class holds additional settings for sending the mail via specific servers. First download the needed files from GitHub (on the right side, click "Download zip") https://github.com/PHPMailer/PHPMailer . Unzip the package and copy the files to your working directory. The code could be like:
<?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->From = 'from#example.com';
$mail->FromName = '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');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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
$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';
}
?>
The easiest way to send mail is to use a gmail account or something. The settings can be found literally everywhere. For example: http://phpmailer.worxware.com/?pg=examplebgmail
**> the mail posted with the below given is going to spam. I am not using
captcha in the form as i don want to. So can anybody help me to the
mail in Inbox**
<?php
if(isset($_POST['submit']))
{
$name = $_POST['Name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$date = $_POST['checkinDate'];
$package = $_POST['package'];
$person = $_POST['adults'];
$kids = $_POST['kids'];
$ip=$_SERVER['REMOTE_ADDR']; //trace the ip address of the user submited
$subject ="Query From :Kerala-Honeymoon-Packages - Promotion\n"; //subject of the email
$to="paul#roverholidays.com";
$cc="online#roverholidays.com";
$ccc="deepti#roverholidays.com";
$from=$_POST['email'];
$adc="Name :$name\n";
$adc.="Email :$email\n";
$adc.="Phone :$phone\n";
$adc.="Date of Travel :$date\n";
$adc.="Package :$package\n";
$adc.="Adults :$person\n";
$adc.="Kids :$kids\n";
$message ="$name copy of the query you submited in Kerala-Honeymoon-Packages";//message header to user submited
$headers="From: <".$from. ">" ;
mail($cc,$subject,$adc,$headers);
mail($ccc,$subject,$adc,$headers);
mail($email,$message,$adc);
header("Location: thanks.htm");
}
else
{
return false;
}
?>
coding-wise I don't think there is anything you can do because it is the email server that classifies your email as a spam not the way you coded your script. All you can do is to control it from the receiver email setting i.e you setup your gmail filters to detect that email based on keyword like "Kerala-Honeymoon-Packages" and move it out of spam.
I don't know for sure what the email servers algorithms are for marking email as spam. However, I think sending email from different domain rather than your domain name is likely to be detected as phishing email. what I mean is when someone put his/her yahoo email in the form and click on send, your server will send the email to emails addresses in the script but it will send it as if it came from yahoo, which will be suspicious for the receiver email server as it knows that it did not come from yahoo.
Many email services block mail sent directly from random servers because they have little to no reputation as a legitimate source of non-spam emails. Instead of using the straight php mail() function, try using a SMTP service like Mandrill or Gmail's SMTP service. Both are free.
Here is the configuration page for Mandrill:
http://help.mandrill.com/entries/23737696-How-do-I-send-with-PHPMailer-
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MANDRILL_USERNAME'; // SMTP username
$mail->Password = 'MANDRILL_APIKEY'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
$mail->AddAddress('josh#example.net', 'Josh Adams'); // Add a recipient
$mail->AddAddress('ellen#example.com'); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
How to get user mails in my free gmail inbox through contact us form on my website. I do not use email with my website name . i use free gmail. I tried many script but the all need email account on domain.
Does this question have something to do with This One?
Well, so you have a form on your site, your users fill it up and you need an email with that data, right?
Simple example:
<form action="sendMail.php" method="post">
Name: <input type="text" name="name" id="name" /><br />
Email: <input type="text" name="email" id="email" /><br />
Text: <textarea name="text"></textarea><br />
<input type="submit" value="Send" />
</form>
then, the php page wich send the mail:
//php sendThis.php page
<?php
require("class.phpmailer.php");
$name = $_POST['name'];
$email = $_POST['email'];
$text = $name . ', ' . $email . ' has filled the form with the text:<br />' . $_POST['text'];
$from = 'your.email#gmail.com';
$to = 'your.email#gmail.com';
$gmailPass = 'your gmail password';
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = $from;
// GMAIL password
$mail->Password = $gmailPass;
$mail->From = $from;
$mail->FromName = $from;
$mail->AddReplyTo($from, $from);
$mail->Subject = 'This is a test!';
$mail->Body = $text;
$mail->MsgHTML($text);
$mail->IsHTML(true);
$mail->AddAddress($to, $to);
if(!$mail->Send()){
echo $mail->ErrorInfo;
}else{
echo 'sent!';
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
EDIT: just tested and works fine. Make sure that the 3 files (class.phpmailer.php, class.pop3.php and class.smtp.php) are in the correct include path
Basically, it involves the PHP mail() function:
<?php
mail(yourGmailAddress, object, message);
?>
As you have already observed, this solution works only if the webserver operates a mail server. This mail server may forbid unknown users. So you need to have an email account on that web/mail server (I believe this is the case). The second step then is to forward mail from your website address to you gmail account. I am 90% certain that it is possible from your gmail configuration. It may also be possible from your website mail configuration. But don't configure both!
Try enabling openssl.
Uncomment the line:
extension=php_openssl.dll
in your php.ini file.
You can also put your email address into "action" attribute of the form element. But that's very unreliable. Like this:
<form method='post' action='mailto:your#email.com?Subject=Hello'>
...
</form>
Users must have email client installed and configured for this to work properly. There are some other drawbacks. You have to do some research to find whether this method is for you or not.
http://www.google.com/search?client=opera&rls=en&q=form+action+mailto&sourceid=opera&ie=utf-8&oe=utf-8