PHP Internet Services Sending Mail - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Internet Services Sending Mail - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, June 21, 2019

PHP Internet Services Sending Mail

PHP Internet Services


Sending Mail


Problem

You want to send an email message. This can be in direct response to a user’s action, such as signing up for your site, or a recurring event at a set time, such as a weekly newsletter.

Solution

Use Zetacomponent’s ezcMailComposer class:

       $message = new ezcMailComposer();
       $message->from = new ezcMailAddress('webmaster@example.com');
       $message->addTo(new ezcMailAddress('adam@example.com', 'Adam'));
       $message->subject = 'New Version of PHP Released!';
       $body = 'Go to http://www.php.net and download it today!';
       $message->plainText = $body;
       $message->build();

       $sender = new ezcMailMtaTransport();
       $sender->send($message);

If you can’t use Zetacomponent’s ezcMailComposer class, use PHP’s built-in mail() function:

       $to = 'adam@example.com';
       $subject = 'New Version of PHP Released!';
       $body = 'Go to http://www.php.net and download it today!';

       mail($to, $subject, $body);

Discussion

The Zetacomponent ezcMailComposer class gives you a way to construct email messages. How the component sends the message depends on which ezcMailTransport implementation you use. In the preceding example, ezcMailMtaTransport() uses the PHP mail() function internally, so it benefits from your PHP configuration. The ezc MailSmtpTransport can be used to talk to an SMTP server directly, as follows:

       $message = new ezcMailComposer();
       $message->from = new ezcMailAddress('webmaster@example.com');
       $message->addTo(new ezcMailAddress('adam@example.com', 'Adam'));
       $message->subject = 'New Version of PHP Released!';
       $body = 'Go to http://www.php.net and download it today!';
       $message->plainText = $body;
       $message->build();

       $host = 'smtpauth.example.com';
       $username = 'philb';
       $password = 'jf430k24';
       $port = 587;

       $smtpOptions = new ezcMailSmtpTransportOptions();
       $smtpOptions->preferredAuthMethod = ezcMailSmtpTransport::AUTH_LOGIN;

       $sender = new ezcMailSmtpTransport($host, $username, $password, $port,
 $smtpOptions);

       $sender->send($message);

If you can’t use the Zetacomponent ezcMailComposer class, you can use the built-in mail() function. The program mail() uses to send mail is specified in the send mail_path configuration variable in your php.ini file. If you’re running Windows, set the SMTP variable to the hostname of your SMTP server. Your From address comes from the sendmail_from variable.

The first parameter to mail() is the recipient’s email address, the second is the message subject, and the last is the message body. You can also add extra headers with an optional fourth parameter. For example, here’s how to add Reply-To and Organization headers:

       $to = 'adam@example.com';
       $subject = 'New Version of PHP Released!';
       $body = 'Go to http://www.php.net and download it today!';
       $header = "Reply-To: webmaster@example.com\r\n"
    .                      "Organization: The PHP Group";

       mail($to, $subject, $body, $header);

Separate each header with \r\n, but don’t add \r\n following the last header.

Regardless of which method you choose, it’s a good idea to write a wrapper function to assist you in sending mail. Forcing all your mail through this function makes it easy to add logging and other checks to every message sent:

        function mail_wrapper($to, $subject, $body, $headers) {
              mail($to, $subject, $body, $headers);
              error_log("[MAIL][TO: $to]");
        }

Here a message is written to the error log, recording the recipient of each message that’s sent. This provides a timestamp that allows you to more easily track complaints that someone is trying to use the site to send spam. Another option is to create a list of do not send email addresses, which prevent those people from ever receiving another message from your site. You can also validate all recipient email addresses, which reduces the number of bounced messages.


No comments:

Post a Comment

Post Top Ad