While developing another plugin for WP, I remembered a small issue I once ran into — programmatically sending email messages. There's really nothing complicated about it, but still, it might be useful to someone.
Usually, across the web, I’ve seen the following code:
wp_mail( 'to_email@example.com', 'Email Subject', 'Email Body' );
What does it do? It sends an email — correct. But! The sender name will be “WordPress” and the sender address will be “wordpress@” + your domain.
What if we want to change that? Personally, I like using “no-reply” as the sender.
You can fix that by adjusting the code like this:
wp_mail( 'to_email@example.com', 'Email Subject', 'Email Body', array( 'From: no-reply <no-reply@example.com>', ));
Better now, right?
Wait a minute — it’s also nice when someone addresses us by name. And in the “To” field, we’ve only specified the email address.
How can we fix that? — in a similar way 🙂
wp_mail( array( 'ToName <to_email@example.com>', ), 'Email Subject', 'Email Body', array( 'From: FromName <from_name@example.com>', ));
Of course, this is not the full functionality of the `wp_mail` function — check out the source code and you’ll see for yourself.
As for me — that’s all I have to say about it for now 🙂
