How to Sendmail in PHP? [Complete Guide] | Pepipost

the author voluntarily contributed this tutorial as part of the pepipost write-to-contribute program.

intro

php comes with a default mail() function that allows you to send mail directly from a php script. here in this tutorial we will talk about the prerequisites to send a mail directly from a php script, the syntax and its parameters.

requirements

  • php4, php 5 or php 7
  • php configured to send mail. In case your php configuration is not configured to send emails to the outside world, please refer to the steps mentioned at the end of this article.

syntax

mail(to,subject,message,headers,parameters);

parameters

astring | The recipient’s email address is required.note: must comply with rfc 2822. you can pass one or multiple email addresses using comma separated.some examples of what the values ​​will look like:

  • [email protected]
  • [email protected], [email protected]
  • recipient name <[email protected]>
  • recipient name <[email protected]>, recipient name 2 <[email protected]>

subjectstring | The subject line of the email to be sent is required. note that the subject must comply with rfc 2047.

message string | required the content of the mail you want to send. each line of the email must be separated with a crlf (rn) and each line must not exceed 70 characters. which is at the beginning of a line. To resolve this, replace dot with a double dot, using the following code:

additional_headersmixed (string or array) | Optional, this parameter is used to pass any additional email headers like from, cc, and bcc. each additional header must be separated with a crlf (rn).note: When sending the email, make sure there is a from header. you can set the from header, either using the additional_headers parameter or you can also set a default value in php.ini.

See Also:  Questions and Answers about the Third-round Economic Impact Payment — Topic J: Payment Issued but Lost, Stolen, Destroyed or Not Received | Internal Revenue Service

additional_parametersstring | optional, this extra_parameter can be used to pass additional flags to the sendmail program as configured in the sendmail_path configuration option. for instance; you can use this parameter to set the envelope sender address when using sendmail with the -f option. php by default internally escapes the values ​​that come in this parameter with escapeshellcmd() to avoid any possible command execution.

return value

the mail() function returns true if the smtp server successfully accepted the mail for delivery, false otherwise.

being true does not necessarily mean that the email is delivered to the recipient’s server. true is just an indication that your mail was successfully queued to the smtp server for delivery. returns true if the mail was accepted for delivery, false otherwise. It’s important to note that just because mail was accepted for delivery, it doesn’t mean the mail will reach its intended destination.

note: the mail() function will not work on the local server. a server connected to the internet and open smtp ports will be required to send mail.

some use cases and practical examples

1. how to send an html mail

2. how to send mail with an additional command line parameter. As described above, for this use case, you should use the additional_parameters parameter to pass an additional parameter to the configured program to be used when sending mail using sendmail_path.

common errors/exceptions with the php sendmail function

  1. cannot send a large volume of emails using the mail() functionit is not recommended to use a mail() function to send large volume emails, because it opens and closes one smtp socket connection for each email. this really isn’t very efficient. if you want to send a large number of emails in php, it is recommended to check out the pear::mail and pear::mail_queue packages or use some good email service provider that can help you scale your email volume.
  2. not receiving emails sent via the php mail() functionalthough there could be a number of reasons for the failure, but one of the twisted problems that many encounter is with the mailing agents. unix mail transfer, which replace lf with crlf automatically, leading to duplicate cr if crlf is already used in your code. in such a case, try using an lf (n) only. read more about message formatting in rfc 2822.
  3. warning: mail(): “sendmail_from” is not set in php.ini or the “from:” custom header is missing. I have not mentioned the from header using the additional_headers parameters.
See Also:  Why Am I Getting Spam From My Own Email Address?

configure php to send mail

In order to configure anything related to php you need to change the `php.ini` file. so, we’ll edit the php.ini file to configure sendmail.

You can easily locate or search for your php.ini file on linux using the following command:

default location is `/etc/php.ini`

you can find the same on windows where xampp or lampp is installed:

`c:xamppphpphp.ini`

clarification:

  • xampp (x (for “some operating systems”), apache, mysql, perl, php)
  • lampp (linux, apache, mysql, perl, php)

change php.ini file to add mail configuration.

1. open your php.ini file using below: for linux/mac os:

for windows: using notepad

2. search for [mail function] in the file. it will be as shown below:

3. add your mail server details to the file or in case you have one you can change it (mail server can be your own i.e local mail server or you can use any esp as mail server) .for linux/mac os: – search for `sendmail_path` and make sure; it is not (the semicolon is used to show that the line is commented out). by default it will use `/usr/bin/sendmail -t -i`, you can change this if you are using a custom .path for window:– search for `smtp = localhost` and change it to the desired mail server (any esp or localhost) no changes required if you are using your own localhost.- or you can also use any mail service provider’s smtp server email such as pepipost, sendgrid, mailgun, sparkpost.

See Also:  How to Receive Fax in Your Gmail Inbox - Google Fax Online

4. save/close php.ini file

5. the final step, don’t forget to restart your webserver/php-fpm.

pro tip: you can host a simple “info.php” on your web server to check each and every one of your php settings using the following 2 line code:

save and exit the file.6. reload your web server and php-fpm.7. click http://localhost/php_info.php in your web browser.

conclusion

hope this tutorial can help you to send mail using php.

In case you run into some issues not mentioned above in the tutorial, or if you have some suggestions, feel free to contribute below in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *