Php mail according to its php official documentation parent page is simply described as mail – send mail.
The Php mail is supported from 4th version through till later version PHP 7 the Php current version as when this post was published.
We all know about PHP Company
How does PHP mail work?
Php mail actually uses a sort of settings (configurations) present in php.ini found in section PHP Mail Runtime Configuration.
According to GolezTrol a strackoverflower gave an approved upvoted answer concerning this Question. he said the php.ini mail configuration settings can be overrode using ini set
The ini_set syntax that can override the php mail configuration
Does PHP mail function work on localhost?
Wow Is it possible to send e-mail from local host using php mail function? Really I was wondering this can never be possible but it is possible as review by a Quora subscriber Chris Divala
Though he made mention of steps to follow which I did test but it ain’t working.
Maybe Chris Diyala did not understand the question but the question is actually if you can send email right from your computer system localhost wamp or xampp
Also from Quora Dibya Sahoo give an explained that you can be able to send from localhost but both miss the answer to the question
Dibya Saboo provided a suggestion of making use of ESP (Email Service Provider) which its documentary that is connected to the asked Question is not working.
Volkerk from Stackoverflow stated with steps of how one can send email using SwiftMailer or Fake Sendmail while using Mercury server configuration but Mihai Lorga Pointed that is going to be tedious to send mail from Localhost to any mail providers
In my opinion sending email from your localhost machine will be harder than using a stmp from a host server.
What is PHPMailer PHP?
As the name implies it is an open-source email-sending library highly popular, actively developed for PHP.
PHPMailer Can be gotten from GitHub, though we more than 1000 available PHPMailer but not all are reliable but this listed below are working properly since updated and maintained regularly by its contributors.
How to install PHPMailer?
PhpMailer is an easy to install and use script
- Upload phpmailer script your public_html localhost folder.
- Edit its settings in order to replace the variables with your domains own and then therefore save.
- Run the index.php
- Simply use the send email form contain to construct your mail and send.
How do I send an email?
Alright sending mail I assumed was done mostly with php and I treat only to write email with PHP in this post.
Though you may send mail in python and Java but in both I think php have a role to play in all here below is php sending mail.
It is easy whence you can be able to define the php mail() function as stated in the php official documentary for mail forwarding.
Please replace example.com with your domain defined variables then upload to your online server and run the page therefore check the $to variable the email address to which you are sending to then you will see the message in it.
What I will cover in this post
- php mail smtp
- php mail script
- php mail from address
- php mail not working
- php mail bcc
- php mail attachment
- php mail multiple recipients
- php mail cc multiple recipients
- php mail headers
php mail smtp
Smtp is an acronym which stands for Simple Mail Transfer Protocol which may define as a communication protocol for electronic email.
Does PHP mail use SMTP?
The PHP mail() function does support SMTP on a hosted server, but uses the sendmail() instead On a *nix machine.
How can you set smtp for PHP mail function?
The SMTP mail settings can be configured from “php. ini” file in the PHP installation folder.
I will explain all and how to use it under php mail script
php mail script
PHP mail() function code is built in function from php that enables developers to send email directly from script to any designated email through a hosted server.
Basic php mail script
<?php // The message $message = "Line 1\r\nLine 2\r\nLine 3"; // In case any of our lines are larger than 70 characters, we should use wordwrap() $message = wordwrap($message, 70, "\r\n"); // Send mail('email@example.com', 'My Subject', $message); ?>
<?php $to = 'firstname.lastname@example.org'; $subject = 'the subject'; $message = 'hello'; $headers = 'From: email@example.com' . "\r\n" . 'Reply-To: firstname.lastname@example.org' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?>
The codes above two different basic php code to send email
php mail from address
From the above php mail script you must make sure that the php mail from address is properly written else the mail will not be send
$headers = 'From: email@example.com' . "\r\n" .
Php mail() may work with or without the from address when the from address is not specified the function automatically assumes from address to be the hosted server address see the 1st code you will notice it comes with no from address but still the php mail function works well
Php mail parameters
According to the official documentary of php its mail function have only 3 parameters with an optional parameter
To : Receiver, or receivers of the mail.
Subject : Subject of the email to be sent.
Message : Message to be sent.
While composing email in php observe the rules below
- Lines should not be larger than 70 characters.
- Every line must end with
\r\nfor new one to start
- The acceptable format of php mail to address are as listed
firstname.lastname@example.org email@example.com, firstname.lastname@example.org User <email@example.com> User <firstname.lastname@example.org>, Another User <email@example.com>
- The subject of the email must comply and satisfy RFC 2047
php mail not working
Four possible factors that can make your php mail function not to work or stop working are
- when the php mail() if present but not properly defined
- if the php mail() parameters are not complete
- when the php mail to or from addresses are invalid
\r\nshould be in double quotes otherwise it is taken literally.
This common error of php mail is normally experienced in all round while writing every code using php once a quote be it single or double is omitted where it should then the whole code will throw an error.
was observed, corrected and working in the one on the
had this problem posted in stackoverflow by bot which was resolved by drew010
- Omitting or miss placing of dots in the php mail headers
php mail bcc
What is BCC in an email?
BCC is email acronym which stands for Blind carbon copy
What is the Purpose of BCC?
The BCC enables the email to hide the identity email address another receiver filled in the bcc: field from other recipients.
Alright having this question in our thought bcc you cannot reveal or unveil any identity entered in the bcc: field from your mailbox bcc is really conceal and cannot be unblind to reveal other recipients of same message you did receive.
To include bcc in your php mail() function the additional_header of the php mail function is applied.
Gregheo is an stackoverflower who answered this question PHP Email sending BCC with 50 upvotes
The php mail bcc is one of the php mail headers fundamental content so it defined like shown below in the above capture
$headers .= ‘Bcc: firstname.lastname@example.org’ . “r\n” .
php mail attachment
To send attachments with php mail() was becoming difficult and disturbing but becomes much easier with phpmailer.
SDC a stackoverflower gave the idea on his post on agreement to @Mihailorga comments that instead of writing many stacks of code with php mail() in order to send attachment via php code that phpmailer is made easy and less bug free
SDC mentioned in his post “It’s just
that one line
$email->AddAttachment(); — you couldn’t ask for any
See the steps on how to send email attachment in php using phpmailer
Awesome isn’t it, it is but if you really don’t want to depend on PHPMailer found on that github.
we already published codes on shopinson about php mail function with image/documents attachment check it out it contains PHP mail attachment script and php mail form as well
what you will observe is that in our already published we included a form to attach your file which you want to send which have been corrected and working
While using phpmailer you may not necessary need to build a form but you only need to include the to send file in the script as shown
Php mail attachment one common mistakes
This mistakes if you are unaware make take you a lot time to observe it correct and working
- if you are using php mail() instead of the phpmailer which you need to built a php mail form therefore omitting the
enctype="multipart/form-data"will definitely not allow your attached file to be sent to its receiver it will not throws errors but will send other messages written excluding the attached file to its reciever.
php mail multiple recipients
Alright while sending mail there 3 different mail recipient fields
- Bcc: field
- Cc: field
- To: field
Only BCC and CC are the two recipient field specified in the php mail() headers
I am trying to send an email to multiple address with php script. I can send an email to multiple addresses.
Yes you can specify the multiple addresses in the php mail headers for only bcc and cc fields as below
$headers .= 'Bcc: email@example.com, firstname.lastname@example.org, email@example.com ' . "r\n" .
$headers .= 'Bcc: Web Master <firstname.lastname@example.org>, Tina Peson <email@example.com>, Guru Pet <firstname.lastname@example.org> ' . "r\n" .
Two things about multiple recipients in php mail
Below listed are the proper way to include multiple recipients in php mail() while sending mail using a script.
- if only the email addresses are specified, each email address must be separated from the next with a comma and a space
- if the receivers fullname is specified before the email address, therefore each email address must enclosed which an angled bracket where each was separated from the next also with a comma and a space.
php mail cc multiple recipients
Anke corbin from posted Quora that cc means carbon copy and she explained it .
Chris Hoffman ceo of howtogeek explained cc that some refer to it as Courtesy Copy which he conclude it best define the meaning of cc in email.
Both made us understand that cc:field is similar to to:field.
To send email to multiple recipients in cc: field using php mail function see the example illustrated above about the bcc: field and follow same procedure while ensuring that you consider the above two things to observe while including multiple recipients in php mail to send email
$headers .= 'cc: email@example.com, firstname.lastname@example.org, email@example.com ' . "r\n" .
$headers .= 'cc: Web Master <firstname.lastname@example.org>, Tina Peson <email@example.com>, Guru Pet <firstname.lastname@example.org> ' . "r\n" .
php mail to multiple recipients
The to: field is the only recipient that is not specified within the php mail header while writing the recipient email only or names and emails you must also adhere to the two things about multiple recipients in php mail above.
$to = ‘email@example.com, firstname.lastname@example.org’;
$to = ‘User <email@example.com>, Another User <firstname.lastname@example.org> ';
- php mail html
- php mail gmail
php mail headers
php mail headers has 9 fundamental parts
The Php mail() Content – Type Code is of three script based on three categories
|No html message||Content – type undefined|
|Message and attachment|
Below is a complete php mail headers
// FULL HEADER $headers = "From: testsite < email@example.com >\n"; $headers .= "Cc: testsite < firstname.lastname@example.org >\n"; $headers .= "Bcc: Customer < email@example.com >\n"; $headers .= "X-Sender: testsite < firstname.lastname@example.org >\n"; $headers .= 'X-Mailer: PHP/' . phpversion(); $headers .= "X-Priority: 1\n"; $headers .= "Return-Path: email@example.com\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Write us if you have suggestion/inquiries to this post or contribution to php mail() please drop comment below for follow up