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 trial

PHP

PHP mailer setfrom not working in cpanel hosting.

Hi there

I hope someone can help me, i have been wasting hours trying to get this working. So lets try Treehouse :P

I am doing a website for a client and it is almost complete. I have a contact form that requires name. email and message and the details sent to the specified email address.

So I tried phpmailer and it works well to some extent. First address I tried to use was a gmail address and apparently you can't display the FROM (sender) address. I tried in cpanel. created the email account, but not working.

I can receive mails, if the email account created in cpanel is in the setFrom function . When I change this to $email I dont get an error message but never receive the mail.

I provided the php code below as well as the contact form

I would really appreciate help, might be a mistake I make but I spent hours copying code, watching videos, reading up but got this far, now I am stuck.

<?php $msg = ""; use PHPMailer\PHPMailer\PHPMailer; include_once "PHPMailer/PHPMailer.php"; include_once "PHPMailer/Exception.php"; include_once "PHPMailer/SMTP.php";

if (isset($_POST['submit'])) {
    $subject = $_POST['subject'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
        $file = "attachment/" . basename($_FILES['attachment']['name']);
        move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
    } else
        $file = "";
    require 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();

    //if we want to send via SMTP
    $mail->Host = "hostserver address"; //cpanel gave details
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = "example@mail.co.za";// I fill my address here ( I created this one in cpanel)
    $mail->Password = "mypass";
    $mail->SMTPSecure = "ssl"; //TLS
    $mail->Port = 465; //587
    $mail->setFrom('$email'); //my problem when I use example@mail.co.za it works fine, I receive mail but not when I change this to $email
    // $mail->Sender = $email;
    // $mail->addCustomHeader('Sender', 'ACME <noreply@example.com>');
    $mail->addAddress('client email address, gmail);
    $mail->AddReplyTo($email, '$subject');

    $mail->Subject = $subject;
    $mail->isHTML(true);
    $mail->Body = $message;

// $mail->addAttachment($file);

    if ($mail->send())
        $msg = "Your email has been sent, thank you!";
    else
        $msg = "Please try again!";

    unlink($file);
}

CONTACT FORM

<!-- CONTACT FORM-->

<div class="container-fluid contactBg" style="margin-top: 100px" id="ContactForm">
    <div class="row justify-content-center">
        <div class="container contacts">
        <div class="col-md-6 col-md-12" align="center">
            <img src="img/contact.png" width="250px;" height="auto" class="img-fluid animated bounce contactlogo"><br><br>
                <h2>Contact us</h2>

            <?php if ($msg != "") echo "$msg<br><br>"; ?>

            <form method="post" action="index.php" enctype="multipart/form-data">
                <input class="form-control" name="subject" placeholder="Full Name..."><br>
                <input class="form-control" name="email" type="email" placeholder="Email..."><br>
                <textarea placeholder="Message..." class="form-control texta" name="message"></textarea><br>
                <!--<input class="form-control" type="file" name="attachment"><br>-->
                <input class="btn btn-primary" name="submit" type="submit" value="Send Email">
            </form>
        </div>
        </div>
        <div class="col-md-2 col-md-12 inquire contacts text-center">
         <p>For inquiries or support, please send us a message and we'll get back to you shortly.</p>
        </div>
    </div>
</div>



<!--    END CONTACT FORM-->

1 Answer

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

In PHP single-quotes are more strict compared to double-quotes:

$firstName = "Henrik";
echo 'Hello $firstName'; // prints 'Hello $firstName'
echo "Hello $firstName"; // prints 'Hello Henrik'

I think this is what's causing you this problem, because in your $mail->setFrom('$email'); you use single-quotes and that makes PHPMailer think that you mean the email is from the address $email and not the actually value of the variable $email.

Try use double-quotes instead, and see if that will fix your problem

// try this
$mail->setFrom("$email");
// instead of
$mail->setFrom('$email');

Hi I checked it, it says mail successful but did not receive a mail. The problem is that setFrom, I don't know if it is my service provider, or google that is preventing me from just displaying a senders mail address.

if I use the email I created eg webmaster@example.com as the setFrom the mail goes through and I receive it. When I change the form to $email even in ' ' or " " it sends but dont receive anything.

Back to the drawing board lol :P