how to get input email and send in php - php

I want to get an email address as input by the user and send an email to them using PHPMailer.
** My HTML code and PHP code**
<html>
<body>
<form method="post" action="Emails.php">
Email address <input type="email" name="emailaddress" id="emailaddress" required=""><br>
<input type="submit">
</form>
</body>
</html>
Emails.php
$_POST["emailaddress"] = "";
$emailaddress = ($_POST["emailaddress"]);
require 'C:\xampp\composer\vendor\autoload.php';
use PHPMailer\ PHPMailer\ PHPMailer;
use PHPMailer\ PHPMailer\ Exception;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'my email address'; // SMTP username
$mail->Password = 'my passsword'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail-> Port = 587;
$mail->setFrom('my email address', 'Mailer');
$mail->addAddress('$emailaddress', 'Dear User'); // Add a recipient
$mail->isHTML(true);
$mail->Subject = 'This is your password';
$mail->Body = 'Enter this as your password';
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: '.$mail - > ErrorInfo;
} else {
echo 'Message has been sent';
}
So I use this HTML and PHP code and get an error as
Invalid address: (to): $emailaddress The message could not be sent. Mailer Error: You must provide at least one recipient email address
My PHP version is 5.512. So could you please help me to solve this problem.

Related

PHPMailer 6 Contact Form with Gmail SMTP

I'm trying to make it so that emails can be sent to my Gmail address rather than my domain name email using a PHPMailer form that is in a Bootstrap website. That is my main goal, and the other is to figure out how to have the form include the person's name, email and a subject populate the email rather than the set subject and "no-reply" email. Any help I could get on this would be great; I thought I would see if anyone wanted to solve this for the community here before I hire a freelancer. Thanks!!
I've tried several tutorials to solve this as well as trying to combine the existing code I'm posting below with the SMTP Gmail version on the PHPMailer Github page ( https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps )but I was not successful, I would instead post the working code here to my domain name email than my failed attempts at sending to Gmail.
<form id="contact-form" method="post" action="contact.php">
<div class="messages"></div>
<div class="controls">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Enter your name." required="required">
</div>
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Enter your email." required="required">
</div>
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Add your message." rows="4" required="required"></textarea>
</div>
<input type="submit" class="btn btn-outline-light btn-sm" value="Send message">
</div>
</form>
<?php
use PHPMailer\PHPMailer\PHPMailer;
require './PHPMailer-master/vendor/autoload.php';
$fromEmail = 'noreply#email.com';
$fromName = 'No Reply Email';
$sendToEmail = 'name#mydomain.com';
$sendToName = 'New Website Email Message';
$subject = 'New message from contact form';
$fields = array('name' => 'Name:', 'email' => 'Email:', 'message' => 'Message:');
$okMessage = 'Successfully submitted - we will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
error_reporting(E_ALL & ~E_NOTICE);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailTextHtml .= "<h3>New message from website:</h3><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a great day!</p>";
$mail = new PHPMailer;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName);
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->isHTML(true);
if(!$mail->send()) {
throw new \Exception('Email send failed. ' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
?>
It is working on my projects. You can also remove the part of the code which you don't need like attachments. One more thing if you want to hide the debugging code errors and notifications remove or comment this line $mail->SMTPDebug = 2.
Check this similar StackOverflow article for more help
If you still need more help to let me know.
Hope this may help you.
<?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\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // 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
//Recipients
$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');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//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;
}`enter code here`
s
This is what I use for my form mail, change the variables accordingly.
// PHPMailer
require '/var/www/...../PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//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';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "abcdefgh#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "pppppppppppppppp";
//Set who the message is to be sent from
$mail->setFrom($Email, $Name);
//Set an alternative reply-to address
$mail->addReplyTo($Email, $Name);
//Set who the message is to be sent to
$mail->addAddress('myemailaddress#gmail.com', 'Form Mail');
//Set the subject line
$mail->Subject = 'Form Mail";
// END PHP Mailer
$mail->ContentType = 'text/plain';
$mail->isHTML(false);
// $mail->msgHTML("$Message");
$mail->Body = $Message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Thank you for sending your message.";
}

send mail using php on form submit in htmo method post [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 5 years ago.
I am newbie and i'm trying to send mail using php but unable to send mail when i click on submit button new window open which contain some part of my php code......i want to send email from user email id to my email id with the content of form
This is the first time i'm using php and i stuck in this
<?php
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
require 'class.smtp.php';
// require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "example#gmail.com";
$mail->Password = "example";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = "example#gmail.com";
$mail->FromName = "ADMIN";
$mail->addAddress("example#gmail.com", "User 1");
$mail->IsHTML(true);
$mail->Subject = "form Submission";
$subject = $_POST['subject'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$name = $_POST['name'];
$company = $_POST['company'];
$city = $_POST['city'];
$message = $_POST['message'];
$mail->Body = "
<html>
<h2><br>Name: $name</br>
<br> Email: $email</br>
<br> Phone: $phone</br>
<br> Subject: $subject</br>
<br> Company: $company</br>
<br> City: $city</br>
<br> Message: $message </br></h2>
</html>";
$mail->AltBody = '';
if (! $mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>
html--> action="http://myIPhere:7070/projectname/sendemail.php"
please help me to resolve this problem
i'm using tomcat server 9.0
my entire php code got print i think php code is not executing
in my webcontent i added
class.smtp.php
class.phpmailer.php
class.PHPMailerAutoload.php
<?php
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "test#gmail.com";
$mail->Password = "test#12#";
$mail->AddReplyTo("test#gmail.com", "test mail");
$mail->AddAddress('test#gmail.com', 'test mail ');
$mail->Body = "message:hiii";
if ($mail->send())
//if (mail($subject,$message, $headers))
{
echo "Thank you for sending mail us!";
} else {
echo "Mailed Error: " . $mail->ErrorInfo;
}
?>
this is the sample code..you can download smtp library function from github....
<?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\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers (smtp.gmail.com if you can use Gmail Account)
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#gmail.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
//Recipients
$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');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Use this configuration and download PHPMailer from GitHub.
https://github.com/PHPMailer/PHPMailer

PHP Mailer Multiple email

I have a form in my website. When the user fills out the form, I want an email to be sent to me with data entered in the form and also a thank you email for the person filling the form.
This is the code I am using:
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress('data#domain.org');
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>
<?php
function sendEmail($subject, $body) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl"; // does tls works with port 25
$mail->Host = 'smtp.zoho.com'; // is this the correct
$mail->Port = 465;
$mail->Username = "noreply#domain.org";
$mail->Password = "mypassword";
$mail->SetFrom("noreply#domain.org");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->IsHTML(true);
$mail->AddAddress(''.$_POST['emailAddr'].''); // **
emailAddr is the name for email field in the form and I wish to send email to this email address.
$mail->Subject = 'Thank you for contacting Domain';
$mail->Body = 'Thanks for getting in touch. Your message has been received and will be processed as soon as possible.';
if(!$mail->Send()) {
$mail->ErrorInfo;
echo "<script>alert('Error Occured sending the enquiry. Please try again')</script>";
}
else
{
echo "<script>window.location.href='http://domain.com/index.html'</script>";
}
}
?>

GMX contact form

I'm trying to create a simple SMTP-contact form with the help of phpMailer for my homepage but it's not working. It should forward the information of the fields "name, mail, subject, message" to my mailbox (in this case "name, mail, subject, message"). The input fields are also named "name, mail, subject, message" in my html code.
Can anyone tell me what I'm doing wrong? Thanks for any advice.
<?
require('class.phpmailer.php');
require('class.smtp.php');
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
$mail->Host = 'mail.gmx.de';
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Mailer = "ssl";
$mail->Password = "****";
$mail->Username = "test#gmx.com";
$mail->SMTPAuth = "true";
$mail->FromName = $_POST['name'];
$mail->AddAddress = $_POST['mail'];
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
if(!$mail->Send())
{
echo 'E-Mail not send.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo "<h5>" . 'Thanks for your message' . "</h5>";
}
?>
The error message says you are missing the recipient address, you have to add one using:
$mail->AddAddress("youemail#youremail.com", "Your Name");
//Your Name is optional. so alternatively you could do:
//$mail->AddAddress("youemail#youremail.com");
The part of the code you reference $mail->Username = "test#gmx.com"; simply sets the username for authenticating with the SMTP server, not the recipient address.

php mailer sender form user

i have phpmailer and i can send email via php page without any problem
but the sender automatically by username it i am use in smtp server
i want take sender email from user who write message not from default sender
and this is form code
<?php
require '../../PHPMailer/PHPMailer-master/PHPMailerAutoload.php';
$name = $_POST['name'];
$Institute = $_POST['Institute'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
//$mail->SMTPDebug = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "MyGmail";
$mail->Password = "MyGmailPass";
$mail->setFrom('Mygmail', $name);
$mail->addReplyTo('MyGmail', 'First Last');
$mail->addAddress('MyEmail', 'Nasser');
$mail->Subject = 'Database Site Reminder';
$mail->Body = ($message);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
i am put `$mail->setFrom('Mygmail', $name); this like
$mail->setFrom($email, $name);
because take sender email from user , and i get Message sent
but message not arrive to my email
i cant find any solution... please assist me
thanks ...
$mail = new PHPMailer();
$body = "Body text goes here";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // 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->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "Gmail Id"; // GMAIL username
$mail->Password = "PaSsWoRd"; // GMAIL password
$mail->SetFrom('fromemail#gmail.com', 'User');
$mail->AddReplyTo("fromemail#gmail.com","User");
$mail->Subject = "Subject Goes Here";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress('toemail#gmail.com', 'User');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo("Success");
}
try this code... its working....
If you are using PHP Mailer from Github,
then you can set the Sender name by,
$mail->SetFrom("info#mibckerala.org", "MIBC");

Categories