PHP Mail


I am trying this PHP code to send email. In an earlier attempt I was able to send mail properly but that was setting up PHP.ini setting.

In this attempt, I am trying to setup my smtp settings in the email program itself.

Attached is the new file.


Sanjay GandhiFounder, KenhalAsked:
Who is Participating?
Sanjay GandhiConnect With a Mentor Founder, KenhalAuthor Commented:
Finally this code works fine, sending mail from website using PHP.

 $from = $_POST["FlFirstName"]." <".$_POST["FlEmail"].">";
 $to = "Info <>";
 $subject = "Visitor Mail From ".$_POST["FlFirstName"];
 $body = $_POST["FlDetails"]."\r\n"."\r\n";
 $body .= $_POST["FlFirstName"]." ".$_POST["FlLastName"].","."\r\n".$_POST["FlEmail"]."\r\n";
 $body.= substr($url,7,14);
 $host = "";
 $username = "";
 $password = "1234yourpassword";
 $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
 $smtp = @Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'username' => $username,
     'password' => $password));
 $mail = @$smtp->send($to, $headers, $body);
 if (@PEAR::isError($mail))
	   $message = "Your mail could NOT be submitted successfully, please send direct mail";
	   $message = "Your mail has been submitted successfully";
 echo "<script type='text/javascript'>alert('$message');</script>";
 echo "<script type='text/javascript'>history.back();</script>";

Open in new window

Yes, you need to change Host, Username, Password in this code. And also use this PHP file as reference in your html code in 'action' in 'form' tag.

And the variables flEmail, flFirstName... are fields in html code, where in the form we accept user values in these fields. And then we transfer these fields to PHP file to process and send mail. These variables are used in Contact Us page in html file.

Thanks for support.

- San
Are you getting any errors??  What is the issue?
Sanjay GandhiFounder, KenhalAuthor Commented:
Oh yes, I forgot to send the error I am facing:

Failed to set sender: [SMTP: Invalid response code received from server (code: 530, response: The SMTP server requires a secure connection or the client was not authenticated.(#5.7.1))]

Here is the problem.

- San.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

have you enabled ssl?

You have 'auth' => false, yet the server is requiring authentication.
Sanjay GandhiFounder, KenhalAuthor Commented:
If I make it true, I get the following error: (I've tried various combinations, getting different errors, so I'll keep informing you about the errors). In fact, the pwd is N0Reply (case sensitive), so it may become easy for you. On the other hand, I am running Xampp, and the PHP.ini and sendmail.ini is also existing there as it is. Here is the error with Auth=True.

authentication failure [SMTP: Invalid response code received from server (code: 535, response: authorization failed (#5.7.0))]

I've not enabled SSL. It is none in my Outlook settings where this mail is working fine.

- San.
I see that you did enable the auth=true.  I'm glad that it worked out for you.
Sanjay GandhiFounder, KenhalAuthor Commented:
Probably after lot of struggle, I could get the exact way I wanted this answer to work for my requirement. So after finding solution, I thought it was my duty to share the answer.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.