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("firstname.lastname@example.org", "Subject", "Message", "From: YOUR NAME <email@example.com>");
- To set the from address using PHP mailer –
$mail->setFrom("firstname.lastname@example.org", "YOUR NAME");
- Alternatively, we can also change the
sendmail_fromdirective 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|
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.
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
<?php // (A) MAIL DATA $to = "email@example.com"; $subject = "Test Email"; $message = "Hello World!"; $header = "From: firstname.lastname@example.org"; // (B) TO SET A DISPLAY NAME $to = "Jon Doe <email@example.com>"; $header = "From: YOUR NAME <firstname.lastname@example.org>"; // (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.COMheader 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
<?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("email@example.com"); $mail->addAddress("firstname.lastname@example.org"); $mail->Subject = "Test Email"; $mail->Body = "Hello World"; // (D) TO SET DISPLAY NAME // $mail->setFrom("email@example.com", "YOUR NAME"); // $mail->addAddress("firstname.lastname@example.org", "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)
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
LINKS & REFERENCES
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!