Send email from php

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:

  • send email from php, php email, sendmail, email php, send email
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

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

AkolagTech Hosting makes it easy for you to access cPanel, Email, and other services Steps to...

How to set up cPanel email on my phone

Accessing your email account from a desktop or mobile device client application To set up your...

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?

Note: Your email should be working by default once you host your website or email with...

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

How Do I Send PHP Mail via SMTP or with My cPanel Email? How do I send PHP mail using my cPanel...