PHP Mail & Sending Email

PHP Server mail Config

In the php.ini file, PHP must be set up correctly so that your system can send emails. Open the php.ini file in the /etc/ directory and look for the [mail() function] section.

Windows

Windows users should make sure that there are two directives. The first is called SMTP and tells you the address of your email server. The second is called sendmail from, and it lets you set your own email address.

Windows should be set up so that it looks something like this:

[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net

; For win32 only
sendmail_from = admin@coderazaa.com

Linux

For Linux users, all they need to do is tell PHP where their sendmail program is. The sendmail  path directive should be given the path and any switches you want.

Linux should be set up so that it looks something like this:

[mail function]
; For Win32 only.
SMTP =

; For win32 only
sendmail_from =

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

You are now ready to go to sending email with php.

PHP Mail Intro

With the mail() function, you can send emails from a script itself.

Requirements

PHP needs an installed and working email system in order for the mail functions to work. The settings in the php.ini file determine which programme will be used.

Installation

PHP’s core includes the mail functions. You don’t have to install anything to use these functions.

Runtime Configuration

Name Default Description Changeable
mail.add_x_header “0” Add X-PHP-Originating-Script, which will include the filename and the UID of the script. For PHP versions above 5.3.0 PHP_INI_PERDIR
mail.log NULL The path to a log file where all calls to mail() will be recorded.
The log shows the full path of the script, the line number, the To address, and the headers. For PHP versions above 5.3.0
PHP_INI_PERDIR
SMTP “localhost” Only for Windows: The SMTP server’s DNS name or IP address PHP_INI_ALL
smtp_port “25” Only for Windows: The number of the SMTP port.
For PHP versions above 4.3.0
PHP_INI_ALL
sendmail_from NULL Only for Windows: Sets the “from” address for mail sent from mail () PHP_INI_ALL
sendmail_path “/usr/sbin/sendmail -t -i” Sets the location of the sendmail programme. This command also works in Windows. SMTP, SMTP port, and sendmail from are ignored if this option is set. PHP_INI_SYSTEM

 

 

PHP Mail Functions

Function Description
ezmlm_hash() Calculate the hash value that EZMLM needs.
mail() Scripts can be used to send emails directly.

How to Use and Define php mail

PHP’s mail() function is used to send an email. There are three required arguments for this function: the email address of the recipient, the subject of the message, and the message itself. There are also two optional parameters.

With the mail() function, you can send emails from a script itself.

Syntax

mail(to,subject,message,headers,parameters);

Here is what is said about each parameter.

Sr.No Parameter & Description
1 to

Required.
Identifies the person or people who will receive the email.

2 subject

Required.
Tells what the email is about.
There can’t be any newline characters in this parameter.

3 message

Required.
Sets the message that will be sent.
A LF (n) should come between each line.
Lines can’t be longer than 70 characters.

4 headers

Optional.
Adds more headers, such as From, Cc, and Bcc.
A CRLF (rn) should come between each of the extra headers.

5 parameters

Optional.
Tells the send mail programme about an extra parameter.

More Examples

Send an email with more header information:

<?php
$to = “user@coderazaa.com”;
$subject = “My subject”;
$txt = “Hello world!”;
$headers = “From: webmaster@coderazaa.com” . “\r\n” .
“CC: userelse@coderazaa.com”;

mail($to,$subject,$txt,$headers);
?>

Send an HTML email:
<?php
$to = “user@coderazaa.com, user_two@coderazaa.com”;
$subject = “PHP HTML email”;

$message = ”
<html>
<head>
<title>HTML Mail Example</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
“;

// Always set content-type when sending HTML email
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=UTF-8” . “\r\n”;

// More headers
$headers .= ‘From: <email@coderazaa.com>’ . “\r\n”;
$headers .= ‘Cc: other_cc@coderazaa.com’ . “\r\n”;

mail($to,$subject,$message,$headers);
?>

 

PHP mail function with Form Example

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<!– form starting –>
<form method=”post” action=”contact.php”>

<!– name –>
<input type=”text” placeholder=”NAME” name=”name” value=”” />

<!– email –>
<input type=”email” placeholder=”EMAIL” name=”email” value=”” />

<!– message –>
<textarea placeholder=”MESSAGE” name=”message”>
</textarea>

<input type=”submit” value=”Submit”>
</form>
<!– end of form –>

</body>
</html>

 

<?php
// Get data from form
$name = $_POST[‘name’];
$email= $_POST[’email’];
$message= $_POST[‘message’];

$to = “yourEmail@mail.com”;
$subject = “This is for subject”;

// The following text will be sent
// Name = user entered name
// Email = user entered email
// Message = user entered message
$txt =”Name = “. $name . “\r\n Email = ”
. $email . “\r\n Message =” . $message;

$headers = “From: noreply@coderazaa.com” . “\r\n” .
“CC: email@coderazaa.com”;
if($email != NULL) {
mail($to, $subject, $txt, $headers);
}

// Redirect to
header(“Location:thank-you.html”);
?>

PHP ezmlm_hash() Function

How to Use and Define

The ezmlm hash() function figures out the required hash value for storing EZMLM mailing lists in a MySQL database.

This function takes an email address and makes an integer hash value out of it. This value works with the EZMLM mailing list manager, and the EZMLM database can then be used to manage users.

Syntax

ezmlm hash(address);

Parameter Values

Information of how ezmlm_hash work

Parameter Description
address Required.Indicates the email address that is being hashed.

Technical Details

Return Value: Returns the address parameter’s hash value, or FALSE if it fails.
PHP Version: 4.0.2+

Example

Calculating hash value for email address:

<?php
$user_email = “someone@coderazaa.com”;
$hash_value = ezmlm_hash($user_email);

echo “The hash value for $user_email is: $hash_value.”;
?>

People also search

Leave a Comment

Scroll to Top