attaching generated pdf to form and send in phpmailer - php

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.

Related

How to send an email to multiple addresses?

How can I send an email to multiple email addresses? Right now this index.php page is being hosted online, and it connects to a send.php page, and I'd like to add another email text field that would be used to also send an email to the address entered in it.
Currently it sends an email to one email address that is entered in the email field in the form.
Index.php
<form id="contactus" method="post" action="send.php">
<input type="name" name="name" class="form-control" placeholder="Name" required>. <br/><br/>
<input type="email" name="email" class="form-control" placeholder="Email" required>. <br/><br/>
<input type="name" name="name2" class="form-control" placeholder="Player 2's Email Address" required><br/><br/>
<textarea name="message" class="form-control" placeholder="What Is Your Question?" required></textarea><br/><br/>
<input type="hidden" name="phone2">
<button type="submit" class="btn btn-success">Send Email</button>
<br><br>
</form>
Send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
$mail->addAddress($email);
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('email#gmail.com');
/*$mail->addBCC('bcc#example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
This is pretty simple - if you follow the code, you just need to duplicate some lines.
Duplicate your form field
<input type="email" name="email" class="form-control" placeholder="Email" required>
<input type="email" name="email_2" class="form-control" placeholder="Email" required>
Duplicate your validation
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
Duplicate the 'Add Address'
$mail->addAddress($email);
$mail->addAddress($email_2);
Fore readability sake in the code use an array and implode it to a comma separated string:-
$recipients = array(
"youremailaddress#yourdomain.com",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

PHPMailer not attaching files larger than 100KB - 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">

PHPMailer not sending from HTML form, returns blank page

I have a problem with PHPMailer on my website. In short, I want someone who wants to contact me to be able to send me an e-mail via contact form on my website. Said code looks like this:
index.html:
<form action="formularzeng.php" method="POST">
<div class="form-group">
<label for="InputEmail">E-mail address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="E-mail" name="email">
</div>
<div class="form-group">
<label for="InputName">Full name</label>
<input type="name" class="form-control" id="InputName" placeholder="Full name" name="name">
</div>
<div class="form-group">
<label for="InputText">Message</label>
<textarea id="InputText" class="form-control" rows="5" placeholder="Message" name="message"></textarea>
</div>
<input type="submit" class="btn btn-default" value="Send" name="submit">
</form>
And formularzeng.php looks like this:
<!DOCTYPE html>
<?php
$to = "example#gmail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact form";
$message = $name . " " . " wrote:" . "\n\n" . $_POST['message'];
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('example#gmail.com', 'Mailer');
$mail->addAddress('$to',);
$mail->addReplyTo('$from', 'Mailer');
$mail->isHTML(true);
$mail->Subject = '$subject';
$mail->Body = '$message';
$mail->AltBody = '$message';
And a html section below with all the content saying "Thank you for your message. After uploading all the files on the server, filling the form and clicking on "Send" all I get is a white page, without any content. I'm totally green with php, but I want a working form on my portfolio website. Why is it not working?
Change all of your '$var_name' to "$var_name" to make PHP parse your strings....
For example, turn this :
$mail->Subject = '$subject';
$mail->Body = '$message';
$mail->AltBody = '$message';
Into :
$mail->Subject = "$subject";
$mail->Body = "$message";
$mail->AltBody = "$message";
Turn out it was one coma too much in line:
$mail->addAddress('$to',);
removing it fixed the problem, now it's
$mail->addAddress('$to');
And it works.

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

PHPMailer not sending e-mail through Gmail, returns blank page

I am trying to set up a contact form at the bottom of my page. I am using PHPMailer and trying to receive emails at my gmail account. Every time I submit the form, my url changes to url/email.php and I get a blank page. What am I doing wrong? Can anyone see the glaring error that I am obviously missing?
HTML:
<div class="container-fluid">
<form action="email.php" id="contact-me" method="post" name="contact-me">
<div class="row-fluid">
<div class="form-group">
<div class="col-md-3 col-md-offset-3">
<input class="input" name="name" id="name" placeholder="Name" type="text">
</div>
<div class="col-md-3">
<input class="input" id="email" name="email"
placeholder="Email" type="text">
</div>
</div>
</div>
<div class="form-group">
<textarea class="input input-textarea" id="message" name="message" placeholder="Type your message here" rows=
"5"></textarea>
</div>
<input name="send" class="send" type="submit" value="Get In Touch">
</form>
</div>
</section>
PHP:
<?php
if (isset($_POST['submit']))
{
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
$name = strip_tags($_Post['name']);
$email = strip_tags($_POST['email']);
$msg = strip_tags($_POST['message']);
$subject = "Message from Portfolio";
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#gmail.com';
$mail->Password = '***********';
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress("me.com", "Katia Sittmann");
$mail->AddReplyTo($email, $name);
$mail->isHTML(true);
$mail->WordWrap = 50;
$mail->Subject =$subject;
$mail->Body = $msg;
$mail->AltBody ="No message entered";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}else{
header("Location: thank-you.html");
}
}
?>
You've got some very confused code here. You should start out with an up-to-date example, not an old one, and make sure you're using the latest PHPMailer (at least 5.2.10).
Don't do this:
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
All you need is this, which will auto-load the other classes if and when they are needed:
require 'PHPMailer-master/PHPMailerAutoload.php';
This won't work:
$name = strip_tags($_Post['name']);
PHP variables are case-sensitive, so do this:
$name = strip_tags($_POST['name']);
This will cause problems:
$mail->From = $email;
When you do this, you're forging the From address, and it will get rejected by SPF checks. Put your own address in here, and let the reply-to carry the submitter's address, as you're already doing.
Submit may not be included in the submission if the form is not submitted via that control, e.g. if it's submitted by hitting return in a text input. Check for a field that you know will always be in a submission:
if (array_key_exists('email', $_POST)) {...
Adding a try/catch will not achieve anything - PHPMailer does not throw exceptions unless you ask it to, and you are not.
You are applying strip_tags on the body, but then calling $mail->isHTML(true); - do you want HTML or not?
All that said, none of those things will cause a blank page. It's fairly likely you are running into gmail auth problems which are popular lately, and to see that you need to enable debug output:
$mail->SMTPDebug = 2;
and then read the troubleshooting guide on what you see.

Categories