I'm currently experiencing a very strange issue trying to set up a simple mailer from a form. I've literally used the code on many sites seems to be working fine. But on this particular one I seem to be getting the Error "You must provide at least one recipient email address."
This is the code that i've been using
<?php
require_once('class.phpmailer.php');
$name = $_POST['name'];
$user_email= $_POST['email'];
$query = $_POST['message'];
$message = file_get_contents('email.html');
$message = str_replace('{{name}}', $name, $message);
$message = str_replace('{{email}}', $user_email, $message);
$message = str_replace('{{message}}', $query, $message);
$email = new PHPMailer();
$email->CharSet = 'UTF-8';
$email->IsHTML(true);
$email->From = $user_email;
$email->FromName = $name;
$email->Subject = 'Talking Together Speech Therapy Enquiry From- '. $name;
$email->Body = $message;
$email->AddAddress( 'barry.tickle12#gmail.com​' );
if($email->send()){
// Trigger when email sends
}else{
//Trigger when email doesn't send
echo $email->ErrorInfo;
}
?>
Can a specific server setup be causing this? Other sites on the same server seem to be working fine with it apart from this particular one.
* Edit *
Var dumped all POST Requests to the file and nothing is being returned empty.
Maybe add a check to see that you have all the required data ?
$email= $_POST['email'] ?? '';
if($email){
/** Your code goes inside this if */
} else {
/** Show error */
}
Try using new version of AddAddress():
$email->addAddress('recipient#domain.com', 'Name');
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So, here we go with silly question number too many to count!
I've made a very simple PHP contact form using tutorials from the internet (I still need to add security measures to it but I wanted to get it working first) When I click on the send button on my website I do get the message sent script however no email arrives in my in box.
Any ideas what I'm doing wrong? The website is currently hosted locally via XAMPP.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$from = 'From: me#mywebsite.co.uk';
$to = 'me#mywebsite.co.uk';
$subject = 'Enquiry';
$body = "From: $name\n Company: $company\n Email: $email\n Telephone: $tel\n Message: $message\n";
if ($_POST['send']) {
if(mail($to, $subject, $body, $from)) {
echo '<p> Your message has been sent!</p>';
} else {
echo '<p>Message could not be sent.<br>Please check that you have completed the name, email and message fields and try again</p>';
}
}
Alright:
Step 1: check your error logs for any problems with the mail not being sent. Normally when installing an Apache inside windows the most people skip to set the from server and credentials.
I used WAMP alot and that one works normally only with an external account.
Step 2: If anything fails.
Download a mailer library and use Gmail to send the emails. Here is an tutorial on that : http://phpmailer.worxware.com/?pg=examplebgmail
Works great. Sure there is a lot of files in phpmailer but it works and easy to upgrade when new software versions are released.
As when I've previously set up contact forms I've been doing so using Code Igniter I didn't realise that I couldn't use mail() without installing a mail server.
Thanks to Parris Varney and RiggsFolly for pointing this out and thanks again to Riggs for letting me know that Code Igniter uses the PHPMailer library.
By using PHPmailer I was able to correct the code and get the form working perfectly in very short order.
For anyone interested the new code used with the latest version of PHPmailer is:
$name = $_REQUEST['name'];
$co = $_REQUEST['company'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];
require("PHPMailerAutoLoad.php");
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "mail.mydomain.co.uk";
$mail->SMTPAuth = true;
$mail->Username = "me#mydomain.co.uk";
$mail->Password = "password";
$mail->SMTPAutoTLS = false;
$mail->From = $email;
$mail->addAddress("me#mydomain.co.uk", "Me");
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Enquiry";
$mail->Body = "From: $name<br>Company: $co<br>Email: $email<br>Telephone: $tel<br>Message: $message";
$mail->AltBody = "From: $name Company: $co Email: $email Telephone: $tel Message: $message";
if(!$mail->Send())
{
echo "Message could not be sent";
}
echo "Message has been sent";
?>
I am a newbie to PHP, and I am currently using phpmailer to let my clients send emails to me, but a bot is apparently taking advantage of it and sends 3 emails at exactly the same time everyday. So I've been trying to add validation to the form using PHP as javascript can be stopped to avoid validation.. Could you guys please help me add validation to this? I just need to validate name and email. Thank you so much in advance.
PS. I removed some parts of the code for privacy.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$proptypes = $_POST['proptypes'];
$units = $_POST['units'];
$purchaseOrRefi = $_POST['purchase'];
$loans = $_POST['loans'];
$income = $_POST['income'];
$apt = $_POST['apartment'];
$message = $_POST['message'];
$body = "<b>[From]</b><br>$name<br><br> <b>[E-Mail]</b><br>$email<br><br> <b>[Phone #]</b><br>$phone<br><br> <b>[Address]</b><br>$address<br><br> <b>[Type of Property]</b><br>$proptypes<br><br> <b>[# of Units]</b><br>$units<br><br> <b>[Purchase or Refi?]</b><br>$purchaseOrRefi<br><br> <b>[Amnt of Loans Requested]</b><br>$loans<br><br> <b>[Total Income]</b><br>$income<br><br> <b>[Total Apt Expense]</b><br>$apt<br><br> <b>[Message]</b><br>$message<br><br>";
$mail->Subject = 'Someone wants to hear more about your mortgage programs!';
$mail->Body = $body;
$mail->From = '';
$mail->FromName = '';
// Add a recipient
$mail->addAddress('');
$mail->addAddress('');
$mail->addAddress('');
$mail->addAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name'], 'base64', $_FILES['attachment']['type']); // Add attachments
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent. Please try again.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Your message has been sent! We will get back to you soon!';
}
?>
So I'm trying to use PHPMailer to handle the email form on my website.
I wrote the code here based on a tutorial I found.
<?php
error_reporting(E_ALL);
require_once("class.phpmailer.php");
include("class.smtp.php");
$email = new PHPMailer();
//
// Set server details for send
//
$email->IsSMTP();
$email->Host = "mail.loganyoung.za.net";
$email->Port = 25;
$email-SMTPAuth = true;
$email->Username = "<my email>";
$email->Password = "<my password>";
//
// Send mail from the contact form
//
$to = "<my email>";
$from = $_POST["from"];
$name = $_POST["name"];
$subject = "From web: ".$_POST["subject"];
$message = $_POST["message"];
$body = "<p>Hi Logan,</p>";
$body .= "<p>You have received a new query on your website.<br />Please see below:</p>";
$body .= "<p>";
$body .= str_replace("\r\n", "<br />", $message);
$body .= "</p>";
$email->SetFrom($from, $name);
$email->AddReplyTo($from, $name);
$email->AddAddress($to, "LoganYoung.za.net");
$email->Subject = $subject;
$email->Body = $body;
$email->IsHTML = true;
session_start();
if(!$email->Send()) {
$_SESSION["mailresult"] = "success";
echo "success";
} else {
echo "<p>Failed:</p><p>".$email->ErrorInfo."</p>";
$_SESSION["mailresult"] = "failed";
$_SESSION["mailerror"] = $email->ErrorInfo;
}
?>
I figure possible reasons for it not sending could be...
Incorrect SMTP details (possibly send without SMTP auth to resolve?)
Handler isn't getting POST data (the ajax that sends it seems to work fine though)
Some problem with this code that I'm not able to identify...
As a means of eliminating possibilities, can anyone spot anything wrong with the code here? If so, what's wrong, and how do I fix it?
Thanks in advance!
$email-SMTPAuth = true;
Isn't that supposed to be:
$email->SMTPAuth = true;
im a php beginner, and I am building a website, and website is supposed to let people send me email. the thing is i never knew anything about sending emails through php. I looked it up online and tried using the codes i found. The thing is my program says it sent the email but i never happen to get the email. I thought maybe it is because I am using apache server to test my php, and maybe it is gona work when I upload it to a real server??(yes this was a question)
Just in case, this is my code, and it is the all php code in my website, also form works fine, there is nothing wrong with it.
<?php
if (isset($_POST['name'])) {
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "From: " . $_POST['name'] . ", " . $_POST['email'] . "\n Message: " . $_POST['message'];
try {
mail("serdarufuk95#gmail.com", $subject, $message, " ");
unset($_POST['name']);
header("Location: success.php");
}
catch (PDOException $e)
{
include 'index.html';
exit();
}
exit();
}
include 'contact2.php';
?>
is there a problem with the code? or do i have to call something from a library or anything or am i missing a code! HELP ME MAKE THIS WORK! when i execute it, it takes me to success.php, so i assumed nothing is wrong with my code, but you guys know better!
It might not work after uploading to a real server if the server's mail configuration has not been set yet. So I suggest you to use a better and simpler version in order to send mails through PHP: PHPMailer
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->AddAddress("mail#domain.com","Display name");
$mail->Subject= "Mail subject";
$mail->Body= "Mail content";
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "formmail#domain.com";
$mail->Password = "123456";
$mail->IsHTML(true); //true if you want to send html content. false for plain text message
$mail->From = $_POST['Email'];
$mail->FromName = $_POST['Name'];
$mail->Send();
You may download the class clicking here
I'm trying to send an order confirmation and also notify the seller about a user purchase. However, PHPMailer only sends the first email. Here's quick and dirty:
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Subject';
$email->Body = $bodytext;
$email->AddAddress($_REQUEST['sahkoposti']);
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
<?php
//send message to seller
$bodytext = 'Mail.';
$email = new PHPMailer();
$email->From = 'mail#mail.com';
$email->FromName = 'Sender';
$email->Subject = 'Tilaus vastaanotettu';
$email->Body = $bodytext;
$email->AddAddress("mail#mail.com");
$email->AddAttachment($path, 'kuitti'.$ordernumber.'.pdf');
return $email->Send();
?>
Is it even possible to send multiple emails from one script?
It is possible, however you're using return in the first statement, which will stop execution of the function. Remove the first return (just use $email->Send();) and it should work.
The second email does not get executed because you are returning right after sending the first email, you should change:
return $email->Send();
for this:
$email->Send();