send email wordpress

How to send email programmatically

If you want to send an email through WordPress and without your theme and plugins, you can do it easily with the code below:

wp_mail( string|string[] $to, string $subject, string $message, string|string[] $headers = '', string|string[] $attachments = array() )

You can fill these parameters :

  • $to refer to the mail address (can be an array if there are multiple addresses)
  • $subject is your mail title
  • $message is the content of the mail. If you want to send your content with HTML, you can change your headers parameter (explained below)
  • $headers allow you to add additional headers. For example, for HTML, you can have this line :
$headers = array('Content-Type: text/html; charset=UTF-8');

Be careful if you use variables for your addresses or your mail content with $_POST or $_GET and content from your WordPress users. You should secure this kind of string to be sure your WordPress is secure.

Leave a Comment

Your email address will not be published.

You may also like