I have two things. I have phpmailer, he sent the content of the form to a E-mailaddress, this works. And I have phpword, he make word file, this also works.
I have a question; how can I get the content of the form the $message (Full name, subject, phone, email and comments) in a Word(docx) file in a Email attachment if you click on the submit button?
With this code you see nothing in the browser, how can I 'mix' phpmailer and phpword?.
Can someone help me?
thanks in advance.
The form code is:
<?php
//phpword
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
$section->addText($message, array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
//phpmailer
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "test#gmail.com"; // Your full Gmail address
$mail->Password = "secret"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "form from website"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("test#gmail.com", "form from website"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" required/>
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" required/>
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
So basically you are trying to add the content of the variable $message to the word document BEFORE you even declared it.
Also, you don't define an "action" in your form, so it is just doing nothing when you click the submit button.
Assuming that the rest of your code works, this should do it:
<?php
if(isset($_POST['submit']))
{
//phpword
require_once '../PHPWord.php';
//phpmailer
require "phpmailer/class.phpmailer.php"; //include phpmailer class
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
$section->addText($message, array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "test#gmail.com"; // Your full Gmail address
$mail->Password = "secret"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "form from website"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("test#gmail.com", "form from website"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="url.php" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" required/>
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" required/>
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
Remember to replace "url.php" with the real URL of your page.
Regards
EDIT:
In order to be able to attach the Word file with phpmailer and reading the documentation (https://github.com/PHPMailer/PHPMailer/wiki/Tutorial):
The command to attach a local file is simply
$mail->addAttachment($path);, where $path contains the path to the
file you want to send, and can be placed anywhere between $mail = new
PHPMailer; and sending the message. Note that you cannot use a URL for
the path - you may only use local filesystem path. See notes on string
attachments below for how to use remote content.
Translated to your script, this means you have to add the following line:
[...]
$mail->AddAddress("test#gmail.com", "form from website"); // Where to send it - Recipient
$mail->addAttachment("Text.docx"); // <--------------------------
$result = $mail->Send(); // Send!
Related
Im trying to make a contact form, I searched alot on the web but didn't found any solution that works.
This is contact-form HTML and PHP:
<?php
if(isset($_POST['submit']))
{
$message=
'Name: '.$_POST['name'].'<br />
Subject: '.$_POST['message'].'<br />
Email: '.$_POST['email'].'
';
require 'class.phpmailer.php';
require 'PHPMailerAutoload.php';
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587; //Gmail SMTP port
// Authentication
$mail->Username = '**********#gmail.com'; // Your full Gmail address
$mail->Password = '**********'; // Your Gmail password
$mail->SMTPDebug = 1;
// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("*********#gmail.com", "Recipient Name"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="name" placeholder="Full Name" />
<br />
<input type="text" name="message" placeholder="Message" />
<br />
<input type="text" name="email" placeholder="Email" />
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
I get this Error.
I have the latest version of PHPMailer, Username and Password are correct.
Can someone please help?
In the php script you see that I use phpmailer and phpword. You use the form and he send to the E-mailaddress. The content of the form in a Word file.
But when I open the Word file he show between the fields the br tag(see with.jpgwith br tag).
But I want to have this without br tag: without br tag in Word
My question is: how get I a Word attachment without br tag with phpWord?
thanks in advance.
my code is:
<?php
if(isset($_POST['submit']))
{
//phpmailer
require "phpmailer/class.phpmailer.php"; //include phpmailer class
//phpword
require_once 'PHPWord.php';
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
Comments: '.$_POST['comments2'].'
';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
//add text
$section->addText($message, array('name'=>'Arial'));
$section->addTextBreak(2);//if this in comment the word file give also br tag
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "test#gmail.com"; // Your full Gmail address
$mail->Password = "secret"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "form from website"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("test#gmail.com", "form from website"); // Where to send it - Recipient
$mail->AddAttachment("Text.docx"); // attachment
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
the form:
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="url.php" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" required/>
<input type="text" name="subject" placeholder="Subject" />
<input type="text" name="phone" placeholder="Phone" />
<input type="text" name="emailid" placeholder="Email" required/>
<textarea rows="4" cols="20" name="comments" placeholder="Question/Comments"></textarea>
<textarea rows="4" cols="20" name="comments2" placeholder="Question/Comments"></textarea>
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
Well, the most flexible way would be to to write the values one by one (please note that you really should use always the htmlspecialchars function when writing text - especially as it is user input directly - into the word document):
/*
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
Comments: '.$_POST['comments2'].'
';
*/
// New Word Document
$PHPWord = new PHPWord();
// add the font style globally so that it can be accessed with the name
$PHPWord->addFontStyle('ArialText', array('name'=>'Arial'));
// New portrait section
$section = $PHPWord->createSection();
//add text
$section->addText(htmlspecialchars('Full Name: ' . $_POST['fullname'], ENT_COMPAT, 'UTF-8'), 'ArialText');
$section->addText(htmlspecialchars('Subject: ' . $_POST['subject'], ENT_COMPAT, 'UTF-8'), 'ArialText');
$section->addText(htmlspecialchars('Phone: ' . $_POST['phone'], ENT_COMPAT, 'UTF-8'), 'ArialText');
$section->addText(htmlspecialchars('Email: ' . $_POST['emailid'], ENT_COMPAT, 'UTF-8'), 'ArialText');
$section->addText(htmlspecialchars('Comments: ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');
$section->addText(htmlspecialchars('Comments: ' . $_POST['comments'], ENT_COMPAT, 'UTF-8'), 'ArialText');
If you wish to combine the values to a single message variable as done originally, you can also use addHtml function of PhpWord (just note that with this option you cannot define the font style):
PhpOffice\PhpWord\Shared\Html::addHtml($section, $message);
I'm trying to send email using my gmail account but i am getting an error
"SMTP Error: Could not connect to SMTP host".
I have tried port 587,465 25 but still its not working.
<?php
if(isset($_POST['submit']))
{
$message=
'Full Name: '.$_POST['fullname'].'<br />
Subject: '.$_POST['subject'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['emailid'].'<br />
Comments: '.$_POST['comments'].'
';
require "phpmailer/class.phpmailer.php"; //include phpmailer class
// Instantiate Class
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 465; //Gmail SMTP port
$mail->SMTPSecure = "tls";
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "rhlsngh302#gmail.com"; // Your full Gmail address
$mail->Password = "*********"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("rhlsngh302#gmail.com", "RahulName"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<div style="margin: 100px auto 0;width: 300px;">
<h3>Contact Form</h3>
<form name="form1" id="form1" action="" method="post">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" />
<br />
<input type="text" name="subject" placeholder="Subject" />
<br />
<input type="text" name="phone" placeholder="Phone" />
<br />
<input type="text" name="emailid" placeholder="Email" />
<br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea>
<br />
<input type="submit" name="submit" value="Send" />
</fieldset>
</form>
<p><?php if(!empty($message)) echo $message; ?></p>
</div>
</body>
</html>
How can I solve this problem?
Try:
$PHPMailer->SMTPOptions = array (
'ssl' => array (
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
works for me!
TLS on port 465 will not work - that port expects SSL. You should change it to port 587. You should not change to ssl for encryption as it's been deprecated since 1998.
It would also help if you read the docs and base your code on an up-to-date example.
First of all, while initiating PHPMailer send in the parameter true like,
$mailer = new PHPMailer(true);
This will help you to catch and handle exceptions.
Secondly, try this
$mailer->SMTPSecure = 'ssl'; // instead of tls
I am trying to send bulk email from gmail using php in which from address,to address and message are set from a form. But it is not working..shows error
Mail.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "username#gmail.com";
$mail->Password = "passwrd";
$mail->SetFrom("$_POST('from')","$_POST('from_name')");
$mail->AddReplyTo("$_POST('from')","$_POST('from_name')");
$mail->AddAddress("$_POST('to')","$_POST('from_name')");
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML("$_POST('message')");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Index.php
<form action="mail.php" method="POST">
<div>
From:
<input type="text" name="from" />
Name:<input type="text" name="from_name" />
</div>
<div>
to:
<input type="text" name="to" />
Name:<input type="text" name="to_name" />
</div>
<div>
Message:
<textarea name="message"></textarea>
</div>
<input type="submit" value ="Submit"/>
</form>
it shows the follwing error:
Invalid address: Array('from')
Invalid address: Array('from')
Invalid address: Array('to')
Mailer Error: You must provide at least one recipient email address.
Someone please help me to solve this problem. I am not getting what the problem is.
Cargo-cult programming:
$mail->SetFrom("$_POST('from')","$_POST('from_name')");
should probably just be
$mail->SetFrom($_POST['from'], $_POST['from_name']);
Note the use of [] instead of (), and the LACK of " quotes around those single values.
You'll have to fix up every line of code in your script where you're doing this sort of bad array referencing.
i am trying to add atatchment while sending mail from gmail using php. But it shows error...it says Could not access file: hai.jpg
follwing is the code i use
gmail.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
access to that
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "name#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
$mail->AddAttachment($_POST['attachment'],'application/octet-stream');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
index.html
<form action="gmail.php" method="POST">
<div>
From Name:<input type="text" name="from_name" />
From:<input type="text" name="from" />
</div>
<div>
To Name:<input type="text" name="to_name" />
To:<input type="text" name="to" />
</div>
<div>
Message:<textarea name="message"></textarea>
</div>
<div>
Attachment:<input type="file" name="attachment" />
</div>
<input type="submit" value ="Submit"/>
</form>
I dont know what if i am doing the right way here.Can anyone please guide me through this?
I've notice the absence of two itens in your code:
The use of the php $_FILES variable, which stores information about the uploaded files
The use of enctype attribute in your form element, which is required to upload files along with the form.
So, your code must treat this two itens.
Your form will look like this:
<form action="gmail.php" method="POST" enctype="multipart/form-data">
<div>
From Name:<input type="text" name="from_name" />
From:<input type="text" name="from" />
</div>
<div>
To Name:<input type="text" name="to_name" />
To:<input type="text" name="to" />
</div>
<div>
Message:<textarea name="message"></textarea>
</div>
<div>
Attachment:<input type="file" name="attachment" />
</div>
<input type="submit" value ="Submit"/>
</form>
And your code may treat the $_FILES variable:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
//access to that
date_default_timezone_set('Etc/UTC');
require '../Mail/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "name#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "passwrd";
$mail->SetFrom($_POST['from'], $_POST['from_name']);
$mail->AddReplyTo($_POST['from'],$_POST['from_name']);
$mail->AddAddress($_POST['to'],$_POST['to_name']);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->MsgHTML($_POST['message']);
$mail->AltBody = 'This is a plain-text message body';
//Attachs the file only if it was uploaded by http post
if (is_uploaded_file ($_FILES['attachment']['tmp_name'])) {
$mail->AddAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'], 'base64',$_FILES['attachment']['type']);
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
Its not quite that straight forward uploading a file from the client browser to the server.
Here's a simple tutorial