How do I send email from PHP the basic?
This article or knowledge base answers the following:
- How to send email from php very basic?
- How to send simple text or HTML emails directly from the script using the PHP
mail()
function?
I would like to send an email from my PHP/HTM code
- If you use the PHP mail function, the PHP mail() function is a method to directly send an email from your website.
- Send email from the HTML, PHP code to your end-users.
- Auto respond the Newsletter, submit action and more.
- You can read more about the function and what it does on PHP's website: http://php.net/manual/en/function.mail.php
- If you are new to PHP coding, you can learn the basic, for beginners, recommend w3schools for beginners.
Example of PHP code within HTML
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello world, My First Send EMail PHP Code";
?>
</body>
</html>
Example of PHP mail function:
The php mail() function enables you to send emails directly from a script that can be embedded in your HTML code.
To learn more about the PHP Mail function, click here
Syntax:
mail(to,subject,message,headers,parameters);
- to: the receiver email address
- subject: The subject of the email, you can not add newline characters to subject "\n"
- message: The body or message to be sent, you can use newline "\n" for the message, each line can not exceed 70 characters
- headers: Optional, this is an additional email add-on like "CC", "BCC"
- from: the sender of the email
<?php
//testing my first php mail()
mail('user@domain.com', 'Subject Line Here', 'Body of Message Here', 'From: info@domain.com');
?>
To send a test email using the PHP Mail function
- You can test your PHP script just copy and paste the code below
- Note: Make sure you change the emails "test@example.com" to the right email
Example #1 send a test email with variable and values syntax
<?
$to = 'test@example.com';
$subject = 'The test for php mail function';
$message = 'Hello';
$headers = 'From: test@test.com' . "\r\n" .
'Reply-To: test@test.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Example #1.2 send a test email for the line that is larger than 70 characters
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>
Example #2 Sending PHP mail with extra headers.
The addition of basic headers, telling the MUA the From and Reply-To addresses:
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
NEED A HOSTING SOLUTION OR LOOKING TO SWITCH HOSTING PROVIDER
- If you do not have a cloud hosting, looking for the best web hosting, email hosting, cloud hosting provider
- We build our hosting platform for developers, engineers click here to see our hosting plan
- Want to learn more about our hosting platform, click here for info about our hosting platform
To access your default email or create email
- For steps to access webmail on AkolagTech(If you have a hosting plan with us) cPanel click here
Links to other useful resources: