PHPMailer not attaching files larger than 100KB - PHP - php

I am trying to set up a contact form which handles multiple file attachments. I am using PHPMailer and built the below script from the PHPMailer example for attaching multiple files.
The below script works great until attachments exceed 100KB. If a file is larger than 100KB, it is skipped when attaching. Only files smaller than 100KB are attached and sent.
I have seen this StackOverflow question which looked promising, but the values in my machine's php.ini file were all set to 32MB or higher.
I am using Mailgun as the SMTP server, and can see in the logs that the attachments that exceed 100KB aren't getting to Mailgun at all so it must have something to do with this script or my PHP environment.
Can anyone help me resolve this?
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$host = 'smtp.mailgun.org';
$username = 'postmaster#domain.com';
$password = 'password';
$email_from = 'from#domain.com';
$email_to = 'to#domain.com';
$send = false;
$subject = "Quote Request from Website";
$name = addslashes(strip_tags($_POST['name']));
$email = addslashes(strip_tags($_POST['email']));
$project_type = addslashes(strip_tags($_POST['project_type']));
$message = addslashes(strip_tags($_POST['message']));
$htmlmessage = <<<MESSAGE
<html>
<head>
<title>$subject</title>
</head>
<body>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Project Type:</strong> $project_type</p>
<p><strong>Message:</strong> $message</p>
</body>
</html>
MESSAGE;
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Host = $host;
$mail->Port = 587;
$mail->setFrom($email_from, $name);
$mail->addAddress($email_to);
$mail->addReplyTo($email, $name);
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
// Attach multiple files one by one
$total = count($_FILES['attachments']['tmp_name']);
echo $total;
for ($ct = 0; $ct < $total; $ct++)
{
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['attachments']['name'][$ct]));
$filename = $_FILES['attachments']['name'][$ct];
if (move_uploaded_file($_FILES['attachments']['tmp_name'][$ct], $uploadfile)) {
echo $filename;
$mail->addAttachment($uploadfile, $filename);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
echo $msg;
}
// $name = $_FILES['attachments']['name'][$ct];
// $path = $_FILES['attachments']['tmp_name'][$ct];
// echo $name . ' - ' . $path . '<br>';
// $mail->addAttachment($path, $name);
}
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlmessage;
// $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';
}
The form:
<form action="contact/quote.php" method="post" id="quote-form" class="validate" role="form" enctype="multipart/form-data">
<label>Name</label>
<input type="text" name="name" id="name" required>
<label>Email</label>
<input type="email" name="email" id="email" required>
<label>Project Type</label>
<select name="project_type" id="project_type" required>
<option value="" selected>Please Select</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
<label>Upload Files</label>
<input multiple="multiple" type="file" name="attachments[]" value="">
<label>Message</label>
<textarea name="message" id="message" rows="5" required></textarea>
<button type="submit" id="submit">Submit</button>
</form>
Any help would be much appreciated!
Thanks.

You're missing the MAX_FILE_SIZE option in your form, which won't help, and it defaults to 100k, which exactly matches what you're seeing. See the docs.
The send_file_upload example provided with PHPMailer shows how to set it correctly.
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

Related

attaching generated pdf to form and send in phpmailer

I am able to generate pdf and send that generated pdf as attachment to entered email-id. But i want something like, when i click on email it should generate pdf and open a form with user entry fields like To, Message, Subject.... etc... And that generated pdf should show as attachment
Now when click on email it will open an user entry form then when i click on send , it will generate pdf and send that pdf.
Here is my code
My html form
<form method="post" action="send_mail.php" enctype="multipart/form-data">
<div class="formSep">
<label class="req">To Email: </label>
<input type="text" name="email" /> <select name="email"><option value="">Select one</option><?php $s1 = mysql_query("select * from lead_contact where company_id=".$company."");
while($r1 = mysql_fetch_array($s1)) { $name = $r1['firstname'].' '.$r1['lastname'];
$cid = $r1['con_id'];
$cemail = $r1['email']; ?>
<option value="<?php echo $cemail;?>"><?php echo $name;?></option>
<?php
}
?>
</select>
</div>
<input type="hidden" name="order_id" value="<?php echo $order_id; ?>" />
<input type="hidden" name="company" value="<?php echo $company; ?>" />
<div class="formSep">
<label class="req">Subject</label>
<input type="text" name="subject" /></div>
<div class="formSep">
<label class="req"> Message</label>
<div class="w-box" id="n_wysiwg">
<div class="w-box-header">
<h4>WYSIWG Editor</h4>
</div>
<div class="w-box-content cnt_no_pad">
<textarea name="message" id="wysiwg_editor" cols="30" rows="10"></textarea>
</div>
</div>
</div>
<div class="formSep">
<input type="submit" name="submit" value="Submit" class="btn btn-info" /></div>
</form>
send_email.php
<?php
if($_POST['submit'] == "Submit")
{
$id = $_POST['order_id'];
$company = $_POST['company'];
include("../admin_auth.php");
include("../connect.php");
require('invoice.php');
$pdf = new PDF_Invoice( 'P', 'mm', 'A4' );
$pdf->AddPage();
//MY PDF CODE GOES HERE
$pdf->Output("D:/wamp/www/folder/uploads/".$id.".pdf","F");
$path = "D:/wamp/www/folder/uploads/".$id.".pdf";
require("class.phpmailer.php");
require("class.smtp.php");
$to = $_POST['email'];
$from = $_SESSION['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->Host = "hostname";
$mail->SMTPAuth = true;
$mail->Username = 'user';
$mail->Password = 'password';
$mail->Port = 587;
$mail->From=$_SESSION['email'];
$mail->FromName=$_SESSION['name'];
$mail->Sender=$email;
$mail->AddAddress($to);
$mail->AddBCC("test#test.com");
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
$mail->Subject = $subject;
$mail->CharSet="windows-1251";
$mail->CharSet="utf-8";
$mail->IsHTML(true);
$mail->Body = $message;
if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;;
}
else
{
echo "Email Sent!";
}
}
?>
I am not getting how to get the form with pdf attached, so that user can write in the body , add cc and sent email
Try this..
$path = "D:/wamp/www/folder/uploads/".$id.".pdf";
$mail->addCC('cc#example.com'); //Cc stands for carbon copy
$mail->addBCC('bcc#example.com'); //Bcc stands for blind carbon copy
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
NOTE: FOR REFERENCE OF PHP MAILER :
Check This: https://github.com/PHPMailer/PHPMailer
Cc stands for carbon copy which means that whose address appears after
the Cc: header would receive a copy of the message. Also, the Cc
header would also appear inside the header of the received message.
Bcc stands for blind carbon copy which is similar to that of Cc except
that the Email address of the recipients specified in this field do
not appear in the received message header and the recipients in the To
or Cc fields will not know that a copy sent to these address.
It really helps if you get your basic PHP syntax right. This is just meaningless:
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
All you need is this:
$mail->AddAttachment($path);
It will take care of the rest for you.

phpmailer not sending variables

I am trying to use phpmailer to send a user's input from an html form
Form is like this
<form action="senditpm.php" role="form">
<input type="text" class="form-control" id="name" name="name"><br>
<input type="email" class="form-control" id="email" name="email">>br>
<input type="text" class="form-control" id="phone" name="phone">>br>
<input type="submit" value="Submit" class="submit-button btn btn-default">
</form>
senditpm.php is like this
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'] ;
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxxxxxxxx';
$mail->SMTPAuth = false;
$mail->Username = 'xxxxxxxx';
$mail->Password = 'xxxxxxxx';
$mail->Port = 25;
$mail->setFrom('xxx#xxxxs.co.uk', 'xxxxx');
$mail->addAddress('xxx#xxx', 'xxx xxxx');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body="
Name: $name <br>
Email: $email <br>
Phone: $phone <br>";
;
$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';
}
?>
When I complete the form and click Submit, I receive an email like this
Name:
Email:
Phone:
As you can see, the php isn't sending the variables
Please could someone let me know where I am going wrong. Thanks in advance
<form> defaults to a GET method if a POST method is not implied and you are using POST arrays.
So change
<form action="senditpm.php" role="form">
to
<form action="senditpm.php" role="form" method="post">

phpmailer add more contents to body of email

I am brand new to phpmailer.. i have made some progress by creating a simple form that allows users to send emails to our inbox:
<form id="contact-form" class="contact-form" method="post" action="send-mail.php">
<input type="text" name="name" id="name" placeholder="Name" tabindex="1">
<input type="text" name="subject" id="trade" placeholder="Your Trade" tabindex="3">
<input type="text" name="email" id="email" placeholder="Your Email" tabindex="2">
<input type="text" name="number" id="number" placeholder="Contact Number" tabindex="4">
<textarea id="message" rows="8" name="message" placeholder="Message" tabindex="5"></textarea>
<div class="grid-col one-whole">
<button type="submit">Send Your Message</button>
</div>
</form>
.php:
<?php
require('PHPFiles/PHPMailerAutoload.php');
require('PHPFiles/class.smtp.php');
require('PHPFiles/class.phpmailer.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->IsSMTP();
$mail->Host = '74.208.5.2';
$mail->SMTPAuth = true;
$mail->Username = 'info#mydomain.com';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; connect to
$mail->From = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->FromName = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->addAddress('info#mydomain.com');
$mail->Subject = isset($_POST["subject"]) ? $_POST["subject"] : "";
$mail->Body = str_replace("\n",'<br>', $_POST['message']);
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
At the moment, the Body of the email contains the contents of the textare field. How can the body contain this, but then also a line break, then, the contents of the number field?
You might want to try this. Make a separate variable for all the data that needs to be in the body of the message.
$message = "Name :- " . $_POST['name'] . "<br>" . " Number :- " . $_POST['number'];
$mail->Body = $message;
so doing it like:
$mail->Body = str_replace('\n','<br />', $_POST['message']);
or:
$mail->Body = str_replace("\\n","<br />", $_POST["message"]);
should fix the problem of string getting the "line breaks".
btw. it`s always good to:
var_dump($data);
die();
to see what are you getting at each step of a code

Calling AddAttachment in PHPMailer does not send mail

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.

phpmailer secure approach by placing credentials out of the web root in INI file

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

Categories