This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
i want to send a mail the code is given below. i cant understand waht is happening . It works on localhost but not on live server.
if (isset($_POST['test_mail'])){
$to = '2606ankit#gmail.com'; //put email address on which mail send
$subject = "Newsletter"; //Put subject of mail here
$from = 'ankit#studiokrew.com'; //put email address from
//email body start
// $body .= file_get_contents('file/'.$filename.'');
$body .= 'anio';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: '.$from. "\r\n";
//if you need to send cc mail then uncomment below line and change email address
//$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to,$subject,$body,$headers);
}
try this..
<?php if (isset($_POST['test_mail'])){
$host=$_SERVER['HTTP_HOST'];
$replyto="<no-reply >";
$to ='2606ankit#gmail.com';
$subject = "Newsletter";
$from = 'ankit#studiokrew.com';
$headers = "From: \"Invoice\"<noreply#$host>\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"abc"\"\r\n\r\n";
$headers .= 'From: '.$from. "\r\n";
$header .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
$body = "This is a multi-part message in MIME format.\r\n";
$body .= "Content-type:text/html; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= 'anio';
mail($to,$subject,$body,$headers);
}
?>
check the MTA logs on your server.also web server logs the answer will be there somewhere.
Test with a simple mail() script that does not require any external data, just to make sure that your server indeed can send mail. If that fails, you'd have to contact the server admins for help.
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 2 years ago.
I am using PHP's mail() function to send emails (sendmail process is running). But all the mails are going to spam (in case of gmail). I have tried many tricks that I found on the net but none is working, please tell me about any sure-shot trick.
You must to add a needle headers:
Sample code :
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
There is no sure shot trick. You need to explore the reasons why your mails are classified as spam. SpamAssassin hase a page describing Some Tips for Legitimate Senders to Avoid False Positives. See also Coding Horror: So You'd Like to Send Some Email (Through Code)
<?php
$subject = "this is a subject";
$message = "testing a message";
// declare variable
$headers = "Reply-To: The Sender <sender#domain.com>\r\n";
// add more info
$headers .= "Return-Path: The Sender <sender#domain.com>\r\n";
$headers .= "From: The Sender <sender#domain.com>\r\n";
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
mail("reciever#domain.com", $subject, $message, $headers);
?>
Try PHP Mailer library.
Or Send mail through SMTP filter it before sending it.
Also Try to give all details like FROM, return-path.
$fromMail = 'set your from mail';
$boundary = str_replace(" ", "", date('l jS \of F Y h i s A'));
$subjectMail = "New design submitted by " . $userDisplayName;
$contentHtml = '<div>Dear Admin<br /><br />The following design is submitted by '. $userName .'.<br /><br /><b>Click here</b> to check the design.</div>';
$contentHtml .= '<div><img src="'.$imageUrl.'" width="250" height="95" border="0" alt="my picture"></div>';
$contentHtml .= '<div>Name : '.$name.'<br />Description : '. $description .'</div>';
$headersMail = '';
$headersMail .= 'From: ' . $fromMail . "\r\n" . 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'Return-Path: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= "Content-Type: multipart/alternative; boundary = \"" . $boundary . "\"\r\n\r\n";
$headersMail .= '--' . $boundary . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headersMail .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$headersMail .= rtrim(chunk_split(base64_encode($contentHtml)));
try {
if (mail($toMail, $subjectMail, "", $headersMail)) {
$status = 'success';
$msg = 'Mail sent successfully.';
} else {
$status = 'failed';
$msg = 'Unable to send mail.';
}
} catch(Exception $e) {
$msg = $e->getMessage();
}
This works fine for me.It includes mail with image and a link and works for all sorts of mail ids. The clue is to use all the header perfectly.
If you are testing it from localhost, then set the below before checking:
How to set mail send from localhost xampp:
comment everything in D:/xampp/sendmail/sendmail.ini and mention the below under
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=yourmailid#domain.com
auth_password=your-mail-password
force_sender=yourmailid#domain.com
In D:/xampp/php/php.ini
a. Under
[mail function]
SMTP = smtp.gmail.com
smtp_port = 587
b. set sendmail_from = yourmailid#domain.com
c. uncomment sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"
Hence it should be look like below
sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"
d. comment sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"
Hence it should be look like below
;sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"
e. mail.add_x_header=Off
<?php
$to1 = 'test#gmail.com';
$subject = 'Tester subject';
// To send HTML mail, the Content-type header must be set
$headers .= "Reply-To: The Sender <sender#sender.com>\r\n";
$headers .= "Return-Path: The Sender <sender#sender.com>\r\n";
$headers .= "From: sender#sender.com" ."\r\n" .
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
?>
I have a website contact form that sends HTML email with photo attachments as an email to Iphone mail application. I am receiving the following error message on my Iphone:
"This message cannot be displayed because of the way it is formatted. Ask the sender to send it again using a different format or email program. multipart/mixed".
Is this has something to do with email content-type and how to fix it in my PHP code.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message_body));
$sentMail = mail($recipient_email, $subject, $body, $headers);
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using this code for my contact form in a html website but mail is not coming in Gmail Inbox.
Can any one help me i am trying to solve this issue but i don't have any guide.
<?php
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "iamuser#gmail.com"; //Recipient's E-mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];
if (#mail($to, $subject, $message, $headers))
{
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
// header('Location: ../index.html');
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
There are some problem in setting the header.
And most important is that you need to define the correct and valid Email Id in the From section because google generally used to validate the domain, the mail coming from.
If it is not white-listed at google end then it wills end the mail to Spam automatically, that is happening now as I think.
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are
You can try the below code.
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
Reference Link.
https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer
There is another code you can try:
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain;charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n";
// use different content types here$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK";
// or use booleans here} else {echo "mail send ... ERROR!";
}
}
If you are developing this program in local server. The emails will not be sent to your gmail account.
If you want to test your code on a local machine please install Test Mail Server Tool.
No email will be delivered while running on local machine but you will get an idea how the email will look like.
When you run the same on web hosting server, the email will be delivered to the email id specified in $to field.
I am using a PHP script to send e-mails to a list of friends. This used to work successfully but now the only e-mails that are sent successfully are those inside my domain ("mycompany.com"). Any e-mails outside my domain fail to be sent. Is this a problem with permissions? Could I fix it by changing the headers I use when I call the mail() function? Here is the PHP script:
$name = "Joe Smith";
$email = "jsmith#mycompany.com";
$to = "ajones#mycompany.com, tom#abc.com, ted#def.com, mary#ghi.com";
$subject = "Dinner Invitations";
$message = "Invitation to Dinner";
$headers = "From: ".$name."<".$email.">\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "Return-Path: ".$email."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n";
mail($to, $subject, $message, $headers);
Only ajones#mycompany.com was sent out by the server. tom#abc.com, ted#def.com, and mary#ghi.com did not get sent.
Any suggestions would be deeply appreciated!
I am trying to dynamically send text messages using a PHP script. PHP code:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$textbody=<<<_MESSAGE_
Some text
_MESSAGE_;
mail('myphonenumber#SMSgateway','subject',$textbody,$headers);
I did receive a text message, but it is a "photo message" or rather multimedia instead of text and I am unable to open the message. I have tried playing around with the encoding and $textbody="this text"; instead of *MESSAGE*.
a) How can I send a normal text message (not multimedia)?
b) Why can't I open it?
c) Is there a way for people to respond to the texts I send with text? When I sent myself a text from hotmail I was able to reply and I got the answer in my inbox. When I tried to put $header.= 'From: me <me#somedomain.com>' . "\r\n"; the email wouldn't send
(reason: 553 sorry, your mail was
administratively denied. (#5.7.1))
Thanks!
$sendTo = "test#test.com";
$subject = trim($_POST['subj']);
$headers = "From: ".$_POST['name']."<" . trim($_POST["email"]) .">\r\n";
$headers .= "Reply-To: " . trim($_POST["email"]) . "\r\n";
$headers .= "Return-path: " . trim($_POST["email"]);
$headers .= "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$message = strip_tags($_POST["messg"]);
if (#mail($sendTo, $subject, $message, $headers))
{ echo "sent successfully"; }
else
{ echo "Error ... Plz try again later" ; }
This code which I'm using to send emails
I worked before on SMS project so if you have any question about how to link with Getaway feel free to contact me