i have code for send email to my email
but if i press ok i get can not send email to **#hotmail.com
i work on linux server (fedora), and i don not change any settings
my mail.html file is
<html>
<head><title>Mail sender</title></head>
<body>
<form action="mail.php" method="POST">
<b>Email</b><br>
<input type="text" name="email" size=40>
<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>
</body>
</html>
and my email.php file is :
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
# Retrieve the form data
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
# Sends mail and report success or failure
if (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
please try help me
Best if you use a dedicated script for sending emails. Such as PHPMailer which gives you various config options including SMTP:
// Send Mail
$mail = new PHPMailer(); // initiate
$mail->IsSMTP(); // use SMTP
// SMTP Configuration
$mail->SMTPAuth = true; // set SMTP
$mail->Host = "myhost"; // SMTP server
$mail->Username = "email#email.com";
$mail->Password = "password";
$mail->Port = 465; // change port is required
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
How can I try my PHP code to see whether it works?
(6 answers)
Issue with contact form (html and php): 405 not allowed
(1 answer)
Closed 4 years ago.
I'm trying to make a very basic contact form in PHP. I'm using MAMP to test it. For some reason, however, the form doesn't work: when I fill in the form and press the "submit" button, I am redirected to index.html?mailsend but I don't receive any email! What am I doing wrong?
my HTML code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="contactform.php" method="POST">
<input type="text" name="name" placeholder="Full Name">
<input type="text" name="mail" placeholder="Your e-mail">
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Message"></textarea>
<button type="submit" name="submit">Send e-mail</button>
</form>
</body>
</html>
my PHP code:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "tomms1998#gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsend");
}
?>
You have to configure SMTP on your server. You can use G Suite SMTP by Google for free:
<?php
$mail = new PHPMailer(true);
// Send mail using Gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "your-gmail-account#gmail.com"; // GMAIL username
$mail->Password = "your-gmail-password"; // GMAIL password
}
// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";
try{
$mail->Send();
echo "Success!";
} catch(Exception $e){
// Something went bad
echo "Fail :(";
}
?>
I am using PHPmailer to send my email. I have created a form that you enter a few details (including the email address). Once I click submit/generate I want to modify the content of the email message using the form details. The email message should show up as an html email. However, I just cannot get my form to display correctly or get my html-email to send correctly.
Form Code (index.php):
`
<?php
//PHP mailer code
require 'PHPmailer/PHPMailerAutoload.php';
$emailmessage = require 'mail.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
if (isset($_POST['submit'])) {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#email.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('email#email.com', 'Me');
$mail->addAddress($_POST['emailrecipients'], $_POST['client']); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['callid'].' | Customer Feedback';
$mail->Body = $emailmessage;
$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';
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form>
<input type="text" id="firstname" name="firstname" placeholder="John" />
<input type="text" id="lastname" name="lastname" placeholder="Doe" />
<input type="email" id="email" name="email" placeholder="email#email.com" />
<button type="submit" name="submit">Generate</button>
</form>
</body>
</html>`
Email Code (mail.php):
<!DOCTYPE html>
<html>
<body>
<p>First Name: <?php $_POST['firstname']; ?></p>
<p>Last Name: <?php $_POST['lastname']; ?></p>
</body>
</html>
I had imagined that upon submitting, the required email message would be altered as per the input fields, then sent to address typed into the the email input field.
I'm not sure if this line works as expected:
$emailmessage = require 'mail.php';
You should have your mail.php as followed:
$emailmessage = "
<!DOCTYPE html>
<html>
<body>
<p>First Name:".$_POST['firstname']."</p>
<p>Last Name: ".$_POST['lastname']."</p>
</body>
</html>";
And then use only
require "mail.php"
in line 2 of your code. You also should add the "form" Tags to your HTML-form.
Anyone can tell why certain emails I put for $email_to will receive the email while other emails I put are unable to receive.
Sorry, a newbie in PHP scripting.
<?php
print "<h2>Simple Contact Form</h2><br/><br/>";
$email_to = "booked#domain.com";
//if "email_from" variable is filled out, send email
if (isset($_REQUEST['email_from'])) {
$email_from = $_REQUEST['email_from'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
//send email
mail($email_to, "$subject", $comment, "From:" . $email_from);
//Email response
echo "Thank you for contacting us!";
}
//if "email_from" variable is not filled out, display the form
else {
?>
<form method="post">
Email From: <input name="email_from" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
Message:<br />
<textarea name="comment" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
}
?>
You need to use php mailer so that it will work
You must have email address with privilege. Like you hv hosting email...
download phpmailer and add it within your code so that you can send your mail..
here is some rference code which is used by me...
<?php
require("PHPMailer/class.phpmailer.php");
require("PHPMailer/PHPMailerAutoload.php");
define("MAILHOST",'hostsite ');
define("MAILSMTPAuth",true);
define("MAILUsername",'hosting mail');
define("MAILPassword",'password');
define("MAILSMTPSecure",'ssl');
define("MAILPort",portno);
define("MAILFrom",'hosting mail');
$mail = new phpmailer();
$result = array();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = MAILHOST; // Specify main and backup SMTP servers
$mail->SMTPAuth = MAILSMTPAuth; // Enable SMTP authentication
$mail->Username = MAILUsername; // SMTP username
$mail->Password = MAILPassword; // SMTP password
$mail->SMTPSecure = MAILSMTPSecure; // Enable TLS encryption, `ssl` also accepted
$mail->Port = MAILPort; // TCP port to connect to
$mail->From = MAILUsername;
$mail->FromName = 'Name';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = " Inquiry Form";
$mail->Body = "message";
$mail->SetFrom('hosting mail address', 'name');
$mail->addAddress('recieving mail address', 'Name'); // Add a recipient admin
}
exit;
?>
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";
?>
I have mail form:
<div class="contact">
<form id="mailer-form" action="./plugins/mailer/gmail.php" method="post" name="message">
<input type="text" name="name" /> <span>Jméno <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="email" /> <span>Email <strong>*</strong></span>
<div class="clear"></div>
<input type="text" name="subject" /> <span>Předmět</span>
<input type="hidden" name="frompage" value="yes" />
<div class="clear"></div>
<span>Zpráva <strong> * </strong></span>
<div class="clear"></div>
<textarea name="message" id="message"></textarea><br />
<input type="submit" name="submit" value="Odeslat zprávu!" />
</form>
</div>
And PHPMailer script:
<?php
if ($_POST['frompage'] == "yes")
{
date_default_timezone_set('Etc/UTC');
require './PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->CharSet = 'UTF-8';
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxx";
//Password to use for SMTP authentication
$mail->Password = "xxx";
//Set who the message is to be sent from
$mail->setFrom($_POST['email'], $_POST['name']);
//Set an alternative reply-to address
$mail->addReplyTo('xxx', 'xxx');
//Set who the message is to be sent to
$mail->addAddress('xxx', 'xxx');
//Set the subject line
$mail->Subject = $_POST['subject'];
$mail->msgHTML($_POST['message']);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Sending email works fine but after sending email the page redirects me to the gmail.php with Message sent echo.
How can I disable it beacuse of staying on the index.php with response in value of the submit button?
Thanks.
It's not redirecting you, you set the form action to "./plugins/mailer/gmail.php", that's where your form data gets sent.
Replace:
echo "Message sent!";
by
header("Location:YourFormPage.php");
It will send you back. It's possible to do what your looking for, you need to research about AJAX so using this will not be necessary to leave the first page and complete the form submition.