Send email from php

Short answer: PHP's built-in mail() function can send basic email directly from a script (see below), but for reliable delivery with fewer spam-folder issues, send via SMTP with your real cPanel mailbox credentials instead - see How do I send PHP mail via SMTP for that method.

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


 

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:

  • 1 Users Found This Useful
  • send email from php, php email, sendmail, email php, send email
Was this answer helpful?
Still stuck? Ask our AI assistant for help troubleshooting this.

Related Articles

How do I verify, check DKIM, SPF, TXT record of my Email domain hosting

Short answer: In cPanel open Email Deliverability, check that your domain shows Valid, then add a...

How to set up cPanel email on my phone

Short answer: Create the address in cPanel Email Accounts, click Connect Devices, then use...

How would i create and access my email account for my hosting plan/cPanel - with web browser, mail client or mobile phone?

1. How do I create an email account? Note: These steps can be skipped if you already created...

How to troubleshoot email issues with your cPanel Hosting?

Short answer: First test with webmail. If webmail works, the mail server is fine and the problem...

How do I send PHP mail via SMTP or with my cPanel email?

Short answer: create a mailbox in cPanel, then send through your own mail server...