PHP and SMTP server - php

My simple code below:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: info#mail.com \r\n";
$headers .= 'Bcc: test#mail.com' . "\r\n";
$subject = "Information";
if (mail($email, $subject, $message, $headers)) {
$mail_status = "success";
} else {
$mail_status = "fail";
}
if ($mail_status == "success") echo '{"status":"success"}';
How can I support SMTP support?

As You mentioned PHPMailer in tags. You can use PHPMailer Class for this process.
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional,
//gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Source: http://phpmailer.worxware.com/index.php?pg=examplebsmtp

SMTP server has to be configured on server, but by PHP, which is a programming language. Ask Server admin to provide you SMTP details.
SMTP information can be set in php.ini or via ini_set()
Example:
ini_set("SMTP","smtp.example.com" );
ini_set('sendmail_from', 'user#example.com');
By the way, if you require SMTP Authentication, mail() does not support it. Try to use other PHP mail library such as SwiftMailer or PHPMailer.

Related

Can not send email from **host-gator** using php mail function code [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I trying to send an email from php using php mail() function.
Each time when I try to send an email it displays the error message.I don't know what went wrong.Here is my code. Can anyone tell me what went wrong ?
$message = "<html><body>";
$message .= "<table rules='all'>";
$message .= "<tr><td><strong>Name: Ramya</strong> </td></tr>";
$message .= "<tr><td><strong>Email: ramyaroy</strong> </td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = 'ramya#example.com';
$email='vinay#example.net';
$subject = 'Website Change Reqest';
$headers = "From:".$email.PHP_EOL;
$headers .= "Reply-To:".$email.PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "Content-Type: text/html; charset=ISO-8859-1".PHP_EOL;
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
There's no error in the code.
The problem could be because of your hosting. Usually, hosting account will allow to send a mail from only that domain account.
Change the $email to vinay#[your_domain_name].com
Let me know after you try this.
<?php
/*
Below code works on live and local both server , also on SSL
which I describe all options on comments
you need to set SMTP server username and password ,
this is same as your google email id and password
*/
/* You need to download php-mailer class
https://github.com/PHPMailer/PHPMailer */
define('SMTP_USERNAME','your smtp username');
define('SMTP_PASSWORD','password');
define('FROM_EMAIL','from email');
define('ADMIN_EMAIL','replay to email');
define('SITE_NM','your site name');
print_r(sendEmail('john.doe#gmail.com','Lorem Ipsum','anything....'));
function sendEmail($to, $subject, $message) {
require_once("class.phpmailer.php");
// create a new object
$mail = new PHPMailer();
// enable SMTP
$mail->IsSMTP();
// debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPDebug = 0;
// authentication enabled
$mail->SMTPAuth = true;
//mail via gmail
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
//$mail->Host = "smtp.gmail.com";
//$mail->Port = 465; // or 587
//mail via hosting server
$mail->Host = SMTP_HOST;
if (SMTP_HOST == 'smtp.gmail.com') {
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 465; // or 58
}else{
$mail->Port = 587; // or 58
}
$mail->IsHTML(true);
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
//$mail->SetFrom(SMTP_USERNAME);
$mail->SetFrom(FROM_EMAIL);
$mail->AddReplyTo(ADMIN_EMAIL, SITE_NM);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($to);
// to add bcc mail list
$mail->AddBCC("example#gmail.com", "your name");
$mail->AddBCC("example2#gmail.com", "XXX XXXXX");
$result = true;
if (!$mail->Send()) {
//echo "Mailer Error: " . $mail->ErrorInfo;
$result = false;
}
return $result;
}
?>

Getting a warning message after opening email using PHP

I am using PHPMailer for sending an email. Emails are getting proper but whenever I am opening email which I was sent using PHP Mailer I am getting a warning message.
Note: If I remove anchor tag from $phpMailerText then I am not getting any warning.If I add anchor tag then I am getting warning.Would you help me in this?
require 'mail/PHPMailerAutoload.php';
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: smtp.gmail.com" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:abc#gmail.com\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $to\n"
. "To: $to\n"
. "From: $to\n";
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "abc#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "****";
//Set who the message is to be sent from
$mail->setFrom('abc#gmail.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('abc#gmail.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = ' ';
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}
Try the following code and do the other steps.
Create reverse dns record
Configure SPF records
$to = $email;
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
// Headers
$headers = "Content-Type: text/plain; charset=\"utf-8\"\n"
. "X-mailer: YOUR_SITE_DOMAIN Server" . "\r\n" // this will identify the real sender
. "Precedence: bulk" . "\r\n" // this will say it is bulk sender
. "List-Unsubscribe:info#YOUR_SITE_DOMAIN\r\n" // this will reveal the OPT-OUT address
. "Reply-To: $email\n"
. "To: $email\n"
. "From: $email\n";
$mail->addCustomHeader( $headers );
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "me#companydomain.com";
//Password to use for SMTP authentication
$mail->Password = "****";
// Because html is being used
$mail->isHTML(true);
//Set who the message is to be sent from
$mail->setFrom('me#companydomain.com', 'code');
//Set an alternative reply-to address
$mail->addReplyTo('me#companydomain.com', 'code');
//Set who the message is to be sent to
$mail->addAddress($to, 'Customer');
//Set the subject line
$mail->Subject = 'code';
$phpMailerText="<!DOCTYPE HTML><html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='http://www.companyname.com/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
$mail->msgHTML($phpMailerText);
//Replace the plain text body with one created manually
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else {
}
Please use following code and tell me what happends.
$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("email#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}

php mail function not working reply SMTP server response: 503

I try to send mail using php mail function. but I getting error like:
PHP Warning: mail(): SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server. in C:\Inetpub\vhosts\qubedns.co.in\httpdocs\Codes\design\rnp\mailsend.php on line 21
Here is my PHP SCRIPT:
<?php
$toEmail = 'bikash336#gmail.com';
$subject = 'hello';
$message = 'Users are able to send emails to local domains and addresses but are unable to send email to any external address.';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <sales#leonwallet.com>' . "\r\n";
$Status = mail($toEmail,$subject,$message,$headers);
if($Status){
echo '1';
}else{
echo '0';
}
?>
Here is my Server configuration: http://qubedns.co.in/codes/php/
What's wrong with me?
I have found the solution with PHPMailer.
Its work for me.
<?php
require('mailserver/PHPMailerAutoload.php');
require('mailserver/class.phpmailer.php');
require('mailserver/class.smtp.php');
require('mailserver/class.pop3.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Username = 'sales#leonwallet.com';
$mail->Password = 'password';
$mail->From = 'sales#leonwallet.com';
$mail->FromName = 'Leon Sales Team';
$mail->AddAddress($to);
$mail->AddReplyTo('sales#leonwallet.com', 'Leon Sales Team');
$mail->Subject = $sub;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $message;
if(!$mail->Send()){
echo "1"; // ERROR
}else{
echo "0"; // SUCCESS
}
?>
Here is a solution. For me he solved the problem
When I changed the environment, the TO parameter was configured with the domain of my test / development project.
I changed the TO email to the production email and the error disappeared.
Here is the solution: http://www.marathon-studios.com/blog/php-mail-503-error/

How to configure mail().php, so I can use it with the gmail SMTP

define('SITE_EMAIL', 'example#mydomain.la');
$to1= SITE_EMAIL;
$subject1 = "Contacto Web";
$message1 = 'Hi!';
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers1 .= "From: ".$_POST['name']." <".$_POST['email'].">\r\n";
mail($to1, $subject1, $message1, $headers1);
print "message send!";
I'm using this code to send an email from a contact form. But mostly goes to spam or even the mail is not sent. What they recommended was to validate the header. The form and the php file is in my domain but I use google apps, so I think I have to use the google smtp. But I really don't know how...
I don't think there's an easy way to use mail() with gmail.
Give phpmailer a try.
This is a sample of using it with gmail:
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

php form send email

I know my php form works here on godaddy server:
http://thespanishlanguageacademy.net/los-angeles/learn-spanish-kids-children/kontact.html
Please test it yourself put your email address in and it will send you a copy.
I copy the same code into a different server. This server is not go daddy. I know php works on this server, but for some reason this form is not working:
http://hancockcollege.us/kontact.html
Here is the php code:
// if the Email_Confirmation field is empty
if(isset($_POST['Email_Confirmation']) && $_POST['Email_Confirmation'] == ''){
// put your email address here scott.langley.ngfa#statefarm.com, slangleys#yahoo.com
$youremail = 'bomandty#gmail.com';
// prepare a "pretty" version of the message
$body .= "Thank You for contacting us! We will get back with you soon.";
$body .= "\n";
$body .= "\n";
foreach ($_POST as $Field=>$Value) {
$body .= "$Field: $Value\n";
$body .= "\n";
}
$CCUser = $_POST['EmailAddress'];
// Use the submitters email if they supplied one
// (and it isn't trying to hack your form).
// Otherwise send from your email address.
if( $_POST['EmailAddress'] && !preg_match( "/[\r\n]/", $_POST['EmailAddress']) ) {
$headers = "From: $_POST[EmailAddress]";
} else {
$headers = "From: $youremail";
}
// finally, send the message
mail($youremail, 'Form request', $body, $headers, $CCUser );
}
// otherwise, let the spammer think that they got their message through
First,
Use this headers:
<?php
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
//Fom
$headers .= "From: XXX XXXX XXXXX <xxxx#xxxx.xxx>\r\n";
//Reply
$headers .= "Reply-To: example#example.com\r\n";
//Path
$headers .= "Return-path: example#example.com\r\n";
//CC
$headers .= "Cc: example#example.com\r\n";
//BBC
$headers .= "Bcc: example#example.com,example#example.com\r\n";
?>
Two, Read about PHPMailer and see the next code:
And try with this:
<?php
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "yourname#example.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.gif');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Maybe you have to initialize manually in the other server the SMTP path, i had a similar problem time ago, setting the correct SMTP fixed my problem.
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "yourmeail#yourdomain.com");

Categories