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");
Related
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;
}
?>
I have a website with an SSL.
I would like to encrypt outgoing emails from my server. I've been digging around at this and I really don't know where to begin.
Here is my PHP email script so you have an idea of what I'm using:
public function email($to, $title, $message){
$from = "angela#mysite.com";
$headers = "From: {$from}\r\n";
$headers .= "X-Confirm-Reading-To: {$from}\r\n";
$headers .= "Reply-To: {$from}\r\n";
$headers .= "Organization: InfiniSys, inc.\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$subject = $title;
mail($to, $subject, $message, $headers);
}
Ubuntu 14.04
I'm not sure if this is a server setting or programming config.
Very interesting post: (can't remember where I got it)
<?php
// Setup mail headers.
$headers = array("From" => "someone#example.com",
"To" => "someone-else#example.com",
"Cc" => "spam#somewhere.org",
"Subject" => "Encrypted mail readable with most clients",
"X-Mailer" => "PHP/".phpversion()
);
// Get the public key certificate.
$pubkey = file_get_contents("C:\test.cer");
// Remove some double headers for mail()
$headers_msg = $headers;
unset($headers_msg['To'], $headers_msg['Subject']);
$data = <<
This email is Encrypted!
You must have my certificate to view this email!
Me
EOD;
//write msg to disk
$fp = fopen("C:\msg.txt", "w");
fwrite($fp, $data);
fclose($fp);
// Encrypt message
openssl_pkcs7_encrypt("C:\msg.txt","C:\enc.txt",$pubkey,$headers_msg,PKCS7_TEXT,1);
// Seperate headers and body for mail()
$data = file_get_contents("C:\enc.txt");
$parts = explode("\n\n", $data, 2);
// Send mail
mail($headers['To'], $headers['Subject'], $parts[1], $parts[0]);
// Remove encrypted message (not fot debugging)
//unlink("C:\msg.txt");
//unlink("C:\enc.txt");
?>
Try Using PHPMAILER It's really Easy
You can find all Username and password from your Cpanel Email Account option
$to= "example#gmail.com";
require 'phpmailerlibrary/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#example.com'; // SMTP username
$mail->Password = '*****'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('support#twekr.com', 'example Inc.');
$mail->addAddress($to); // Add a recipient
$mail->addReplyTo('support#example.com', 'Support');
$mail->addCC($to);
$mail->addBCC($to);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Subject of Email';
$mail->Body = 'Content of your html email';
$mail->AltBody = 'Please Upgrade Your Browser to view this email';
if(!$mail->send()) {
echo "Unable to send email"; exit;
}
You should Use TLS as it is also encryption method 90% website uses including google too.
Check out this post where a user suggests to use PHP Mailer.
You can use phpmailer to send outgoing messages through gmail's SMTP server (smtp.gmail.com), and it has options to connect to the SMTP server by SSL. phpmailer is very simple to setup - just a few PHP files to copy to your server.
Here is a great tutorial
If you don't want to use a third library, you'll need to communicate with a SMTP server using socket and send all the commands manually.
Check out this RFC to know how the protocol works
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/
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I wish to know the difference between mail sending using localhost and live server.
This is my code and someone help to correct this code so that mail along with attachment can be sent successfully.it showed mail sent but I did not receive any.I am not sure where the error or code missing happens.Help me so that I can rectify it.
//This is m1.php file
<?php
$max_no_img=1; // Maximum number of images value to be set here
echo "<form method=post action=m2.php enctype='multipart/form-data'>";
echo "<tr><td>Photo: </td><td><input type='file' name='uploadfile' class='bginput'></td></tr>";
?>
<input type="submit" name="Submit" value="Send">
//This is m2.php file.From m1.php link redirects to here and the mail with attachment to be done here
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "mymail#gmail.com"; // SMTP account username
$mail->Password = "password";
if($_POST){
$targetFolder = "image/".$_FILES['uploadfile']['name']; //My location where images stored & retrieved
copy($_FILES['uploadfile']['tmp_name'],$targetFolder);
$htmlbody = " Your Mail Content Here.... You can use html tags here...";
$to = "mymail#gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: mymail#gmail.com\r\nReply-To: mymail#gmail.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents($targetFolder)));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip;
name=\"$targetFolder\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
}
?>
mail() will return value if your mail sent successfully.
Try this.
$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
if($status) {
echo 'Mail sent successfully';
} else {
echo 'Mail sending failed';
}
The reason that you are not receiving the email is likely that you have not configured your localhost machine to run an SMTP server.
To test emails on localhost I use Antix SMTP imposter. It sits on your machine and shows emails that would have been sent had your app been connected to an SMTP server. This has the added bonus of allowing you to see the emails as they would be received without having to worry about sending live emails from your test environment.
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.