PHP Lesson 18 – How to Send e-mails with PHP
The email function of PHP is very handy because it allows us to send form data or create entire newsletter scripts. You can also use this procedure for a guestbook, for example, if you receive an email when someone enters the guestbook.
If you perform this function on your computer, you will normally receive an error message:
Warning: mail (): …
This is because your local server cannot send emails. You can ignore the error message because, on the real server, it should not actually occur.
With the PHP function mail() can you send the mails, the command looks like this:
<?Php mail($recipient_email, $subject, $message, "From: sender <[email protected]>"); ?>
It makes sense to save the individual texts in variables so that you keep the overview.
However, only text e-mails can be sent, HTML e-mails are a bit more complicated (see below).
A finished PHP mail function would look like this:
<?php $recipient = "[email protected]"; $subject = "The mail function"; $from = "From: Robort Gabriel <[email protected]>"; $text = "Here you will learn how to use PHP to send mails, coding180.com"; mail ($recipient, $subject, $text, $from); ?>
If your server is set correctly, the e-mail should arrive immediately.
Of course, you can also query the texts from a form.
The mail will be sent as it is in the source text of the script. For example, if you use a ü through & uuml; (HTML code for a ü) replace, so it says later in the mail: & uuml; , Because there are then not the umlauts displayed, but the HTML code for it (in text emails).
In addition, you are not allowed to use HTML commands, such as in the e-mail is, so that is not converted to a line break, but in the mail is then.
For line breaks, a simple enter in the source text or a \n suffices. To pick something, you have to use tab or space.
You can also send HTML emails with PHP. But you should know that not all e-mail programs and e-mail providers correctly display these HTML emails. Also, not all e-mail programs and providers also show all HTML commands, many providers only allow very specific commands to protect their visitors from defective e-mails.
For PHP to send HTML emails, you need to change the $from variable.
<?Php $recipient = "[email protected]"; $subject = "The mail function"; $from = "From: Robort Gabriel <[email protected]> \ n"; $from. = "Reply-To: [email protected] \ n"; $from. = "Content-Type: text / html \ n"; $text = "Here you will learn how to use <b> PHP </ b> mails sent "; mail ($recipient, $subject, $text, $from); ?>
The variable $ from is simply added, but make sure that the sender still needs a \n.
In addition, we have a reply (Reply) address specified.
For the text, we have to be careful that we need to “mask” the quotation marks with a backslash (\).
<?Php $text = "Learn how to send mails with <strong> PHP </ strong>"; ?>
The text of the message must be formatted using HTML! Here is enough for a line break no Enter, but you have to use <br />.
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?