Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAris Constantinou
2,687 PointsFor some reason PHP Mailer won't allow me to send emails
Hi,
Once i used PHP Mailer on my website it worked for around 10 emails and then it stopped and now gives me this error "Message could not be sent.Mailer Error: Could not instantiate mail function."
Also do i need ->IsSMTP for personal emails?
This is my suggestion.php
ini_set('display_errors', 1);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));
if ($name == "" || $email == "" || $details == "") {
echo "Please fill in the required fields: Name, Email and Details";
exit;
}
if ($_POST["address"] != "") {
echo "Bad form input";
exit;
}
require("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer;
if (!$mail->ValidateAddress($email)) {
echo "Invalid Email Address";
exit;
}
//$mail->IsSMTP();
$email_body = "";
$email_body .= "Name: " . $name . "<br>";
$email_body .= "Email: " . $email . "<br>";
$email_body .= "Customer Question: " . $details . "<br>";
$mail->setFrom($email, $name);
$mail->addAddress('arisconstantinou@cytanet.com.cy', 'Author'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Customer Question ' . $name;
$mail->Body = $email_body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
header("location:suggest.php?status=thanks");
}
?>```
2 Answers
Craig Watson
27,930 PointsHi
Sorry for the long delay in getting back to you I was away over the Easter break.
Have you managed to get to the bottom of this one yet ?
Craig
Aris Constantinou
2,687 Pointsofcourse,
- by using php mail function, one.com only accepts 4 parametres for example
mail($to, $subject, $message, $headers); <<< Only 4 parametres allowed
2.Replace $to with $from(your email address/better create one with one.com email service for unlimited traffic)
3.In html form dont forget to give name to the button for example {<input type="submit" name="send" value="Send" />}
4 .Also if you use your own personal email for example a hotmail or gmail you are only limited to 20 emails per day,unless if you create an email address within their email service you get unlimited email traffic.
Or just copy paste my code:
<?php
if (isset($_POST['send'])) {
$from = 'emailaddresshere'; // Use your own email address
$subject = 'Feedback from my site';
$message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email: ' . $_POST['email'] . "\r\n\r\n";
$message .= 'Message: ' . $_POST['message'];
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
header('Location: thanks.php');
if ($email) {
$headers .= "\r\nReply-To: $email";
}
$headers = "From: ".$_POST['email']."\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
$success = mail($from, $subject, $message, $headers);
}
?>
That's what i learned by talking to them, their technical team helped me with my php code while chatting and they are great !!
Aris Constantinou
2,687 PointsAris Constantinou
2,687 Pointsyeah, actually i switched to php mail function and with a little help from my hosting provider (one.com) i finally got it to work for infinite messages
Craig Watson
27,930 PointsCraig Watson
27,930 PointsGlad to here you got it all sorted!
And again sorry for the huge delay. Did the hosting provider tell you what the issue was? Just in case anyone else comes to this they may get a helpful hint in the right direction that all :)
Craig