I added this contact form to a custom page template in wordpress:
<form id="contact_form" action="inc/mail.php" method="POST">
<input id="name" name="name" placeholder="Name">
<input id="email" name="email" placeholder="Email">
<input id="phone" name="phone" placeholder="Phone Number">
<textarea id="comments" name="comments" placeholder="Comments / Questions"></textarea>
<input class="button" type="submit" id="submit" value="Submit">
</form>
And I created a PHP page to handle the form once posted:
<?php
// Gather form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$client = "";
$clientname = "";
$password = "";
if($name && $email && $comments) {
// Create instance of PHP mailer
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = $client;
$mail->Password = $password;
$mail->From = $client;
$mail->SetFrom($email, $name);
$mail->AddAddress($client, $clientname);
$mail->IsHTML(false);
$formcontent="From: \n $name \n\n Email: \n $email \n\n Phone: \n $phone \n\n Comments: \n $comments";
$mail->Subject = "Miss Mary's Inquiry";
$mail->Body = $formcontent;
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>
The form and handler work perfectly outside of Wordpress, but I think Wordpress is preventing the PHP script (mail.php) from being accessed. Is there a way to post the form to my PHP script without Wordpress / htaccess interfering?
Thanks!
If you want to send mails in wordpress try using plugins. There are tons of wordpress plugins for that.
Conatact form 7 is the one which I like the most. Still you can develop your own but create it as plugin. You can convert the above code into a shortcode plugin. Check this link
wp_mail( $to, $subject, $message, $headers, $attachments )
This function is normally used for sending mails in wordpress
Related
So the emails I recieve only shows the subject, who it was sent from, who it was sent to but not the senders name or phone number, this is not done locally but from where my website is hosted. I want it to show the senders name and phone number after the body. This is my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $_POST['message'];
$mail->Name = $_POST['name'];
$mail->Phone = $_POST['number'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
//echo $mail->ErrorInfo;
?>
This is my html:
<form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
<div class="input-grids">
<input type="text" name="name" placeholder="Full name or Business"
class="contact-input" required=""/>
<input type="email" name="email" placeholder="Your email" class="contact-
input" required=""/>
<input type="text" name="subject" placeholder="Subject" class="contact-
input" required=""/>
<input type="text" name="number" placeholder="Phone number"
class="contact-input" />
</div>
<div class="form-input">
<textarea name="message" placeholder="Type your message here"
required=""></textarea>
</div>
<div class="form-input mb-5">
<label class="container"><p>Send me a copy <a href="#">privacy policy.
</a></p>
<input type="checkbox">
<span class="checkmark"></span>
</label>
</div>
<button class="btn submit">Submit</button>
</form>
I am not sure if I need to do something like this:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
}
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
and just give my submit button this attribute
<button class="btn submit" name="submit">Submit</button>
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You basically want to create a $message variable and assign that to $mail->Body.
The second example you have shown is close but you need to make sure that the below is within your $_POST['submit'] if statement.
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
Like so:
if (isset($_POST['submit'])) {
require 'vendor/autoload.php';
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['number'];
$mail = new PHPmailer();
$mail->Host = "";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->Username = "";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->addAddress('');
$mail->setFrom($_POST['email']);
$mail->Subject = $_POST['subject'];
$mail->isHTML(true);
$mail->Body = $body . "<br>Phone number: " . $number . "<br>Name: " . $name;
$response = $mail->send(); //to actually send the email
}
For:
Last thing: I want to show that the message has been sent just above the contact form instead of at the top of the page, it is not top priority but something I was still wondering about.
You could subject the form via ajax and listen to the $response value. If its 1 then good to go, else there was an issue.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I've been working on this for some time and am quite new to php. I'm having trouble getting this to send. A second set of eyes on this code would be most helpful:
<?php
if(isset($_POST['submit'])){
$to = "myEmail"; // this is your Email address
$from = $_POST['emailAddress']; // this is the sender's Email address
$fullName = $_POST['fullName'];
$subject = "Form submission";
$message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment'];
$message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $fullName . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<form method="post" action="contact.php">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">
<label for="emailAddress">Email</label>
<input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe#example.com">
<label for="comment">Comment</label>
<textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>
<button name="submit" type="submit" class="btn">Submit</button>
</div>
</form>
Many thanks!
I would suggest to use PHPMailer. It is an open-source project available on GitHub : PHPMailer - GitHub
This class permits you to exploit the SMTP services of the most famous mailing platform to get a fast system to send mail using PHP.
It's really simple to set up a code with this class, starting from an HTML form :
<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>
<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');
if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]);
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);
if(!$mail->Send())
{
echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>
As you can see in the PHP code, I'm using Gmail SMTP service to send this mail. Note that if you want to use other services you have to change the SMTP server.Furthermore, you need to login into your email service to get an access to the SMTP service, and you need really often to enable the possibility of accessing your mailing account by third part applications too.
In some cases, the SMTP server won't accept TLS or SSL encryption.
Is it possible to add attachment adding enctype="multipart/data" attribute to your form tag and getting the uploaded file through the $_FILES array.
I hope this is going to help you!
In your code parameters of second mail function are not completed you didn't define value of subject2 I think your first message send in the right way but the second will not
Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.
Update: My send.php now looks like this and when i click send got a blank page and send nothing. I have the class.phpmailer.php in the right place.
<html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>
<?php
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.******.hu"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.******.hu"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "support#******.hu"; // SMTP account username
$mail->Password = "******"; // SMTP account password
$mail->SetFrom('support#******.hu', 'First Last');
$mail->AddReplyTo("support#******.hu","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$address = "support#******.hu";
$mail->AddAddress($address, "support#******.hu");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "support#******.hu";
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$formcontent= eregi_replace("[\]",'',$formcontent);
$mail->MsgHTML($formcontent);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Helo!
I want to send a html form in email with this php code but not working...
I don't know why... If you know the solution please help me :)
Thank you!
The HTML form:
<div id="stylized" class="myform">
<form id="form1" action="send.php" method="POST">
<label>Név
<span class="small">Kérem adja meg nevét</span>
</label>
<input type="text" name="name">
<label>Email
<span class="small">Kérem valós címet adjon meg</span>
</label>
<input type="text" name="email">
<br />
<br />
<label>Telefon
<span class="small">Visszahíváshoz adja meg telefonszámát</span>
</label>
<input type="text" name="phone">
<br />
<br />
<label>Elérhetőség
<span class="small">Kérem adja meg mikor érhetjük el telefonon</span>
</label>
<select name="priority" size="1">
<option value="Low">Délelőtt</option>
<option value="Normal">Délután</option>
<option value="High">Este</option>
<option value="Emergency">Egész nap</option>
</select>
<br />
<br />
<br />
<label>Szolgáltatás
<span class="small">Mivel kapcsolatban keres minket?</span>
</label>
<select name="type" size="1">
<option value="update">Szolgáltatás Megrendelése</option>
<option value="change">Szolgáltatás Lemondás</option>
<option value="addition">Információ</option>
<option value="new">Hiba Bejelentése</option>
</select>
<br />
<br />
<br />
<label>Tárgy
<span class="small">Írja le az üzenet tárgyát</span>
</label>
<input type="text" name="website">
<br />
<br />
<br />
<label>Üzenet
<span class="small">Írja le üzenetét</span>
</label>
<textarea name="message" rows="15" cols="29"></textarea><br />
<button type="submit" value="Send" style="margin-top:15px;">Küldés</button>
<div class="spacer"></div>
</form>
</div>
And my php:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
$mail = new PHPMailer();
$formcontent = eregi_replace("[\]",'',$formcontent);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.myserver.hu"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.myserver.hu"; // sets the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "support#myserver.hu"; // SMTP account username
$mail->Password = "myserverpass"; // SMTP account password
$mail->SetFrom('email#myserver.hu', 'First Last');
$mail->AddReplyTo("email#myserver.hu","First Last");
$mail->Subject = "test";
$mail->AltBody = "test"; // optional, comment out and test
$mail->MsgHTML($formcontent);
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "email#myserver.hu";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Please help me if you can! :) Thaks!
You might want to add $mail->SMTPSecure = "ssl";
You can use mail basic function PHP... with this short code:
Class Mailer{
public static function sendMailNewUser($username,$firstname,$lastname,$password){
$to = $username;
$from_name = "INFO MAIL";
$from_mail = "noreply#mail.it";
$subject = "New User";
$uid = md5(uniqid(time()));
// header
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=utf8\r\n";
$message = "Welcome $firstname $lastname, ";
$message .= "<br><br>";
$message .= "This is your account:<br>
<b>username: $to<br>
password: $password</b><br>";
mail($to, $subject, $message, $header);
}
}
You just made one mistake you need to put all variables before use them, like this :
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$address = "email#myserver.hu";
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$formcontent= eregi_replace("[\]",'',$formcontent);
$mail->MsgHTML($formcontent);
I have been created simple mail sending from localhost using php,
Her is the code:
HTML:
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
EMAIL.PHP:
<?php
require_once('class.phpmailer.php');
$mail->MsgHTML($body);
$body ='A sample email';
$mailer->IsSMTP();
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mailer->Host = 'smtp.gmail.com';
$mailer->Username = 'YYYYYYYYYY#gmail.com';
$mailer->Password = 'XXXXXXXXX';
?>
when run this code,
following error displayed,
Fatal error: Call to a member function MsgHTML() on a non-object online 6
note: where is got phpmailer.php source code from this link
I m new to php, but i want to know, particular this section.
Can anyone help me to fix this,
Thanks in advance,
Perhaps $mail is not instantiated, and spelled wrong (did you mean $mailer?). Also, you should set $body before MsgHTML($body). From your link, you may want to add this
$mailer = new PHPMailer;
and make changes like so:
$mailer = new PHPMailer;
$body ='A sample email';
//$mail->MsgHTML($body);
$mailer->MsgHTML($body);
$mailer->IsSMTP();
...
Here is an example of an HTML-form and PHPMailer:
HTML
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
mail.php
<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require_once('class.phpmailer.php');
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer#bradm.inmotiontesting.com
// pass: password
$mail->Username = "send_from_PHPMailer#bradm.inmotiontesting.com"; // SMTP username
$mail->Password = "password"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>