How do I send an email from gmail within my app? - php

I have the following code. I would like to send a confirmation email to the user as soon as they click the 'register' button. When I click on the register button my database gets updated with the correct information but it doesn't send the email.
Email.php:
<?php
include('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
//GMAIL config
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl'; // sets the prefix to the server
$mail->Host = 'smtp.gmail.com'; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = 'xxx#gmail.com'; // GMAIL username
$mail->Password = 'yyy'; // GMAIL password
//End Gmail
$mail->From = 'xxx#gmail.com';
$mail->FromName = 'SYS';
$mail->Subject = 'Registration Successful';
$mail->MsgHTML('You have successfully been added to the System!');
$mail->AddAddress($_POST['EmailAddress']);
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {//to see if we return a message or a value bolean
echo "Mailer Error: " . $mail->ErrorInfo;
} else echo "Message sent!";
?>
This is the part of the code where I get the email address from. I have it so that the user inputs his email address into the textbox. it gets saved as EmailAddress
AddUser.php:
<div class="">
<label style=""> Email Address </label>
<input id="Email Address" type="email" name="EmailAddress" value="">
</div>
My sql query goes here. I include the Email.php file after it.
<?php
.
.
.
include('./content/Email.php');
?>
This is my button. When the user clicks it, the information gets stored and is supposed to send the email.
</select></div>
</div>
</div>
<br><br>
<input type="submit" class="btn btn-submit" value="Add User">
</div>

Related

Php script to sending email doesn't work

I created a simple form on my page and now I tried to add php script to sending email. Unfortunately it does not work. After clicking on the button, I want the user to remain on my side without redirection.
mail_sender.php
<?php
if(isset($_POST['submit'])){
$to = "someone#gmail.com";
$from = $_POST['email'];
$message = " You received the fallowing message:" . "\n\n" . $_POST['message'];
mail($to,$message,$from);
echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>
HTML
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" id="submit" value="Send">
</form>
First of all name attribute is missing in your submit button. And php mail function is wrong.
It should be:
$subject = "Your subject";
$headers = "From: $from ";
mail($to,$subject,$message,$headers);
instead of:
mail($to,$message,$from);
PHP's default mail() function doesn't work most of the times, especially with GMail. This is because your e-mail needs to be formatted in a special way to be accpeted by Google's mail server. You'll be better off using a mail library like PHPMailer.
Here's how to send an e-mail using PHPMailer from a GMail Account.
$mail = new PHPMailer();
// ---------- adjust these lines ---------------------------------------
$mail->Username = "xxx#gmail.com"; // your GMail user name
$mail->Password = "passwd"; // your GMail Password
$mail->AddAddress("yyy#gmail.com"); // recipients email
$mail->FromName = "Your Name"; // readable name
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
//----------------------------------------------------------------------
if(!$mail->Send())
{
echo "mail sent";
}
I tried everything and now i received message SMTP ERROR: Failed to connect to server and SMTP connect() failed
<form action="mail_sender.php" method="post">
<textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
<textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
<input type="submit" name="submit" id="submit" value="Send">
</form>
PHP
<?php
require "PHPMailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer();
$to = "someone#gmail.com"; // required
$from = $_POST['email'];
$comment =
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;
$mail->Username = "someone#gmail.com"; // your GMail user name
$mail->Password = "Password"; // your GMail Password
$mail->AddAddress("someone#gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';
//----------------------------------------------------------------------
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>

Form post email to certain users only

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;
?>

PHP Mailer is giving me error 404

Here is my html for my form, action and everything else should be correct, I'm thinking its probably my php?
<form class="form-inline" role="form" method="post" action="index.php">
<div class="form-group">
<label for="inputName">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Jane Doe">
</div>
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="jane.doe#example.com">
<button type="submit" class="btn btn-default" id="formBtn">Send Email</button>
</div>
</form>
Here is my PHP that I copied and then changed the details to fit myself from github.
<?php
require 'phpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$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#gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'email#gmail.com';
$mail->FromName = 'Bradley';
$mail->addAddress('email#gmail.com', 'Brad'); // Add a recipient
$mail->addReplyTo('email#gmail.com', 'Reply address');
$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 = 'Email from Vote Unanimous';
$mail->Body = 'You have a new email from your Vote Unanimous account. You should have a name and new email';
$mail->AltBody = 'You have a new email from your Vote Unanimous account. You should have a name and new email';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Thanks for helping out. I had to change my password in my gmail account to make it work

PHPMailer '$mail->From = $_POST['email'] not working

I have set up a contact form on my website that people can use to email me. The form allows them to leave their email behind so I can get back to them somehow. However for some reason no matter what email is left, the system seems to always think that I am the one who sent the email to my own email address, I am guessing this is due to the $mail->Username line of code being me? Although the from should set this, it seems it won't even if I enter the from address directly into the code.
This is the form I am using on my site to submit the form itself.
<form id="contact-form" name="contact-form" method="post" action="contact-form.php" enctype="application/x-www-form-urlencoded">
<input type="text" placeholder="Name" name="name" required="true">
<i class="fa fa-envelope"></i><input type="email" placeholder="Email" name="email" required="true">
<input type="text" placeholder="Subject" name="subject" required="true">
<textarea placeholder="Message" name="message" rows="7" required="true"></textarea>
<button type="submit" name="submit" value="submit">Send</button>
<button type="reset" value="reset">Reset</button>
</form>
The code for the php to be executed when the form is submitted is this.
<?php
require 'assets/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myemail#hotmail.co.uk'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $_POST['email'];
$mail->addAddress('myemail#hotmail.co.uk', $_POST['name']); // Add a recipient
$mail->addReplyTo('info#example.com', 'Information');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->AltBody = $_POST['message'];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Any help would be much appreciated, thank you.
Some systems just don't allow you to use a from address that's anything other than your own as an anti-forgery measure - because make no mistake, what you're trying to do is forge their address! It's also likely to run into problems with anyone that uses SPF, so you really should not try to do this.
Gmail will allow you to set specific aliases, or use your own domain.
reply-to is probably the most practical solution to this, or just provide a mailto link in the message body.
I think you don't have to "pretend" $mail->From = $_POST['email']; that someone have send you an email because as you can see it wont work. If you are trying to send email from someone to yourself just to have ability to push "reply to" button and write to this person use:
$mail->AddReplyTo($_POST['email']);
Email will be send from your mailbox to your mailbox but you will respond to email posted by the user.

How to disable PHPMailer redirect

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.

Categories