How To Set Mail From Address In PHP – Simple Examples

Welcome to a quick tutorial on how to set the mail from address in PHP. Getting some errors when sending out emails, or want to change the “email from” address?

  • To change the email from address with the mail function – mail("jon@doe.com", "Subject", "Message", "From: YOUR NAME <your@email.com>");
  • To set the from address using PHP mailer – $mail->setFrom("your@email.com", "YOUR NAME");
  • Alternatively, we can also change the sendmail_from directive in php.ini (Windows only).

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Mail From Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

SETTING THE EMAIL FROM

All right, let us now get into a few quick examples of setting the “email from” in PHP.

 

1) MAIL FUNCTION

1-mail.php
<?php
// (A) MAIL DATA
$to = "jon@doe.com";
$subject = "Test Email";
$message = "Hello World!";
$header = "From: your@email.com";

// (B) TO SET A DISPLAY NAME
$to = "Jon Doe <jon@doe.com>";
$header = "From: YOUR NAME <your@email.com>"; 

// (C) SEND!
echo mail($to, $subject, $message, $header)
  ? "Mail sent"
  : "Error sending mail" ;
  • For you guys who are using the native mail() function, just pass the From: YOUR@EMAIL.COM header into the fourth parameter.
  • If you want to set a display name, define the email in the format of DISPLAY NAME <WHOEVER@EMAIL.COM> instead.

 

 

2) PHP MAILER

2-phpmailer.php
<?php
// (A) INSTALL PHP MAILER
// https://getcomposer.org/
// composer require phpmailer/phpmailer

// (B) LOAD PHP MAILER
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require "vendor/autoload.php";

// (C) SET MAIL
$mail = new PHPMailer;
$mail->setFrom("your@email.com");
$mail->addAddress("jon@doe.com");
$mail->Subject = "Test Email";
$mail->Body = "Hello World";

// (D) TO SET DISPLAY NAME
// $mail->setFrom("your@email.com", "YOUR NAME");
// $mail->addAddress("jon@doe.com", "Jon Doe");

// (E) SEND!
echo $mail->send()
  ? "Mail sent"
  : "Error sending mail" . $mail->ErrorInfo ;

For you guys who are on PHP Mailer, things are a lot more straightforward – $mail->setFrom("YOU@EMAIL.COM", "DISPLAY NAME").

 

3) PHP.INI (WINDOWS ONLY)

php.ini
sendmail_from = YOU@EMAIL.COM

Lastly, for the Windows servers only – We can also hard code the email from address in php.ini. You guys who are on Mac and Linux, things can get rather complicated depending on which distro…. Which I will not cover in this simple guide. Do a separate search on your own if you want.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

How To Set Email From In PHP (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “How To Set Mail From Address In PHP – Simple Examples”

  1. David in Mississippi

    Another great tutorial. THANKS!

    However, in this tutorial, you say, “…for the Windows users only” — but it confuses me (and probably some others) whether you’re referring to Windows client computers or Windows SERVERS. I initially thought it would be servers, as they are prevalent in both Windows and LINUX flavors, but then you also mentioned Macs, which was confusing. I’m not aware of any servers that run on the MacOS. (Am I wrong?) That’s why I was confused. Might you clarify this? Thanks again!

    1. 1) Yes, windows server. PHP is a server-side script, it has nothing to do with the client.
      2) Yes, there are Mac servers. All modern Mac OSX comes with PHP pre-installed.

Leave a Comment

Your email address will not be published. Required fields are marked *