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);
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.
I add to country code input area but country code not included when sending mail. When you fill in your phone number, it looks like the picture in the picture 1 but I want you to look like the picture 2. How do I edit this in php mailer? The codes index.php and mail.php are below. (I corrected the picture by checking the item in order to be an example) If you need JS Code
http://allinoneinvest.com/landing/intlTelInput.js
Picture 1
Picture 2
index.php phone input code
<div class="lp-pom-form-field clearfix" id="container_phone">
<label for="phone" class="main lp-form-label" id="label_phone">Phone *</label><input type="tel" id="mobile-number" oninput="this.value = this.value.replace(/[^0-9+.]/g, '').replace(/(\..*)\./g, '$1');" name="phone" class="text form_elem_phone" required>
</div>
mail.php
<?php
if (isset($_POST["submit"])) {
$isimsoyisim = $_POST['your_name'];
$email = $_POST['your_email'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$message = $_POST['message'];
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
//$mail->SMTPSecure = 'tls';
$mail->Host = "mailhost";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet ="utf-8";
$mail->Username = "mail";
$mail->Password = "mailpwd";
$mail->SetFrom("$email", "$isimsoyisim");
$mail->AddReplyTo("$email", "$isimsoyisim");
$mail->AddAddress("email");
$mail->Subject = "Landing Page - $isimsoyisim";
$mail->Body = "<strong>Message:</strong><br />$message <br /><br /><strong>Mail:</strong> $email<br /><br /><strong>Phone:</strong> $phone<br /><br /><br /><br /> This message was sent from the landing page";
if(!$mail->Send()){
echo "Error: ".$mail->ErrorInfo;
} else {
$gittiMesaji = "Dear $isimsoyisim Your Message Sent";
}
}
?>
i have this form:
<form action="/sendemail.php" id="main-contact-form" method="post" name="contact-form" role="form">
<div class="form-group">
<input class="form-control" id="name" name="name" placeholder="put your name" required="" type="text" />
</div>
<div class="form-group">
<input class="form-control" id="email" name="email" placeholder="Email" required="" type="email" />
</div>
<div class="form-group">
<input class="form-control" id="subject" name="subject" placeholder="Subject..." required="" type="text" />
</div>
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Mensaje" required="" rows="8"></textarea>
</div>
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Enviar" />
and this is sendemail.php:
<?php
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message\n $pos";
mail ($to, $subject, $body, $from)
?>
the email is arrive empty, i try a lot of stuffs but always comes in the same way, is like the form can´t pass the data from inputs to php variables. im new in php so, any help is welcome.
thanks.
The use of mail() function is so insecure and is a vector attack to send massive spam, don't use anymore, for this reason is better to use PHP Mailer libray.
Create a EMAIL account (example: no-reply#domain.com) in your hosting and after that install this library PHP Mailer, now you can do this:
<?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', '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->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';
}
UPDATE
After some research I've found that:
You need to ask godaddy technical support to enable "sendmail".
PHP mail() not working on GoDaddy
----------
I've tested your code and it works. I've received the email with ALL the $_POST contents without any problems.
$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#gmail.com ';
$subject = 'contact message ';
NOTE:
1 - $pos = $_POST; is an array and it will be displayed as Array.
2 - Emails sent via mail() will most likely end-up on the spam folder (read the comments below).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have created an HTML form, and some part of a PHP script. Look below.
I need, somehow, to send the form, using the mail function. How do i do this?
HTML:
<form id="kontaktForm" action="Scripts-ContactForm/recieving.php" method="POST">
<div id="kontaktFormVenstre">
<div id="inputFields_container">
<div id="inputField_container">
<input class="inputField" type="text" name="name" placeholder="Dit navn" />
</div>
<div id="inputField_container">
<input class="inputField" type="text" name="email" placeholder="Din E-Mail" />
</div>
</div>
<div id="inputFieldMessage_container">
<textarea class="inputFieldMessage" name="message" placeholder="Din besked her" rows="8" cols="20"></textarea>
</div>
</div>
<div id="kontaktFormHøjre">
<div id="button">
<input type="submit" name="submit" value="Send mail" />
</div>
</div>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Fra:'$email;
$to = 'MyEmail';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
You can send mail like this. Use PHP mail() function.
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Fra:'$email;
$to = 'MyEmail';
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail($to,$subject,$body,"From:$from");
}
?>
Your better off using something like PHPMailer, you can download it from: https://github.com/PHPMailer/PHPMailer
There are many examples to help you get started, but using this you can generate HTML emails and HTML alternatives ( for clients that cant read html )
example from github:
<?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->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';
}
as an alternative you can use PHP's mail() function and as said above its as simple as using:
if(mail('toemail', 'subject', 'message', 'headers')) {
echo 'Mail has sent';
} else {
echo 'Mail not sent';
}
http://php.net/manual/en/function.mail.php
Try this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'MyEmail';
$subject = 'Hello';
$headers = 'From: '.$name.' <' . $email . ">\r\n" .
'Reply-To: '.$name.' <' . $email . ">\r\n";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail($to, $subject, $body, $headers);
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