InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Sending Mail in PHP
PHP

Sending Mail in PHP

InfinityCoder December 20, 2016

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.

Use Zetacomponent’s ezcMailComposer class:

1
2
3
4
5
6
7
8
9
10
$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:

1
2
3
4
5
$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);

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$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:

1
2
3
4
5
6
7
$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:

1
2
3
4
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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Colorizing Console Output in PHP
You want to display console output in different colors. Use …

Colorizing Console Output in PHP

Parsing Program Arguments with getopt in PHP
You want to parse program options that may be specified …

Parsing Program Arguments with getopt in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap