As a relative newbie to PHP I've been very impressed with the ease of implementation of the functionalities behind PHPMailer 5.2.0 so far for use on the contact me page of my website which is still under construction. However, I have a small problem and have been searching all of SO, the PhpMailer GitHub, and the rest of the web for a solution for the last few hours and I am completely stumped to be honest as none of the answers seem to quite fit what problem I am experiencing.
What I want on my website's contact form page is to allow users to send me an email using my form including their full name, email address, any attachments they want to send, and their message followed by my email signature.
The current situation is this; when a user fills out the contact form on the contact.html page the results are posted to the email.php form and everything is sent to my email as it is supposed to, followed by sending an auto-response message to the senders email address. However, I am not receiving the attachments in the emails, but i can receive both the initial email and the auto-response if I use another email account I can access, and they are not being uploaded to the uploads folder in my website directory.
I think, from what I have read so far, that I have incorrectly set up 2 things and am also unable to find a php.ini file in my website directory.
The first thing is the structure of my websites public_html folder which contains the following related files and folders; contact.html, email.php, class.phpmailer.php, class.smtp.php, PHPMailerAutoload.php, and a folder called uploads with the privileges set to enable read and write by all user types.
The second thing I assume is wrong is in the following extract of PHP code, taken from the included example in the PHPMailer Git download (the example is not wrong, just my use of it), which I should probably edit to point to the uploads folder instead of "sys_get_temp_dir()":
//Attach multiple files one by one
for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
//The line below may be looking for the wrong directory?
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
$mail->addAttachment($uploadfile, $filename);
} else {
}
}
The current form is coded similarly to below, having been trimmed of all unnecessary styling for easy reading, in the file "contact.html":
<form method="post" action="email.php" enctype="multipart/form-data">
<div>
<input type="text" id="name" name="name" placeholder="Your Name" required>
</div>
<div>
<input type="email" id="email" name="email" placeholder="Your Email Address" required>
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="file" id="userfile" name="userfile[]" multiple="multiple">
</div>
<div>
<textarea rows="20" id="message" name="message" placeholder="Your message Text..." required></textarea>
</div>
<div class="recaptchaContainer">
<div class="g-recaptcha" data-sitekey="*" data-callback="enableBtn">
</div>
<div>
<button id="sendMsgBtn" type="submit">
Send Message
</button>
<p id="response"></p>
</div>
</form>
And the email.php code it posts to, as defined in the form tag:
<?php
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("PHPMailerAutoload.php");
$mail = new PHPMailer;
// $name, $email, and $message are the data that is being
// posted to this page from our html contact form
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$file = 'images/test.jpg' ;
$message = $_REQUEST['message'] ;
/*Attach multiple files one by one
for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
$mail->addAttachment($uploadfile, $filename);
} else {
}
}*/
$emailsig = 'Email signature...' ;
// set mailer to use SMTP
//$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "TLS";
$mail->Host = 'smtp.live.com';
$mail->Port = '587';
// Email address & Password:
$mail->Username = "************#**********.com"; // SMTP username
$mail->Password = "******************"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
$mail->FromName = $name;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("****************#***************.com", "***********");
// set word wrap to 50 characters
$mail->WordWrap = 50;
$mail->Subject = "You have received feedback from " . $name . " via your website!";
$mail->AddAttachment( $file, 'test.jpg' );
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body = nl2br($message) . $emailsig;
$mail->AltBody = $message;
$mail->IsHTML(true);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
} else {
$mail->ClearAllRecipients();
$mail->ClearAttachments();
$mail->From = "*********#*********.com";
$mail->FromName = "******** - Auto-Response";
$mail->AddAddress($email, $name);
$mail->Subject = "Auto-reply message from ********!";
$message = 'Some auto-reply message text here...';
$mail->Body = nl2br($message) . $emailsig;
$mail->send();
echo "Message has been sent successfully!";
}
?>
I have a feeling it is due to a problem with what I have done (or not done) on my web server combined with not pointing to the upload folder in my directory and maybe even not pulling the files across to my php file initially? What I would like to know really is a step-by-step set of instructions for sorting out my code and setting this up on my web server if anyone can help?
I very much appreciate the fact that my question will probably have WAY too much detail and is probably an easy fix for someone who is an expert on this sort of thing, I just wanted you to have all the information at your disposal to make working this one out a bit easier.
Related
After going through my website the user can email me a file and a brief description to go along with it. However once the user of my website clicks the submit button, he goes to a page that says "this webpage is unavailable" and I don't get an email.
I have been using PHP and HTML for this part of my website and I don't know why it isn't working.
PHP
<?php
mail('Example#gmail.com', $_POST['Subject'], $_POST['Content']);
?>
HTML
<form method="post" action="email.php">
<input type="file">
<input type="text">
Content Goes Here
<br>
<br>
<input type="Submit">
</form>
Try to use something like libmail class for sending Emails, in most cases it fixes the problem.
If even after making it work with libmail you will get an issues, try to use SMTP along with libmail.
Cheers.
Sure, here is example of usage:
Download php_libmail class with this link http://webi.ru/base/files/tovar/php_libmail_2_1.zip
Then use this code:
<?php
include "libmail.php"; // including the class
$m= new Mail; // create instance
$m->From( "asd#asd.com" ); // from
$m->To( "who#asad.com" ); // to
$m->Subject( "Subject zzz" ); // subject
$m->Body( "Hey, pal" ); // body
$m->Cc( "copy#asd.com"); // copy of email, if need
$m->Bcc( "bcopy#asd.com"); // hidden copy of email, if need
$m->Priority(3) ; // priority of message, i think from 1 to 5
$m->Attach( "asd.gif","", "image/gif" ) ; // attachment, if need
$m->smtp_on( "smtp.asd.com", "login", "password" ) ; // via SMTP, if need
$m->Send(); // And the magic Send ;)
echo "Message body:<br><pre>", $m->Get(), "</pre>";
?>
For make functionality you need, just create simple HTML form with enctype="multipart/form-data" attribute and add any fields you want, files, inputs, texts, any of those.
And then in you PHP script accept those field values via global $_POST variable and pass accepted values into the libmail instance ;)
For accepted files use global $_FILES variable.
You can simply use PHP Mailer for sending any mail.
It is very helpful and easy way to do this kind of work.
Code would be like-
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("recepient1#example.com", "Recepient Name");
$mail->addAddress("recepient1#example.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("reply#yourdomain.com", "Reply");
//CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
Here is a illustrative example given for this.
Here is code for sending email from localhost after i referred a lot from online.
html form:
<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>
email.php:
<?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 = "mypassword"; // SMTP password
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;//587;
$mail->AddAddress("bradm#inmotiontesting.com", "Brad Markle");
$mail->SetFrom('xxxxxxxx#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";
?>
so when i run my code, i received mail but time is correct in gmail,
If i use hotmail means, the received time is not correct.
May i know know?
Anyone can help me?
Thanks,
Note: I m new to php, but i want to know particular this one and fix the problem. and I referred similar questions in stack, but it didn't help me,
Can anybody help me to fix this?
Thanks,
when i check my email acc, it shows like this,
so in that image, the time showed., 8.48AM, it is not correct according to indian time, it need to show 2.05PM ..
According to my question,
For hotmail time settings,
I modified my hotmail acc, after that it will work,
The steps are,
Log into your Hotmail Account
Click on Options in the upper right corner of the page
Under "Manage Your Account, click on the option "View and edit your personal information"
One the Account Summary page, click the link name "Registered information" under your birthdate
Select your correct country in the Home Location section
Select your correct time zone in the Time Zone section.
Click on the Save tab
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I have a very simple contact form and a very simple php mail script. But when I tried to send it to the specified email address, I did not receive the test mail.
Below are my codes:
HTML Form
<div class="form" >
<form action="mail.php" method="post">
Email: <input type="text" name="email" size="38"><br>
詢問主旨:<input type="text" name="subject" size="36"><br>
<div class="queryTitle">
詢問內容
</div>
<br>
<textarea name = "message" rows="6" cols="37">
</textarea>
<br>
<input name = "submitted" type="submit" value="傳送">
</form>
</div>
Php script:
<?php
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From: ".$_REQUEST['email']."\r\n"; //Site name
// Change this to your email address you want to form sent to
$to = "verymeanguy2#gmail.com";
$subject = $_REQUEST['subject'];
$message = $from." ".$_REQUEST['message'];
mail($to,$subject,$message,$from);
}
}
?>
Could it be that Gmail blocked my mail? If so, how can I devise a script that can send mails to the popular mails?
Thanks in advance!
Jason
PS: I am hosting mine on heliohost's free Stevie server, if that accounts for something.
lolka_bolka: yes, mail() was called and adding an echo in front of mail function prints 1. And it's not in spam. Len_D: it did print anything, so I think mail was called and returned true. Anthony: how do I ensure that?
I just want to report back my testing. It seems that Gmail blocks Yahoo mail address sender for some reason. When the header is a Gmail address or even a bogus made-up address, Gmail can receive no problem. Yahoo mail on the other hand can receive mail with no problem at all. Anyone can shed a light on that?
When mail returns true (or 1), it means it did correctly what it tried to do. But doesn't always mean it sent mail at all.
the mail() funciton uses "sendmail" to actually send the email.
If sendmail is configured to send mail, then that's what will happen (it everything went ok).
However, by defult, sendmail stores mail in the server. It "simulates" a real sendmail.
From my personal experience, I recommend PHPMailer. It's easy to use, it's much easier to check for errors (check if the email was sent, and if not, get infomation about the problem).
Example:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.server.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->From = "email#domain.com";
$mail->FromName = "auto";
$mail->AddAddress("example#example.net", "Name of example user"); //send to....
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = "=?UTF-8?B?".base64_encode(stripslashes($asunto))."=?=";
$mail->Body = stripslashes($mensaje);
if(!$mail->Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
return true;
} else {
echo "Message has been sent";
return false;
}
Hope it helped :)
I'm preparing to create a form page for a website that will require many fields for the user to fill out and that will be sent to a specified email.
So far I've created a dummy php email page that gets your Message, 1 attachment, and Recipient Email address using Google's SMTP.
Here's my code for uploadtest.html:
<body>
<h1>Test Upload</h1>
<form action="email.php" method="get">
Message: <input type="text" name="message">
Email: <input type="text" name="email"><br>
Attach File: <input type="file" name="file" id="file">
<input type="submit">
</form>
</body>
uploadtest.html is what the user will see
Here's the code for email.php:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$recipiant = $_GET["email"];
$message = $_GET["message"];
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->SMTPSecure = 'ssl';
$mail->Username = "xxxxx#gmail.com"; // SMTP account username
$mail->Password = "xxxxxxxx"; // SMTP account password
$mail->AddAttachment($_FILES['tmp_name']); //****HERE'S MY MAIN PROBLEM!!!
$mail->SetFrom('cinicraftmatt#gmail.com', 'CiniCraft.com'); // FROM
$mail->AddReplyTo('cinicraftmatt#gmail.com', 'Dom'); // Reply TO
$mail->AddAddress($recipiant, 'Dominik Andrzejczuk'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = $message;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
So from what I can tell here, PHPMailer's AddAttachment() method takes as a parameter the URL of the file DIRECTORY you want attached. And this is where my main problem is.
What would the name of the variable be that would get the location of my file (dir/upload.jpg) that I've uploaded so I could use it as a parameter in the AddAttachment() method?
No, it doesn't take URLs, or directories. It takes a direct path to a FILE.
e.g.
$mailer->AddAttachment(
'/path/to/file/on/your/server.txt',
'name_of_file_in_email',
'base64',
'mime/type'
);
The path is self-explanatory. The name_of_file_in_email allows you to "rename" the file, so that you might loaded a file named "foo.exe" on your server, it can appear as "bar.jpg" in the email the client receives.
Your problem is that you're trying to attach an uploaded file, but using the wrong source. It should be
<input type="file" name="thefile" />
^^^^^^^
$_FILES['thefile']['tmp_name']
^^^^^^^
Note the field name relationship to $_FILES.
This will be send like this through PHPMailer Class
$mail->AddAttachment($_FILES['tmp_name'],$_FILES['name']); //****HERE'S MY MAIN PROBLEM!!! So you would have to give file name also.
please try with this
include "phpMailerClass.php";
$email = new PHPMailer();
$email->From = $to;
$email->FromName = $_POST['name'];
$email->Subject = $subject;
$email->Body = $EmailText;
$email->AddAddress( 'shafiq2626#hotmail.com' );
$email->IsHTML(true);
$file_to_attach = $_FILES['file']['tmp_name'];
$filename=$_FILES['file']['name'];
$email->AddAttachment( $file_to_attach , $filename );
$email->Send();
This is code is working fine.
Your <form> tag should specify the enctype attribute like so :
<form ... enctype="multipart/form-data">
...
</form>
Use the following code:
$objectMailers->AddAttachment("file.extension");
tested on PHP 7 and using PHPMailer 5.3
How to get user mails in my free gmail inbox through contact us form on my website. I do not use email with my website name . i use free gmail. I tried many script but the all need email account on domain.
Does this question have something to do with This One?
Well, so you have a form on your site, your users fill it up and you need an email with that data, right?
Simple example:
<form action="sendMail.php" method="post">
Name: <input type="text" name="name" id="name" /><br />
Email: <input type="text" name="email" id="email" /><br />
Text: <textarea name="text"></textarea><br />
<input type="submit" value="Send" />
</form>
then, the php page wich send the mail:
//php sendThis.php page
<?php
require("class.phpmailer.php");
$name = $_POST['name'];
$email = $_POST['email'];
$text = $name . ', ' . $email . ' has filled the form with the text:<br />' . $_POST['text'];
$from = 'your.email#gmail.com';
$to = 'your.email#gmail.com';
$gmailPass = 'your gmail password';
$mail = new PHPMailer();
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the server
$mail->SMTPSecure = "ssl";
// sets GMAIL as the SMTP server
$mail->Host = 'smtp.gmail.com';
// set the SMTP port
$mail->Port = '465';
// GMAIL username
$mail->Username = $from;
// GMAIL password
$mail->Password = $gmailPass;
$mail->From = $from;
$mail->FromName = $from;
$mail->AddReplyTo($from, $from);
$mail->Subject = 'This is a test!';
$mail->Body = $text;
$mail->MsgHTML($text);
$mail->IsHTML(true);
$mail->AddAddress($to, $to);
if(!$mail->Send()){
echo $mail->ErrorInfo;
}else{
echo 'sent!';
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
EDIT: just tested and works fine. Make sure that the 3 files (class.phpmailer.php, class.pop3.php and class.smtp.php) are in the correct include path
Basically, it involves the PHP mail() function:
<?php
mail(yourGmailAddress, object, message);
?>
As you have already observed, this solution works only if the webserver operates a mail server. This mail server may forbid unknown users. So you need to have an email account on that web/mail server (I believe this is the case). The second step then is to forward mail from your website address to you gmail account. I am 90% certain that it is possible from your gmail configuration. It may also be possible from your website mail configuration. But don't configure both!
Try enabling openssl.
Uncomment the line:
extension=php_openssl.dll
in your php.ini file.
You can also put your email address into "action" attribute of the form element. But that's very unreliable. Like this:
<form method='post' action='mailto:your#email.com?Subject=Hello'>
...
</form>
Users must have email client installed and configured for this to work properly. There are some other drawbacks. You have to do some research to find whether this method is for you or not.
http://www.google.com/search?client=opera&rls=en&q=form+action+mailto&sourceid=opera&ie=utf-8&oe=utf-8