All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Send An Email With PHP

Last Updated : Mar 11, 2024

How To Send An Email With PHP

In this article we will show you the solution of how to send an email with php, with the specifics about just how their system sends email, PHP should be configured correctly inside the php.ini file.

Locate the [mail function] section inside the php.ini file in the/etc/directory.

Windows users should supply two directives.

The first is SMTP, which specifies your email server's address. The second is sendmail from, which specifies your email address.

Sendmail must be pointed to from the PHP code in Linux. The sendmail path directive should be passed the path along with any desired switches. Emails are sent using PHP's mail() function.

Three parameters are required for this function: the email address of the recipient, the message's subject, and the actual message.

All of the content in a text message sent via PHP will be handled as simple text.

Even though HTML tags can be used in text messages, they won't be formatted in accordance with HTML syntax and will instead be displayed as plain text.

However, PHP offers the option to transmit an HTML message as such an actual HTML message.

You can choose the character set, content type, and Mime version to submit an HTML email when you send an email.

Step By Step Guide On How To Send An Email With PHP :-

<?php
$msg = "First line of text\nSecond line of text";
$msg = wordwrap($msg,70);
mail("someone@example.com","My subject",$msg);
?>

HTML email : -

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
  1. Tags to summarize identifiers begin with "?" and end with "?>." Short style tags should be enabled in php.ini file types on servers.
  2. Then we create the message.
  3. If the lines were indeed longer than 70 characters, we use word wrap().
  4. We then send a message.
  5. After that we close the program by sending email.

Conclusion :-

Emails are sent using PHP's mail() function. Three parameters are required for this function: the email address of the recipient, the message's subject, and the actual message.

Two additional parameters are optional. The Content-type header needs to be changed to multipart/mixed to begin creating a mixed content email.

It is then possible to define the boundaries between the text and attachment sections.

A boundary is introduced by two hyphens, followed by a distinct number that cannot appear in the email's message.

I hope this article on how to send an email with php helps you and the steps and mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪