Issues with HTML PHP Mailer - php

i I've been trying to get a .php that works with this HTML code for my website (template), I tried using my old .php from my old website and changing the details but that sadly lead to no avail.
I am clueless when it comes to .php and would really appreciate your help!
What would my .php have to contain?
<form action="#" id="contact-form">
<div id="success"></div>
<ul>
<li class="input-name">
<input type="text" id="name" class="required" placeholder="Name">
</li> <!-- END input-name -->
<li class="input-email">
<input type="text" id="email" class="email" placeholder="Email Address">
</li> <!-- END input-name -->
<li class="input-subject">
<input type="text" id="subject" placeholder="Subject">
</li> <!-- END input-name -->
<li class="input-subject">
<textarea rows="7" cols="50" id="message" class="required" placeholder="Message"></textarea>
</li> <!-- END input-name -->
</ul> <!-- END #contact -->
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Message</button>
</form>

if you are not using ajax then write php file name in form action attribute currently there is #. then you php file will be called.

Change the below code
<form action="#" id="contact-form">
to
<form action="mailer.php" method="post" id="contact-form">
hope this will help.

You have to include the in action="#" the php file. example: action="contactForm.php". That's how the HTML code knows where to send the parameters.
Also, make sure your server supports the 'mail' function.
AND!
I have a good standard example for you of a well written mailing php file that I used. So you can check yourself for syntax issues.
<?php
require_once "Mail.php";
if(isset($_POST['email'])) {
$email = $_POST['email']; //this is how you get your variables from the HTML file. 'edit' is the id of the element.
$email_to = "yourEmail#example.com";
$host = "ssl://smtp.gmail.com:465"; //use your email host - this example is gmail based. (you can search for your own email host via google).
$username = 'username#example.pro';
$password = 'yourPass';
$email_subject = "You have a new email from $email via example.com website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email_to, $headers, $message);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}
}
In case of sending emails with ajax - it's an other thing.. and I can help you with that too.

Related

how do i display a custom message after submitting a form?

I have a html form that sends email, using phpMailer, after its submission. Everything works fine but i want to display a message on the form when the email is sent.
With my code, a message is displayed on an empty white page.
My php code:
<?php
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$msg = $_POST['msg'] ?? '';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->Host='smtp.gmail.com';
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Port=587;
$mail->SMTPAuth=true;
$mail->SMTPSecure='tls';
$mail->Username='email#email.com';
$mail->Password='password';
$mail->setFrom($email, $name); // who send the email
$mail->addAddress('email#email.com'); // who recive the email
$mail->isHTML(true);
$mail->Subject = 'Portfolio response';
$mail->Body = $msg;
$mail->send();
echo 'Your message has been sent!';
} catch (Exception $e){
echo '';
}
?>
Your message has been sent! is obviously the message displayed.
Html form code:
<form action="mail-handler.php" method="POST" id="contact-form">
<span class="contacts-text" id="first-contacts-text">To get in touch with me, please compile this form.</span>
<span class="contacts-text">I will reply as soon as possible. Thank you!</span>
<ul class="form-content">
<li class="form-input">
<label for="name" class="label">Full name</label>
<input class="input" id="name" type="text" name="name" required>
</li>
<li class="form-input">
<label for="email" class="label">E-mail</label>
<input class="input" id="mail" type="text" name="email" required>
</li>
<li class="form-input">
<label for="msg" class="label">Insert text</label>
<textarea class="input" id="comment" type="text" name="msg" cols="30" rows="10" style="resize: none;" required></textarea>
</li>
<li class="form-input" id="last-input">
<input class="input" id="form-button" type="submit" value="submit" name="submit" onclick="sendEmail()">
</li>
</ul>
</form>
Thanks in advice!
First, change the name of your index.html to index.php, so it can be parsed by a PHP interpreter.
Next modiy your success statement body, i.e.:
try {
$mail->send();
header('Location: index.php?mailsent=1');
die(); // we don't want exactly anything after sending redirect header.
} catch (Exception $e){
echo '';
}
Finally in file with your form add the message if $_GET['mailsent'] variable is available.
HTML code of your page...
...
<?php
if (isset($_GET['mailsent']) && intval($_GET['mailsent']) == 1 ){
echo '<div class="messagesbox">Mail was sent</div>';
}
?>
<form action="mail-handler.php" method="POST" id="contact-form">
... your form body skipped here
</form>
Other options
If you don't want to pass arguments via GET array, you can try to use sessions.
Also, you can just create a static thankyou.html page and redirect the user there, after mail submitting
Also, if you move the whole PHP code for the form submittion into index.php you won't need to make redirects.
Finally, as you added - while it's one-page site you should also consider using AJAX probably with jQuery - that way you're will be able to submit the form without leaving the page, display messages without reloading, etc. Anyway, this topic is definitely too broad to be described in this answer and I can only suggest you get familiar with jQuery AJAX.

New to PHP. Can't figure out what is missing to send mail

I'm new to PHP and having a hard time getting the PHP to mail when the contact form is filled out and submit is pressed. I've posted the PHP and HTML files. Any help is very much appreciated.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jaza Solutions - Contact</title>
<link rel = "stylesheet"
type = "text/css"
href = "jazasolutions.css" />
</head>
<body>
<div id = "header">
<img src="jazasolutions.png" alt = "Jaza Solutions, LLC">
</div>
<div id="nav">
<ul>
<li><a href=JazaSolutionsContact.html>Contact</a></li>
<li><a href=JazaSolutionsAboutUs.html>About Us</a></li>
<li><a href=JazaSolutionsCourses.html>Courses</a></li>
<li><a href=JazaSolutions.html>Home</a></li>
</ul>
</div>
<div class="sideRight">
<p> Jaza Solutions</p>
<p> 9818 Ushers Place </p>
<p> Waldorf, MD 20601 </p>
<br>
<p>301-861-2133</p>
<p>info#jazasolutions.com</p>
</div>
<div class = "main">
<p> Get the Management Certification you need to make the next step in your career! </p>
<br>
<p>SEND A MESSAGE</p>
<form class="contact-form" action="contactform.php" method="post">;
<input type="text" name="name" placeholder="Full Name">
<input type="text" name="mail" placeholder="Email">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit" name="submit">SEND MAIL</button>
</form>
</div>
</body>
</html>
The PHP code is posted here. This should send an email, but I can't figure out why it's not working.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "pmckeown#jazasolutions.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
?>
I tried something similar a few years ago. After hours of searching I got a Tipp I'll never forget. The Mail RFC is so huge and has so many special cases that you don't do yourself a favor when you try to implement this on your own (Even if you get your Mail send it's most likely that it will be marked as spam from most of the Mail-Services out there because you missed some special handling or some tags). This becomes really funny when you try to send an attachment or even images in your mailer implementation. I'd recommend you try one of the proven Mail-Libraries for PHP like PHPMailer. You can expect that most of the common use-cases work out of the box without great problems.

Sending emails with Mailgun occasionally hangs and doesn't send email

So ive just implmented mailgun into a website for sending contact form info.
This works some of the time, but mostly the page hangs when i press send with the message "waiting for url..." in the bottom left of chrome.
There is no ssl on the server hence the strange mailgun constructor.
This is my php which is placed just inside the body of my website.
<?php
require 'mailgun-php/vendor/autoload.php';
use Mailgun\Mailgun;
if(isset($_POST['register'])){
$message = "Contact Form.\n\n".
"Name: ".$_POST['name']."\n".
"Email: ".$_POST['email']."\n".
"Message: ".$_POST['message']."\n";
$mg = new Mailgun(*my key*, "api.mailgun.net", "v2", false);
$domain = *my domain*;
$mg->sendMessage($domain, array(
'from'=>'Contact Form <build#<url>>',
'to'=> *email*,
'subject' => ' Contact Form',
'text' => $message
)
);
header('Location: ?sent=1');
}
?>
This is the form code:
<form method="post" action="index.php">
<div class="row 50%">
<div class="6u 12u$(mobile)"><input type="text" class="text" name="name" placeholder="Name" /></div>
<div class="6u$ 12u$(mobile)"><input type="text" class="text" name="email" placeholder="Email" /></div>
<div class="12u$">
<textarea name="message" placeholder="Message"></textarea>
</div>
<div class="12u$">
<button class="button" type="submit" name="register"> Send Message </button>
</div>
</div>
</form>
I'm not getting any errors when the email is not sent. The page will eventually reload after the submit button is pressed but the header redirect is not being applied (which im assuming is because the email was not sent successfully).
The php error logs do not show anything going wrong either.
Thanks
Maybe that's a (very) late answer, but I've just met the same problem. And I discovered, that Mailgun has whitelist of IP addresses, so in case you haven't added there your public IP address, the connection to Mailgun's API will never be established.
At the moment of writing this answer, the whitelist is present under this link: https://app.mailgun.com/app/account/security/api_keys

How to create php form handler and intergrate with existing html web form? [duplicate]

This question already has answers here:
Send email with PHP from html form on submit with the same script
(8 answers)
Closed 7 years ago.
So here is the problem I am facing. I have created a pretty simple web form:
<form method="post" action="#">
<div class="field"> <label for="name">Name</label>
<input name="name" id="name" type="text"> </div>
<div class="field"> <label for="email">Email</label> <input
name="email" id="email" type="email"> </div>
<div class="field"> <label for="message">Message</label> <textarea
name="message" id="message" rows="4"></textarea> </div>
<ul class="actions">
<li><input value="Send Message" type="submit"></li>
</ul>
</form>
I need to know how I can use this form to send data inputed by the user to my email address admin#nue-tech.uk I am aware this can be done in PHP but am unsure how to approach this as I am unfamilliar with PHP. If someone could please point me in the right direction as to how this can be done, and also where I should place the PHP file relative to this, that'd be awesome!
You could copy the code below and paste it in your HTML file below your html. In your html you set the action attribute empty. Don't forget to change your file extension to .php
<?php
if(isset($_POST['submit'])){
$to = "admin#nue-tech.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Blablabla"; //Write whatever you want here
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page if the mail was send succesfully
} else {
header('location: contact.html'); //if it was not send succesfully it redirects to the contact page again
exit(0);
}
?>
In
header('location: contact.html');
you could also use
echo "Something went wrong. Try again later"
or something simular.
I would highly recommend you search and learn on W3Cschools or at php.net.

php mail attachments with message

I have seen this is a common question but through searching I haven't found the answer I need. I have used PHP code to create a mailer with attachments that are located on my server.
My site has a CRUD system and the read.php file displays various elements of an uploaded file. I also have a form on this page which allows the user to enter and email address and message to send on the said file.
My form code is:-
<form class="form-horizontal" action="mailer.php" method="post">
<div class="form-group">
<label class="col-sm-2">Email Address</label>
<div class="col-sm-4">
<input name="doc_email" type="text" placeholder="Enter Email Address to" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="col-sm-2">Message</label>
<div class="col-sm-4">
<input name="doc_message" type="text" placeholder="Enter a message (not mandatory)" class="form-control" >
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Send Email</button>
</div>
</form>
And my mailer.php code is currently;-
<?php
if (isset($_POST['submit']))
{
$file_name = $_POST['file_name'];
$doc_email = $_POST['doc_email'];
$doc_message = $_POST['doc_message'];
require_once('class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'you#example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $doc_message;
$email->AddAddress = $doc_email;
$path_file_to_attach = '/documents/uploads/';
$file_to_attach = $file_name;
$email->AddAttachment( $path_file_to_attach , $file_to_attach );
return $email->Send();
//redirect user after success
header("Location: index.php");
}
?>
The class.phpmailer.php file is within the same directory (/documents) as the read.php/mailer.php, all I get is a blank screen with no email or errors.
The folder with the files is located in a sub folder called 'uploads'... documents/uploads/
Any help as always is greatly appreciated.
You have two things empty:
if (isset($_POST['submit'])) //this is false, your button must have a name="submit" attribute
$file_name = $_POST['file_name']; // you don't have any element with this name.
Just add to your html:
<input type="text" name="file_name">
And your button
<button type="submit" name="submit"></button>

Categories