Email attachments that are uploaded from web form with SwiftMailer - php

I am trying to use swftmailer to - upload a file (jpg,ppt,pdf,word etc with a max upload of eg: 6MB) from a form on a html web page, - email the content of the form with the attached file via swiftmailer.
My form is created & I am using $_POST to capture the field values in a thankyou.php page. The email is sending, but I cant get the file to attach!
The upload/attach file needs to be an option, I found a resource online that only sends the info if a file is uploaded, which is not always going to be the case, so I have turned to swiftmailer which looks great, but in the documentation/examples it specifies a path to upload the attachment too, I just want to be able to read in the file (not specify a path) and send it as an attachment to the given email address along with the other form fields, Name Email,Phone & Message.
Any help would be greatly appreciated.
My HTML (contact.html)
<div id="form-main">
<div id="form-div">
<form class="form" method="post" action="thank-you.php" enctype="multipart/form-data">
<p class="name">
<input name="fieldFormName" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="email">
<input name="fieldFormEmail" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
</p>
<p class="phone">
<input name="fieldFormPhone" type="number" class="validate[required,custom[onlyLetter],length[0,20]] feedback-input" placeholder="Phone" id="phone" />
</p>
<p class="text">
<textarea name="fieldDescription" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
</p>
<p class="upload">
<small>Send us a Drawing or file</small>
<input name="fieldAttachment" id="attachment" class="feedback-input" type="file">
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</div>
My PHP Code:
<?php
require_once 'lib/swift_required.php';
$fromEmail = $_POST['fieldFormEmail'];
$fromName = $_POST['fieldFormName'];
$fromPhone = $_POST['fieldFormPhone'];
$fromMessage = $_POST['fieldDescription'];
$fromAttachment = $_POST['fieldAttachment'];
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
"sender#domain.com" => "Sender Name"
));
$message->setSubject("This email is sent using Swift Mailer");
$message->setBody(
"From: " . $fromName . "\n" .
"Email: " . $fromEmail . "\n" .
"Phone: " . $fromPhone . "\n" .
"Message: " . $fromMessage . "\n"
);
$message->setFrom("$fromEmail", "$fromName");
// *** This is the part I am struggling to understand *** //
$message->attach(Swift_Attachment::newInstance('$fromAttachment'));
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
?>
Thank you in advance

Replace your code
$message->attach(Swift_Attachment::newInstance('$fromAttachment'));
on this
$message->attach(
Swift_Attachment::fromPath($_FILES['fieldAttachment']['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
);

I have taken DarveL's code and added an if statement to to check if a file is uploaded because I still want the email to send if the user does NOT attach a file.
The below code works, so now I just have to add some file type/size validation.
if(is_uploaded_file($_FILES['fieldAttachment']['tmp_name'])) {
$message->attach(
Swift_Attachment::fromPath($_FILES['fieldAttachment']
['tmp_name'])->setFilename($_FILES['fieldAttachment']['name'])
);
}

Check form upload. Maybe you have error in this.
<form enctype="multipart/form-data" action="" method="POST">

Related

Can I use action='POST' to PHP from contact form in React JS?

I have tried to post form data from a React JS rendered form, calling aq php file with the action part of the form... I have uploaded the form and the attached PHP file to my website, but when the form is filled out out submitted, no email is sent, and the url changes back to the homepage with the name of the PHP file tagged on (index.js/contact.php).
Is my code below bad?
Or is this just not possible to do using a combination of React JS and PHP?
contactMe.js
import React, { Component } from 'react';
class ContactMe extends Component {
render() {
return (
<div className='contact-body'>
<div className='contact-right'>
<div className='contact-side'>
<h2 style={{ fontSize: '25px', fontFamily: 'Anton', paddingTop: '10px'}}>
Contact Me
</h2>
<hr />
<div className='contact-list'>
<div className='form'>
<form action="contact.php" method='post'>
<input type="text" name="name" placeholder="Your name..." />
<input type="email" name="email" placeholder="Your email..." />
<textarea name="message" placeholder="Message me..."></textarea>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</div>
</div>
)
}
}
export default ContactMe;
contact.php
<?php
if (isset($_POST['submit'])) {
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$userMessage = $_POST['message'];
$emailSubject = 'New Message From My Website Visitor';
$emailBody = 'Visitor Name: ' . $userName . '\n\n' .
'Visitor Email: ' . $userEmail . '\n\n' .
'Message: ' . $userMessage . '\n\n';
$emailTo = 'james_ross#outlook.fr';
$headers = 'From: ' . $emailFrom . '\n\n';
mail($emailTo, $emailSubject, $emailBody, $headers);
header('Location: index.js/mailsent');
}
If I need to use another technique/language in place of the PHP, please advise me of s decent source to read up on it, Thank you,
action="contact.php" is a relative link and it looks like your frontend runs as xxx/index.js/, you have to set it to an absolute url like action="/contact.php" or action="xxx/contact.php" where xxx is the folder with your php script.
If you develop with react than you don't want to switch between server side and client side pages, just send your form with ajax and display the output in your react app.
Your PHP code has a security issue:
$emailFrom is not defined, if you have some old PHP configuration with register_globals than it is possible to insert own header content and f.e. send spam emails to any email addresses.

How to create php form handler and intergrate with existing html web form? [duplicate]

This question already has answers here:
Send email with PHP from html form on submit with the same script
(8 answers)
Closed 7 years ago.
So here is the problem I am facing. I have created a pretty simple web form:
<form method="post" action="#">
<div class="field"> <label for="name">Name</label>
<input name="name" id="name" type="text"> </div>
<div class="field"> <label for="email">Email</label> <input
name="email" id="email" type="email"> </div>
<div class="field"> <label for="message">Message</label> <textarea
name="message" id="message" rows="4"></textarea> </div>
<ul class="actions">
<li><input value="Send Message" type="submit"></li>
</ul>
</form>
I need to know how I can use this form to send data inputed by the user to my email address admin#nue-tech.uk I am aware this can be done in PHP but am unsure how to approach this as I am unfamilliar with PHP. If someone could please point me in the right direction as to how this can be done, and also where I should place the PHP file relative to this, that'd be awesome!
You could copy the code below and paste it in your HTML file below your html. In your html you set the action attribute empty. Don't forget to change your file extension to .php
<?php
if(isset($_POST['submit'])){
$to = "admin#nue-tech.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Blablabla"; //Write whatever you want here
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page if the mail was send succesfully
} else {
header('location: contact.html'); //if it was not send succesfully it redirects to the contact page again
exit(0);
}
?>
In
header('location: contact.html');
you could also use
echo "Something went wrong. Try again later"
or something simular.
I would highly recommend you search and learn on W3Cschools or at php.net.

No input file specified when sending email php

UPDATE: non of any php files is running. Is there a way to solve the problem ?
I have html a page that takes the email details:
<form method="POST" sourceindex="44" action="semail.php" name="form1" onSubmit="return validate_form ( );">
<p>
<label>Your name:<br>
<input sourceindex="47" name="mail_name" class="text_field" type="text">
</label>
</p>
<p>
<label>Your e-mail:<br>
<input sourceindex="51" name="mail_email"
class="text_field" type="text">
</label>
</p>
<p>
<label>Message:<br>
<textarea sourceindex="55" name="mail_msg" cols="45"
rows="5" class="text_area"></textarea>
</label>
</p>
<input name="B1" type="submit" class="form_button" value="" />
</form>
and here is the php code (this is all in semail.php):
<?php
$mail_to ="someEmail#hotmail.com";
$mail_subject = "Message From a Customer";
$mail_body ="Name of the Coustomer: ".$_POST['mail_name']."\n";
$mail_body .= "E-Mail Of the Coustomer: ".$_POST['mail_email']."\n";
$mail_body .= "The Message: ".$_POST['mail_msg']."\n";
if(mail($mail_to, $mail_subject, $mail_body))
echo "Thanks for your Message";
else
echo "Failed to send the e-mail"
?>
Whenever I click send the semail.php page shows me this error: No input file specified
Can someone help me please in figuring out where is the problem ?
To first check and verify that PHP is running and available with the present Web hosting company, create a file called check_server.php and insert the following code:
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
If successful, you will see your server information and configurations. It will also show the path to mail.
Regarding your form code:
My answer may or may not solve a probable Hosting company issue, however there were a few errors in your code which would result as unsuccessful.
Please read the following:
In using the following form, had success in sending and receiving the message.
<form method="POST" sourceindex="44" action="semail.php" name="form1">
<p>
<label>Your name:<br>
<input sourceindex="47" name="mail_name" class="text_field" type="text">
</label>
</p>
<p>
<label>Your e-mail:<br>
<input sourceindex="51" name="mail_email"
class="text_field" type="text">
</label>
</p>
<p>
<label>Message:<br>
<textarea sourceindex="55" name="mail_msg" cols="45"
rows="5" class="text_area"></textarea>
</label>
</p>
<input name="B1" type="submit" class="form_button" value="Submit" />
</form>
N.B.: I do need to point out that your PHP mail handler did not contain a few required elements.
For example the From: element/variable which was not present in your handler.
$mail_email= $_POST['mail_email'];
Also headers were not present. Mail requires 4 variables to send/receive Email.
For example you have if(mail($mail_to, $mail_subject, $mail_body))
Which should read as if(mail($mail_to, $mail_subject, $mail_body, $headers))
Omitting the headers would result in a message marked as SPAM because of the missing Email from and would show as unknown sender
In conjunction with the form above, am including a working copy of the PHP mail handler:
<?php
$mail_to ="someEmail#hotmail.com";
$mail_email= $_POST['mail_email'];
$mail_subject = "Message From a Customer";
$mail_body ="Name of the Customer: ".$_POST['mail_name']."\n";
$mail_body .= "E-Mail Of the Coustomer: ".$_POST['mail_email']."\n";
$mail_body .= "The Message: ".$_POST['mail_msg']."\n";
$headers = "From: $mail_email \r\n";
$headers .= "Reply-To: $mail_email \r\n";
if(mail($mail_to, $mail_subject, $mail_body, $headers))
echo "Thanks for your Message";
else
echo "Failed to send the e-mail";
?>
Read up on the mail( ) function on PHP.net: http://php.net/manual/en/function.mail.php

Javascript Form Submit with Email Action?

I need make this form send me a email like a contact form:
Script code:
<script type="text/javascript">
$(document).ready(function(){
$("#contactLink").click(function(){
if ($("#contactForm").is(":hidden")){
$("#contactForm").slideDown("slow");
}
else{
$("#contactForm").slideUp("slow");
}
});
});
function closeForm(){
$("#messageSent").show("slow");
setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);
}
</script>
HTML CODE:
<div class="box">
<div id="contactFormContainer">
<div id="contactForm">
<fieldset>
<label for="Name">Nome: </label>
<input id="name" type="text" />
<label for="Telefone">Telefone Fixo: </label>
<input type="text" id="phone" maxlength="15" onkeypress="Mascara(this);" />
<label for="Message">Assunto:</label>
<textarea id="Message" rows="3" cols="20"></textarea>
<input id="sendMail" type="submit" name="submit" onclick="closeForm()" />
<span id="messageSent">Sua solicitação foi enviada com sucesso, por favor, aguarde...</span>
</fieldset>
</div>
<div id="contactLink"></div>
</div>
When click and close the form i need send me a email with the content of form, how to?
Some idea? thanks!
Firstly i can't see the form tags in your code. According to me you're doing this wrong and i'm sure many of our friends on stack will agree too.
Your question suggests that you basically want to receive an email with the data submitted through the form. Why don't you try the below method.
HTML
<form action="mail.php" method="POST">
<input type="text" name="fname"></input>
<input type="text" name="lname"></input>
<button>SUBMIT</button>
</form>
PHP
<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$to = "someone#example.com";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "sender#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
The above example is the most simplest method of sending an email. You can go advance by adding more header information and graphically formatting the email.
Go through these tutorials if you get confused.
http://www.w3schools.com/php/php_mail.asp
http://www.phpeasystep.com/phptu/8.html
And since you mentioned that you want to perform the task via javascript you can try submitting the form via ajax, refer the below tutorials
http://teachingyou.net/php/simple-php-contact-form-using-ajax/
http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX
Since you've tagged the question php, have a look at php's mail function. http://php.net/manual/en/function.mail.php
$to = 'you#domain.com';
$subject = 'Contact Form';
$message = '...' //concatenate the $_POST (or $_GET) variables to create this message
mail($to, $subject, wordwrap($message, 70, "\r\n");
This function requires that your server has a properly configured to send mail - see the php documentation for requirements: http://www.php.net/manual/en/mail.requirements.php

Contact Form PHP not being sent to email

Hello i have a contact form PHP from a theme i purchased. I've been trying to make a customized form with it but with no luck. Tried changing the variables around to work for the one i made myself but it is not being sent to the email i want the information to go to.
This is what i have in my HTML
<form id="" action="application/application.php" method="post" class="validateform" name="send-contact">
<div id="sendmessage">
Your message has been sent. Thank you!
</div>
Youtube Channel Name <br> <input type="text" name="youtubename" placeholder="* Enter Your YouTube Name">
<div class="validation"></div>
First Name <br> <input type="text" name="firstname" placeholder="* Enter Your First Name">
<div class="validation"></div>
Last Name <br> <input type="text" name="lastname" placeholder="* Enter Your Last Name">
<div class="validation"></div>
Your Paypal Email Address <br> <input type="text" name="paypal" placeholder="* Enter Your Paypal Email">
<div class="validation"></div>
Your YouTube Email Address <br> <input type="text" name="youtubeemail" placeholder="* Enter Your YouTube Email">
<div class="validation"></div>
Skype <br> <input type="text" name="skype" ``placeholder="* Enter Your Skype Name">
<div class="validation"></div>
<button class="btn btn-theme margintop10 pull-left" type="submit">Submit Application</button>
</form>
And for my PHP i have the following;
<?php
include 'config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
$youtubename = stripslashes($_POST['youtubename']);
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$paypal = stripslashes($_POST['paypal']);
$youtubeemail = trim($_POST['youtubeemail']);
$skype = stripslashes($_POST['skype']);
$error = '';
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message
"From: ".$firstname." <".$youtubename.">\r\n"
."Reply-To: ".$youtubeemail."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
}
?>
And in my config.php i have
<?php
// To
define("WEBMASTER_EMAIL", 'Support#XvinityNetwork.com');
?>
I'm not very savvy with HTML and i have a member of my staff that does this however has had an emergency issue to attend to this and i need to get this contact form up and running. I do have the default form that came with the theme and it works perfectly so im guessing i've done something wrong here. Would appreciate the help!
A notable error is you are missing a comma between $message and headers:
$mail = mail(WEBMASTER_EMAIL, $subject, $message, // Here
"From: ".$firstname." <".$youtubename.">\r\n"
."Reply-To: ".$youtubeemail."\r\n"
."X-Mailer: PHP/" . phpversion());
You have few syntax errors. A good redactor was solving your problem in a minute.
First of all the "skype" input - it has two quotes in it. Remove it. Second, in the mail function, you adding undefined $message, with no operator (such as dot) to random text. if $message containing something, place "." after it, and if not, just delete it. And if you meant to add headers, just add comma after the $message.

Categories