I am sure most of you have already done with kind of thing but i am unable to use correct charset for subject with html format on this code for phpmailer somehow eventhough i've tried hundereds of way. Could you guys catch what could be wrong with the code below?
If i replace "Content-Type: text/html" to "Content-Type: text/plain" then utf8 works fine for only message body but not for subject still gets bad header warning besides mail does not seem as html format either. And what should i write to use an external mail server into this code ?
$message = "Hi $uname,\r\n\r\n\r\n";
$message .= "$passwordLink\r\n\r\n\r\n";
$message .= "you just hit password recovery request\r\n;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Password Service <service#domain.com> \n";
$headers .= "To-Sender: \n";
$headers .= "X-Mailer: PHP\n";
$headers .= "Reply-To: noreply#domain.com\n";
$headers .= "Return-Path: noreply#domain.com\n";
$headers .= "Content-Type: text/html; charset=utf-8";
$host = "mailgw.domain.com";
$subject = "Pass reminder service";
#mail($email,$subject,$message,$headers);
return str_replace("\r\n","<br/>",$message);
Thanks in advance,
Pass your $message and $subject through this function http://php.net/manual/en/function.utf8-encode.php.
To use external server I would recommend to use PHPMailer class https://github.com/PHPMailer/PHPMailer, so you could do something like this:
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
...
Related
I am a bit confused with setting up Sparkpost SMTP. I need to send automatic email from my website to clients, for eg. a welcome message when they register, a reminder email for consultation etc. The website will automatically generate the email.
On Sparkpost they show:
define('PHPMAILERHOST', 'smtp.sparkpostmail.com');
$phpmailer_smtpuser = 'SMTP_Injection';
$phpmailer_smtppassword = '<API_KEY>';
define('PHPMAILERPORT', 587);
However when I searched for more information on Stackoverflow, I found this:
$config['mailtype'] = "html";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.sparkpostmail.com';
$config['smtp_user'] = 'user';
$config['smtp_pass'] = 'password';
$config['smtp_crypto'] = 'tls';
$config['smtp_port'] = '587';
$condig['crlf'] = "\r\n";
$config['newline'] = "\r\n";
In light of the above, will the following work:
//Sparkpost configuration
$config['mailtype'] = "html";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.sparkpostmail.com';
$config['smtp_user'] = 'user';
$config['smtp_pass'] = 'password';
$config['smtp_crypto'] = 'tls';
$config['smtp_port'] = '587';
$condig['crlf'] = "\r\n";
$config['newline'] = "\r\n";
//My email code
$to_email = "$Email";
$from_email = "me#mydomain";
$subject = "Email Subject";
$comment = "<html>Email message</html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= "From: Info <" . $from_email . ">\r\n";
//send email
mail($to_email, "$subject", $comment, $headers);
The reason I was asking, I don't want to setup, test and emails go through and while I am under the impression that my Sparkpost setup is working, the email goes through my host.
If you want to use SparkPost you can either use their library or, as suggested by Jon Stirling, use PHPMailer with SparkPost configurations.
Use SparkPost PHP Library
Read installation instructions here
Once installed, see the examples directory for many use cases
Use PHPMailer
Include phpMailer class in your script
Configure it to use SparkPost like below
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.sparkpostmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'SMTP_Injection';
$mail->Password = '<API_KEY>'; //make sure you add SMTP permission to API Key
$mail->setFrom('testing#sparkpostbox.com'); //you can use w/o adding your sending domain like ~50 messages. Once you add your sending domain, use that.
$mail->addAddress('recipient#domain.com');
$mail->Subject = 'Test subject';
$mail->Body = 'Hello World!';
$mail->send();
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/
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");
Hello I am learning php where i came to know mail() function i have tried this code
function sendMail()
{
$to = 'Sohil Desai<sohildesaixxxx#gmail.com>';
$from = 'Sohil Desai<sohildesaixxxx#hotmail.com>';
$subject = 'Test Mail';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
$message = 'This mail is sent for testing.';
$mail = mail($to, $subject, $message, $headers);
if (!$mail) {
return 'Error occured sending mail.';
}
return 'Mail successfully sent.';
}
echo sendmail();
I have tested only for gmail, ymail and hotmail.
This function sends mail in a spam for gmail & hotmail and it won't send mail to ymail.
why it happens??
I am using Ubuntu 12.04 & php version 5.3.10.
Can anyone help me?? Thanks to halpers in advance..
Try adding "to" as a header:
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
More details can be found at http://www.php.net/manual/en/function.mail.php (example #4).
Verry simple
$to = 'someone#example.com';
$subject = 'the subject';
$message = 'the message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Hope helps you!
Source: http://w3webtools.com/send-email-using-php/
Instead of using build in crappy email() function, make a use of PHPMailer.
It is easy to use and does most of the hard work for You.
Features:
Probably the world's most popular code for sending email from PHP!
Used by many open-source projects: WordPress, Drupal, 1CRM, SugarCRM,
Yii, Joomla! and many more Integrated SMTP support - send without a local mail server Send emails with multiple TOs, CCs, BCCs and
REPLY-TOs Multipart/alternative emails for mail clients that do not read HTML email Support for UTF-8 content and 8bit, base64, binary,
and quoted-printable encodings SMTP
authentication with LOGIN, PLAIN, NTLM, CRAM-MD5 and Google's
XOAUTH2 mechanisms over SSL and TLS transports Error messages in
47 languages!
DKIM and S/MIME signing support Compatible with PHP
5.0 and later Much more!
All you have to do is to download and include this class to your project and then use it as in example:
A Simple Example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You'll find plenty more to play with in the examples folder at https://github.com/PHPMailer/PHPMailer.