PHP Mailer Coding Issue - php

I am having an issue with getting my PHP mailer code to work correctly. I can get it to send an email on page load fine, the issue is with the ISSET command to get it to submit the form once the submit button has been pressed. I have tried putting it in various places but still can't get it to work. Any help would be appreciated.
<?php
require '/phpmailer/PHPMailerAutoload.php';
require_once '/phpmailer/class.phpmailer.php';
include '/phpmailer/class.smtp.php';
if(isset($_POST["Submit"]))
{
$emailaddress = '****#**********';
$message=
'Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "********"; // SMTP server
$mail->SMTPDebug = 1; // 1 = errors and messages,2 = messages only
$mail->SMTPAuth = false; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->SMTPSecure = 'false'; // Enable encryption, 'ssl' also accepted
$mail->CharSet = 'UTF-8'; // so it interprets foreign characters
$mail->setFrom('***********');
$mail->AddReplyTo('**********');
$mail->Subject = "Contact form submission from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Thank you, your message has been sent!";
}

I'm going to make my comment as an answer, because that is the only conclusion I can come up with and the most likely to be, without seeing the OP's HTML form.
Your conditional statement if(isset($_POST["Submit"])) is based on a submit button named Submit. If your submit button has name="submit" instead of name="Submit", then that would explain it. If it isn't named, then do.
I.e.:
<input type="submit" name="Submit" value="Send Email">
is not the same as
<input type="submit" name="submit" value="Send Email">
POST variables are case-sensitive.
Your presently shown code has the proper bracing.
Having used error reporting and placed the top of your file(s) right after your opening <?php tag
error_reporting(E_ALL);
ini_set('display_errors', 1);
would have signaled an Undefined index Submit... warning message.

Related

Get data from database and use in mail function in php

I am new to php and currently doing a project
I stored data in database by using a form.
I have a mail function, when on clicking the submit button, a mail is send to reputed email id. I am using phpmailer library.
This is my mail function
class mail
{
public function sendMail(){
require 'vendor/autoload.php';
$mail = new PHPMailer(); // Passing `true` enables exceptions or null without exception
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$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 = 'xyz#gmail.com'; // SMTP username
$mail->Password = 'xyz123'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('xyz#gmail.com', 'xyz');
$mail->addAddress('abc#gmail.com', 'abc');
$mail->addCC('zxc#gmail.com');
//Attachments
$mail->addAttachment('lob.png', 'sample.png'); // Add attachments
// Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Report';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if (!$mail->send()) {
return "Error sending message" . $mail->ErrorInfo;
} else {
return "Message sent!";
}
}
}
This is my code to store data into database as send.php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$quality = $_POST['quality'];
$sql = "INSERT INTO user(name,age,quality) VALUES('$name','$age','$quality')";
}
This is my html form
<form class="quality" method="POST" action="send.php">
<label for="name">Name</label>
<input type = "text" class = "form-quality" name="name" value="" / >
<label for="age">Age</label>
<input type="text" class ="form-quality" name="age" value="" / >
<label for="quality">Quality</label>
<input type="ratio" class ="form-quality" name="quality" value="E" / >
<input type="ratio" class ="form-quality" name="quality" value="P" / >
</form>
1) How to get the data's from database ?
2) In mail function previously sending emails to one or two id's, but i need as when i select "value = E" from ratio button , it should send to one email and if i select "value= P" it should send to another email based on user selected values stored in the database
Anyone with a best reply will be much more needed help for me
Assuming that when you said you are doing a project is within an education environment and not for professional purpose:
When you get the data from you form on if(isset($_POST['submit'])) send those variables to the sendMail function as parameters, then inside the function you can make a variable using your parameters to build a well formed message that you can send in your $mail->Body .
Inside the function you can also check if what you recieved on your radio-button input was "E" or "P" and give different values (receivers email) and pass it to your addAddress.
If this is not a school project you MUST be very carefully on what you send or you insert to the db. Validate everything you recieve on your form before sending and go ahead and check the link in the comments about SQL injection.
This should've been a comment insetead of a reply but i don't have enough reputation to comment so sorry about that.

Issues in mail received time when using hotmail in php mailer

Here is code for sending email from localhost after i referred a lot from online.
html form:
<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
// $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');
require 'PHPMailerAutoload.php';
require 'class.smtp.php';
$mail = new PHPMailer();
$body='hellooooo';
$mail->IsSMTP();
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mygmailid#gmail.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
$mail->SetFrom('xxxxxxxx#gmail.com','Selva Rani');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
$mail->MsgHTML($body);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
so when i run my code, i received mail but time is correct in gmail,
If i use hotmail means, the received time is not correct.
May i know know?
Anyone can help me?
Thanks,
Note: I m new to php, but i want to know particular this one and fix the problem. and I referred similar questions in stack, but it didn't help me,
Can anybody help me to fix this?
Thanks,
when i check my email acc, it shows like this,
so in that image, the time showed., 8.48AM, it is not correct according to indian time, it need to show 2.05PM ..
According to my question,
For hotmail time settings,
I modified my hotmail acc, after that it will work,
The steps are,
Log into your Hotmail Account
Click on Options in the upper right corner of the page
Under "Manage Your Account, click on the option "View and edit your personal information"
One the Account Summary page, click the link name "Registered information" under your birthdate
Select your correct country in the Home Location section
Select your correct time zone in the Time Zone section.
Click on the Save tab

PHP mail send to Multiple recipients

I have some PHP code that I'm using to send email to a specific e-mail address. However, I'd like to include a couple more e-mail addresses in the PHP for when it sends it.
when i tried it showing the following
ERROR:Mailer Error: You must provide at least one recipient email address.
code
include "class.phpmailer.php"; // include the class file name
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxx";
$mail->Password = "xxxxx";
$mail->SetFrom("cpn#xxxxxxx.com");
$mail->Subject = $sub1;
$mail->Body = $text_mail;
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "Message has been sent";
}
any one guide me how to do it
Change this line
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
to this:
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
You can run that function as many times as you like until you've got all the addresses you need.
See Here for more info
It seems that you are miss using the AddAdress method. You should pass every mail separatly like that :
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
See PHPMailer AddAddress() for more details.
If you want to send email to multiple address, You need to call the AddAddress() function for each and every Email address. First parameter is EMAIL address, Second one is Recipient name and it is optional.
$mail->AddAddress("xxxxxxx#gmail.com", "XXXXXXXX");
$mail->AddAddress("aral#xxxxxx.com", "Aral");
Try this
$mail->AddAddress("xxxxxxx#gmail.com");
$mail->AddAddress("aral#xxxxxx.com");
$mail->AddAddress("akhader#xxxxxx.com");
Take a look here PHPMailer AddAddress()
and keep an eye of your line:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
Instead of:
$mail->AddAddress("xxxxxxx#gmail.com;aral#xxxxxx.com;akhader#xxxxxx.com");
You should use
$mail->AddAddress('xxxxxxx#gmail.com', '1');
$mail->AddAddress('aral#xxxxxx.com', '2');
$mail->AddAddress('akhader#xxxxxx.com', '3');
Or use CC copies:
$mail->AddCC('xxxxxxx#gmail.com', '1');
$mail->AddCC('aral#xxxxxx.com', '2');
$mail->AddCC('akhader#xxxxxx.com', '3');
Try this.... This may help you
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("chauhanamrish32#gmail.com,amrinderpuri1990#gmail.com,amrinder_puri#yahoo.com,amrindersinghpuri13#gmail.com", $subject,$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>

Send email with attachment via PHPMailer

I'm preparing to create a form page for a website that will require many fields for the user to fill out and that will be sent to a specified email.
So far I've created a dummy php email page that gets your Message, 1 attachment, and Recipient Email address using Google's SMTP.
Here's my code for uploadtest.html:
<body>
<h1>Test Upload</h1>
<form action="email.php" method="get">
Message: <input type="text" name="message">
Email: <input type="text" name="email"><br>
Attach File: <input type="file" name="file" id="file">
<input type="submit">
</form>
</body>
uploadtest.html is what the user will see
Here's the code for email.php:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$recipiant = $_GET["email"];
$message = $_GET["message"];
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->SMTPSecure = 'ssl';
$mail->Username = "xxxxx#gmail.com"; // SMTP account username
$mail->Password = "xxxxxxxx"; // SMTP account password
$mail->AddAttachment($_FILES['tmp_name']); //****HERE'S MY MAIN PROBLEM!!!
$mail->SetFrom('cinicraftmatt#gmail.com', 'CiniCraft.com'); // FROM
$mail->AddReplyTo('cinicraftmatt#gmail.com', 'Dom'); // Reply TO
$mail->AddAddress($recipiant, 'Dominik Andrzejczuk'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = $message;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
So from what I can tell here, PHPMailer's AddAttachment() method takes as a parameter the URL of the file DIRECTORY you want attached. And this is where my main problem is.
What would the name of the variable be that would get the location of my file (dir/upload.jpg) that I've uploaded so I could use it as a parameter in the AddAttachment() method?
No, it doesn't take URLs, or directories. It takes a direct path to a FILE.
e.g.
$mailer->AddAttachment(
'/path/to/file/on/your/server.txt',
'name_of_file_in_email',
'base64',
'mime/type'
);
The path is self-explanatory. The name_of_file_in_email allows you to "rename" the file, so that you might loaded a file named "foo.exe" on your server, it can appear as "bar.jpg" in the email the client receives.
Your problem is that you're trying to attach an uploaded file, but using the wrong source. It should be
<input type="file" name="thefile" />
^^^^^^^
$_FILES['thefile']['tmp_name']
^^^^^^^
Note the field name relationship to $_FILES.
This will be send like this through PHPMailer Class
$mail->AddAttachment($_FILES['tmp_name'],$_FILES['name']); //****HERE'S MY MAIN PROBLEM!!! So you would have to give file name also.
please try with this
include "phpMailerClass.php";
$email = new PHPMailer();
$email->From = $to;
$email->FromName = $_POST['name'];
$email->Subject = $subject;
$email->Body = $EmailText;
$email->AddAddress( 'shafiq2626#hotmail.com' );
$email->IsHTML(true);
$file_to_attach = $_FILES['file']['tmp_name'];
$filename=$_FILES['file']['name'];
$email->AddAttachment( $file_to_attach , $filename );
$email->Send();
This is code is working fine.
Your <form> tag should specify the enctype attribute like so :
<form ... enctype="multipart/form-data">
...
</form>
Use the following code:
$objectMailers->AddAttachment("file.extension");
tested on PHP 7 and using PHPMailer 5.3

How to get user mails in my free gmail inbox through contact us form on my website

How to get user mails in my free gmail inbox through contact us form on my website. I do not use email with my website name . i use free gmail. I tried many script but the all need email account on domain.
Does this question have something to do with This One?
Well, so you have a form on your site, your users fill it up and you need an email with that data, right?
Simple example:
<form action="sendMail.php" method="post">
Name: <input type="text" name="name" id="name" /><br />
Email: <input type="text" name="email" id="email" /><br />
Text: <textarea name="text"></textarea><br />
<input type="submit" value="Send" />
</form>
then, the php page wich send the mail:
//php sendThis.php page
<?php
require("class.phpmailer.php");
$name = $_POST['name'];
$email = $_POST['email'];
$text = $name . ', ' . $email . ' has filled the form with the text:<br />' . $_POST['text'];
$from = 'your.email#gmail.com';
$to = 'your.email#gmail.com';
$gmailPass = 'your gmail password';
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = $from;
// GMAIL password
$mail->Password = $gmailPass;
$mail->From = $from;
$mail->FromName = $from;
$mail->AddReplyTo($from, $from);
$mail->Subject = 'This is a test!';
$mail->Body = $text;
$mail->MsgHTML($text);
$mail->IsHTML(true);
$mail->AddAddress($to, $to);
if(!$mail->Send()){
echo $mail->ErrorInfo;
}else{
echo 'sent!';
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
EDIT: just tested and works fine. Make sure that the 3 files (class.phpmailer.php, class.pop3.php and class.smtp.php) are in the correct include path
Basically, it involves the PHP mail() function:
<?php
mail(yourGmailAddress, object, message);
?>
As you have already observed, this solution works only if the webserver operates a mail server. This mail server may forbid unknown users. So you need to have an email account on that web/mail server (I believe this is the case). The second step then is to forward mail from your website address to you gmail account. I am 90% certain that it is possible from your gmail configuration. It may also be possible from your website mail configuration. But don't configure both!
Try enabling openssl.
Uncomment the line:
extension=php_openssl.dll
in your php.ini file.
You can also put your email address into "action" attribute of the form element. But that's very unreliable. Like this:
<form method='post' action='mailto:your#email.com?Subject=Hello'>
...
</form>
Users must have email client installed and configured for this to work properly. There are some other drawbacks. You have to do some research to find whether this method is for you or not.
http://www.google.com/search?client=opera&rls=en&q=form+action+mailto&sourceid=opera&ie=utf-8&oe=utf-8

Categories