I am implementing mail utility in my PHP web application. It works fine for simple email. But if try to attach any file, send function of PHPMailer is not called i.e. mail is not sent and it is not showing any error also. Below is my sample code:
HTML Form
<form action="Email.php" enctype="multipart/form-data" method="POST">
<input type="text" name = "to" id="to">
<br>
<input type="file" name="file" id="file">
<br>
<input type="text" name="subject" id="subject" size="155"><br>
<textarea rows="10" cols="50" name="msg" id="msg">
</textarea>
<br>
<input type="submit" value="Send Mail">
</form>
PHP Code
include 'library.php';
include "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->Username = USERNAME;
$mail->Password = USERPASS;
$mail->SetFrom(USERNAME);
$mail->Subject =$_POST['subject'];
$mail->MsgHTML($_POST['msg']);
$mail->IsHTML(true);
$mail->AddAddress($_POST['to']);
if (!empty($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES["file"]["tmp_name"],"Upload/" . $_FILES["file"]["name"]);
if(!$mail->AddAttachment("Upload/" . $_FILES['file']['name']))
{
echo "Error in attachments";
}
else
{
echo "File Attached";
}
}
$result = $mail->Send();
echo $result;
if(!$result) {
echo 'Error sending email' . $mail->ErrorInfo;
}
else{
echo "Email Sent";
}
Output of this code is following:
File Attached
After this no message is displayed and also mail is not sent from the id provided. But if I don't add any attachments it will work fine.
I have resolved this problem. Solution is I was trying to add file with 350KB which was taking more time and was throwing error of exceeded 30 sec. I just increased my time limit and problem is solved. Thank you all for helping.
Related
I've finally built my first website and gotten the hosting figured out. As stated in the title I'm using hostinger and phpmailer, I have never used PHP so I went through their short tutorial and got the PHP file set up how I feel is the correct way.
The issue I'm having is that when I submit the form, I get an error page. Nothing in the network tab on chrome suggests that it is trying to send any data, although it does show it opening the PHP file, I think, again this is my first website and first time using PHP.
PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'hostinger-email#domain.com';
$mail->Password = 'password for hostinger email';
$mail->setFrom('same-as-username??', 'Admin');
$mail->addAddress('destination#gmail.com', 'Savannah');
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'PHPMailer contact form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
$msg = 'Share it with us!';
}
?>
HTML FORM:
<header class="form_header">Contact Me</header>
<form id="form" class="topBefore" action="/domains/piecesofblonde.com/public_html/formscript.php" method="post">
<input id="name" type="text" placeholder="NAME">
<input id="email" type="text" placeholder="E-MAIL">
<textarea id="message" type="text" placeholder="MESSAGE"></textarea>
<input id="submit" type="submit" value="GO!">
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm running locally from my mac, when the info is entered and I click send the email is not showing up in the outgoing email account in sent mail, and not showing up in the incoming email either; spam folder included. There is not an error echo. The echo states the message is sent without error. I'm at a loss here. By the way I am a newbie at coding so please be gentle and explain in depth. I appreciate any help that anyone might be able to provide. Here is my code.
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" />
</p>
<p>
Subject:
</p>
<p>
<input type="text" name"subject" />
</p>
<p>
Note:
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file" />
</p>
<input type="submit" name="submit" value="Send Email" />
</form>
<?php
if (isset($_POST['submit'])) {
require "php-mailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer;
try {
$sender = "????????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '$sender';
$mail->Password = '????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
$mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>
The code below will send a message for me. I replaced the ??? with my username and password (also had to set my gmail account to allow access).
Changes I made to your code:
I changed the require for my composer locally.
You used single quotes around the $sender - needs to be double quotes.
Added titles to the inputs (probably not needed).
Also used the namespaced values for instantiating $mail PHPMAILER();
Commented out the attachment feature.
I would also recommend checking into sanitizing your user input.
Something like:
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL));
Code:
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" title="receiver1"/>
</p>
<p>
Subject:
</p>
<p>
<input type="text" name="subject" title="subj1"/>
</p>
<p>
Note:
</p>
<p>
<textarea name="message" title="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file"/>
</p>
<input type="submit" name="submit" value="Send Email"/>
</form>
<?php
require '../vendor/autoload.php';
if (isset($_POST['submit'])) {
$mail = new \PHPMailer\PHPMailer\PHPMailer();
try {
$sender = "?????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "$sender";
$mail->Password = '?????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
// $mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
}
}
?>
Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.
After some of edited my email.php source code,
Here,
<?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 = "mygmailpassword"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
$mail->SetFrom('rajaselva.csc#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";
?>
here is 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>
after run, it shows error like this,
Message could not be sent.
Mailer Error: Extension missing: openssl
Note: I referred similar questions in stack, but it didn't help me, so only i posted new one. and also i m new to php, but i want to know particular this section.
after all, i removed semicolon in this line extension=php_openssl.dll in php.ini file
but still stays same error..
Can anybody help?
Thanks,
first of all thank you for your time and helping me on this.
I have a simple contact form and i'm using phpmailer.
I want to store credentials in an INI file out of webroot and then include it in my mail.php file which is the mail sending script.
How to write the INI content and how to call them on mail.php file?
This is my HTML file which is contact us form:
<h3><font style="line-height:170%" size="4"> <a id="contactus">Contact us form</a> </font></h3>
<form method="post" action="email.php" accept-charset="UTF-8">
<p>
<label>your name</label>
<input name="dname" type="text" size="30" />
<label>your email</label>
<input name="demail" type="text" size="30" />
<label>your domain name</label>
<input name="ddomain" type="text" size="30" />
<label>Message</label>
<textarea name="dmessage" id="dmessage" rows="5" cols="5"></textarea>
<br />
<input class="button" type="submit" value="submit"/>
</p>
</form>
<br />
and this is the email.php file:
<?php
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "info#example.com"; // SMTP username
$mail->Password = "this is the password"; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
I want to store credentials in a safe place.
Another problem is that whenever someone open email.php file in browser ( example.com/email.php) an empty email will be sent to me, how to prevent it?
I want the email.php file to be executed only a result of a customer's contact via filling the form and not by directly opening the email.php file
Thank you all
<?php
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
$constants = parse_ini_file("/outside/web/sample.ini");
header('Content-type: text/plain; charset=utf-8');
$email = $_REQUEST['demail'] ;
$message = $_REQUEST['dmessage'] ;
$ddomain = $_REQUEST['ddomain'] ;
require("PHPMailer_v5.1/class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = $constants['username']; // SMTP username
$mail->Password = $constants['password']; // SMTP password
$mail->From = $email;
$mail->AddAddress("info#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $ddomain;
$mail->Body = $message;
$mail->AltBody = $ddomain;
if(!$mail->Send())
{
echo "Error <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "we have received your email";
?>
INI file (sample.ini) looks like:
username = "info#example.com"
password = "PASSW0RD"
Thank you for your helps.
I did some change on your code and it is excatly what is want.
instead of saying:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
die();
}
i used this one:
if (empty($_REQUEST['demail']) || empty($_REQUEST['dmessage'])) {
echo "Please fill-in Email and Message sections completely!";
exit;
}
Thank you for your help