You can test PHP SMTP functions with the following two examples. The first one is standard SMTP while the second one is SMTP with SSL.
We strongly recommend using an SMTP relay that requires authentication. Sending mail through unauthenticated SMTP servers (including the localhost relay on Cloud Sites) can result in delays or undelivered email due to stringent anti-spam filters.
You will only need to change the following variables:
The host, username, and password values will depend on the provider you're using to send your email. If you have a legacy Sites email account or are using Rackspace Email the host would be "mail.emailsrvr.com" and the username would be your email address. If you're using another service such as Mailgun or Gmail to send email, you'll need to fill in the SMTP server for that service. Mailgun server information can be accessed via your Mailgun control panel.
<?php
require_once "Mail.php";
$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
$host = "mail.emailsrvr.com";
$username = "webmaster@example.com";
$password = "yourPassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
You will only need to change the following variables:
<?php
require_once "Mail.php";
$from = "Web Master <webmaster@example.com>";
$to = "Nobody <nobody@example.com>";
$subject = "Test email using PHP SMTP with SSL\r\n\r\n";
$body = "This is a test email message";
$host = "ssl://secure.emailsrvr.com";
$port = "465";
$username = "webmaster@example.com";
$password = "yourPassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Note: Mail.php is a PEAR module and is installed on the server. It is included in the default include_path for PHP, so requiring it here will work by default without any additional effort on your part.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

32 Comments
Can I use SMTP of different host in MY host
For instance, if I take host as google.com and give my username and password, and Setup SMTP in my Hosting, So that when it sends an email, it doesn't bother my hosting.
It just Cleanly Pick up and Connect with google apps host and than starts sending my email.
Thanks
Re: Different host
What is the correct syntax
$headers["Content-Type"] = "text/html; charset=iso-8859-1"; But doesnt work...
Re: PHP headers
header('Content-Type: text/html; charset=iso-8859-1');
Make sure you include any header() calls before the page delivers any output (before the html tag).
For more info you can check the manual page for header(): http://php.net/manual/en/function.header.php
Attachments
http://pear.php.net/manual/en/package.mail.mail-mime.example.php
Add CC and BCC
Add CC and BCC
Email php limited to using the $username as $to only?
$password for each email address and use them depending on the enquiry type of the form.
Re: username
I'm not certain how the PHP SMTP can handle multiple recipients with its built-in functions (you'd want to check the PHP documentation for that), but a simple workaround if it's a short list of recipients might be to just create an array of "to" addresses, then run a loop to cycle through the array and create an email to each one in turn.
SMTP username / password
Re: username and password
We'll look at updating this article to make that more clear. If you still have any problems definitely let us know.
Thanks, Jered!
re: username and password
My mistake... I posted this
Re: SMTP data
For that matter, you could save a bit of trouble by signing up with SendGrid and sending mail through them (in which case, you'd use the username and password for their service). It's free for Rackspace Cloud users up to 40,000 emails a month. More details on that here:
http://www.rackspace.com/knowledge_center/sendgrid
How do i send html format email with help of above code?
I am using 2nd option of code and tried to send html format of email but it send junk code of html in the mail body. I have added below option in header but its not working at my end.
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
any suggestion what changes need to do to send html format mail.
Re: html mail
http://php.net/manual/en/function.mail.php
Hopefully that does the trick.
How to send HTML mail in PHP
$to = “recipient@domain.com“;
$subject= “Subject of email”;
$message= “This is <h1>HTML</h1> mail.”;
$fromName = “Name of the sender”;
$fromEmail = “sender@domain.com“;
// To send HTML mail, the Content-type header must be set
$headers = “MIME-Version: 1.0? . “\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1? . “\r\n”;
// Additional headers
$headers .=”From: $fromName <$fromEmail>”.”\r\n”;
//Mail function
mail($to, $subject, $message, $headers);
Source:
<a href="http://phphelp.co/2012/05/03/how-to-send-html-mail-in-php//">http://http://phphelp.co/2012/05/03/how-to-send-html-mail-in-php//</a>
OR
<a href="http://addr.pk/a1c4">http://http://addr.pk/a1c4</a>
How add "cc" and "bcc" in php code
I am useing smtp. How add cc and cc in my code. Please help me.
<?php
include('SMTPconfig.php');
include('SMTPClass.php');
$to='webmaster@advenser.com';
$subject="Contact from Advenser.com";
$from="webmaster@advenser.com";
$fname=$_POST["txtFName"];
$lname=$_POST["txtLName"];
$com=$_POST["txtCompany"];
$web=$_POST["txtWebsite"];
$email=$_POST["txtEmail"];
$tel=$_POST["txtPhone"];
$adrs=$_POST["txtAddress"];
$msg=$_POST["txtComments"];
$hear=$_POST["txtHear"];
$body ="
First Name : $fname
Last Name : $lname
Company : $com
Website : $web
Email : $email
Telephone : $tel
Address : $adrs
Messages : $msg
How did you hear about us : $hear
";
$m="";
if($_POST["ans"]==7)
{
if(ereg("([0-9,a-z,A-Z]@[0-9,a-z,A-Z].[0-9,a-z,A-Z])",$email))
{
if($_POST["C"]=="Send")
{
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
if($SMTPMail==true)
$m="Mail sent Successfully";
else
$m="Sent Failed";
}
}
else
{
if($_POST["C"]=="Send")
{
$m="Invalid email address";
}
}
}
else
{
if($_POST["C"]=="Send")
{
$m="Invalid Answer";
}
}
?>
Re: cc and bcc
If you use the Mail.php from the example above, to add cc or bcc you would modify the "headers" array to include an entry for "cc" and one for "bcc".
Re: adding cc & bcc in smtp mail
re: cc and bcc
http://php.net/manual/en/function.mail.php
Using Cc - working example...
require_once "Mail.php";
$from = "Mister Smith <mister@smith>";
$to = "Mister Smith<mister@smith.com>";
$cc = "Mister Brown <mister@brown.com>";
$recipients = $to.", ".$cc;
$subject = "Test subject";
$body = "My message body line 1" . "\r\n\r\n";
$body .= "My message body line2". "\r\n\r\n";
$host = "mail.emailsrvr.com";
$username = "xxxx@yourdomain.com";
$password = "xxxxxx";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject, 'Cc' => $cc);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($recipients, $headers, $body);
?>
Hope that helps someone.
For HTML email, just add the additional header information as described in a comment above.
Thank you ,the script is very
recipients/subject in body
Here's the code I am using:
$host = "xxxxxxxxxxxxxxx";
$port = "465";
$user = "xxxxxxxxxxx";
$pass = "xxxxxxxxxxxxx";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $user,
'password' => $pass));
$mail = $smtp->send($to, $headers, $body);
I've checked the emails in in Outlook and webmail just to make sure it wasn't the email app - and the result is the same.
Thanks.
re: recipients/subject in body
Also, have you tried a different mail server, to see if that changes the result? You can set up an account with Mailgun or Sendgrid that would allow low-volume mail sending, so it may be worth seeing if the result is the same with them.
re: recipients/subject in body
thanks for the response.
Yes, the variables are defined earlier, I have tested each one and they are correct.
The fact that they are in the body is not by design. As a test I removed the middle line from:
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
and it removed the 'To' part from the body of the email.
I'm using the emailsrvr mail server so I'm guessing that shouldn't be the cause of it?
I may look at Mailgun or Sendgrid as I'm struggling to think what else I can check at this point, let me know if you spot anything.
re: recipients/subject in body
I have managed to get this working using Mailgun, but this does have the downside that (unless you pay) you lose the ability to set the from variable as from your own domain.
Given that the mailserver I'm using is hosted by rackspace and the code I'm trying to get to work is also based on the rackspace code it would be good to know why I can't pass the subject variable - and why the to variable ends up being set to 'undisclosed recipients'.
re: Mailgun
I do agree that if it works with Mailgun it should work with the Rackspace Email servers as well. I'll try to look into that further. It might be worth trying "smtp.emailsrvr.com" and port 587 to see if the non-ssl mail server behaves any differently, but only if you really want to use the Rackspace mail server with the code. If you have things working on Mailgun, no need to change to test that if you don't want to - I'll check with some folks on the backend to see if they can tell why this might be happening.
re: recipients/subject in body
thanks for the response.
Yes, the variables are defined earlier, I have tested each one and they are correct.
The fact that they are in the body is not by design. As a test I removed the middle line from:
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
and it removed the 'To' part from the body of the email.
I'm using the emailsrvr mail server so I'm guessing that shouldn't be the cause of it?
I may look at Mailgun or Sendgrid as I'm struggling to think what else I can check at this point, let me know if you spot anything.
PS this is the 5th time I've tried to submit this message but it keeps throwing out an error.
how exactly do the test.i
re: test
Add new comment