/    Sign up×
Community /Pin to ProfileBookmark

how to send email by php?

Hi guys, I am new in this world.
i want to send email by php, Because I use the school environment, I cannot configure the server.
I try to use mail() function, but it is not work, I checked the information that needs to configure php.ini, but could not find this file.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 25.2018 — Can we start by viewing the code you are using to "send email"?

There is more to using php than just sending email. Can you do other things with php scripts?
Copy linkTweet thisAlerts:
@newboy0714authorApr 25.2018 —  $to = $_POST[Email];
$subject = "Welcome join UTAS Cafe ! ";

$message = "
<html>
<head>
<title>Welcome join UTAS Cafe ! </title>
</head>
<body>
<p>This is a welcome email !</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";

$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";

mail($to,$subject,$message,$headers);
Copy linkTweet thisAlerts:
@newboy0714authorApr 25.2018 — this is my php scripts, thank you.
Copy linkTweet thisAlerts:
@ginerjmApr 25.2018 — Array indices should be in quotes. Email is not defined.

The email requires a from address. Usually I place mine in the headers.

Is there actually a to value in the POST array yet?
Copy linkTweet thisAlerts:
@NogDogApr 25.2018 — Unfortunately, the built-in mail() function gives developers very little to work with as far as debugging goes, as it only returns either true or false with no warnings/errors, and even true does not guarantee that the server's mail program actually sent it.

As mentioned above, you may have to add a "From:" header to your $headers string. If so, it will probably have to be an email address that is configured for that server's mail set-up, so you may need to check with the system administrator to find out which address to use. Note that you can also set a "Reply-To:" header if you want the recipients to reply to a different address.

Personally, I almost always end up using [PHPMailer](https://github.com/PHPMailer/PHPMailer) when I have to do emails, as it provides many more options and better debugging. Your mileage may vary.
Copy linkTweet thisAlerts:
@rootApr 26.2018 — Also, most web hosts require you to have the email address set up in the email server that you specify in the "from" / "Reply-To" parts of the email header.

You need to consult your web host for any limits that may be imposed by them.
Copy linkTweet thisAlerts:
@shahroznawazMay 22.2018 — You can try this code.

``<i>
</i>&lt;?php

if(isset($_POST['email'])) {
$email_to = "***.******@*********.***";
$email_subject = "Summarized propose of the email";
//Errors to show if there is a problem in form fields.

function died($error) {
    echo "We are sorry that we can procceed your request due to error(s)";
    echo "Below is the error(s) list &lt;br /&gt;&lt;br /&gt;";
    echo $error."&lt;br /&gt;&lt;br /&gt;";
    echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
    die();

}

// validation expected data exists

if(!isset($_POST['first_name']) ||

       !isset($_POST['last_name']) ||

       !isset($_POST['email']) ||

       !isset($_POST['telephone']) ||

       !isset($_POST['comments'])) {

    died('We are sorry to proceed your request due to error within form entries');   

}

$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

   $telephone = $_POST['telephone']; // not required

$comments = $_POST['comments']; // required

$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';

 if(!preg_match($email_exp,$email_from)) {

$error_message .= 'You entered an invalid email&lt;br /&gt;';

 }

$string_exp = "/^[A-Za-z .'-]+$/";

 if(!preg_match($string_exp,$first_name)) {

$error_message .= 'Invalid first name&lt;br /&gt;';

 }

 if(!preg_match($string_exp,$last_name)) {

$error_message .= 'Invalid Last name&lt;br /&gt;';

 }

 if(strlen($comments) &lt; 2) {

$error_message .= 'Invalid comments&lt;br /&gt;';

 }

 if(strlen($error_message) &gt; 0) {

   died($error_message);

 }

$email_message = "Form details below.nn";

function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}

$email_message .= "First Name: ".clean_string($first_name)."n";

$email_message .= "Last Name: ".clean_string($last_name)."n";

$email_message .= "Email: ".clean_string($email_from)."n";

$email_message .= "Telephone: ".clean_string($telephone)."n";

$email_message .= "Comments: ".clean_string($comments)."n";

// create email headers

$headers = 'From: '.$email_from."rn".

'Reply-To: '.$email_from."rn" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?&gt;

&lt;!-- include your own success html here --&gt;

Thank you for contacting us. We will be in touch with you very soon.

&lt;?php

}

?&gt;

&lt;form name="contactform" method="post" action="email_form.php"&gt;

&lt;table width="450px"&gt;

&lt;tr&gt;

&lt;td valign="top"&gt;

 &lt;label for="first_name"&gt;First Name *&lt;/label&gt;

&lt;/td&gt;

&lt;td valign="top"&gt;

 &lt;input  type="text" name="first_name" maxlength="50" size="30"&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td valign="top""&gt;

 &lt;label for="last_name"&gt;Last Name *&lt;/label&gt;

&lt;/td&gt;

&lt;td valign="top"&gt;

 &lt;input  type="text" name="last_name" maxlength="50" size="30"&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td valign="top"&gt;

 &lt;label for="email"&gt;Email Address *&lt;/label&gt;

&lt;/td&gt;

&lt;td valign="top"&gt;

 &lt;input  type="text" name="email" maxlength="80" size="30"&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td valign="top"&gt;

 &lt;label for="telephone"&gt;Telephone Number&lt;/label&gt;

&lt;/td&gt;

&lt;td valign="top"&gt;

 &lt;input  type="text" name="telephone" maxlength="30" size="30"&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td valign="top"&gt;

 &lt;label for="comments"&gt;Comments *&lt;/label&gt;

&lt;/td&gt;

&lt;td valign="top"&gt;

 &lt;textarea  name="comments" maxlength="1000" cols="25" rows="6"&gt;&lt;/textarea&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td colspan="2" style="text-align:center"&gt;

 &lt;input type="submit" value="Submit"&gt;  &lt;/a&gt;

&lt;/td&gt;

&lt;/tr&gt;

&lt;/table&gt;

&lt;/form&gt;<i>
</i>
``

No email addresses in posts, you don't want spam!
×

Success!

Help @newboy0714 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...