Advance PHP mail script - php

I have created an php mail script, And in the message of the mail i sent i am using many variables (data). For example i want to sent an mail with this body msg:
Name: Somename
Email: someemail#somwhere.com
City: Somecity
State: somestate
.........
What i am doing is this:
$msg = "Name: $name (brake) Email: $email (brake)......"
this message is not working in major emails like gmail, hotmail, yahoo...
I get the mail with html tags and i dont want that.
There must be some other way to do this so my mail structure looks good on every email account ?

Make sure in the header declarations you are declaring it as an HTML e-mail.
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"
http://php.net/manual/en/function.mail.php should help out as well. Follow the example code in there and it's guaranteed to work.

I would recommend the PHP email packages available through PEAR:
PEAR Mail - for interfacing with the server/smtp mailer and performing the action of sending
PEAR Mail_Mime - Which handles the formatting for sending of plaintext or HTML email.
Check out the documentation for usage.

Well there is the possibility that you aren't setting the header to indicate that the type is HTML. But why would you want to roll your own mail sender. There is a great email sender for php: http://sourceforge.net/projects/phpmailer/ and it is free.

Related

Mail ends up in junk folder - PHP contact form [duplicate]

I wrote a PHP script to send emails.
My script is like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: abc#yahoo.com' . "\r\n";
// Email Variables
$toUser = "someone#yahoo.com"; // recipient
$subject = "testing"; // subject
$body = "<html><body><p>
Example of including an image via html \<img\> tag:
<br>
<img src='../images/profile.jpg'>
<br>
My new picture
<br></p></body></html>"; // content
if (mail($toUser,$subject,$body,$headers)) {
echo "sent";
} else {
echo "failed";
}
Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?
Please try this:
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
Perhaps the problem is that yahoo uses domainkeys verification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.
When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.
For example: SpamAssassin score list
1. Check mail content
As others have hinted it is probably marked as spam because your mail looks like spam.
I am not sure if you the script that you have posted is the actual one that you are testing.
If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9
X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
version=3.2.5
Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.
2. Mail server's IP
Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.
3. Blocklists
Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.
Use mxtoolbox.com to check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.
Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.
Hope that helps!
**This Works Perfectly fine for me**
$to="reciever#reciever.com";
$subject="This is Your Message";
$from = 'Sender <noreply#sender.com>';
$body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
$headers = "From: " .($from) . "\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($from) . "\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";
mail($to,$subject,$body,$headers);
I was having the same problem:
The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.
But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.
As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.
So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.
Another one is the html contents without the alternative plain text contents, but, this one is less important.
I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)
On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
Make sure you send your e-mails from an existing account on your server/domain.
Make sure you have the reply-to address in your header.
These are the basic things you can try.
if your website domain is mydomain.com then in From headers make sure to use someone#mydomain.com
Remove the Content-type: text/html and add $headers .= "X-Priority: 2\nX-MSmail-Priority: high"; to get rid of Spam. This method has been tried and tested.
the problem is, the server you're sending the mail from is not a yahoo server. most spam filters check if they match, otherwise it would (and is - or was) possible to easily fake the sender. ever wondered why you get spam from bill.gates AT microsoft.com or your own mail address?
You've got two solutions:
use Yahoo's SMTP using abc#yahoo.com credentials to send mail from abc#yahoo.com;
use other from, with your own domain;
You can try the mail class and test file that I have created here. I have tested the files and can send emails to my hotmail and gmail under a different mail name. The main reason why the emails are mark as junk is because the structure (both header and message) is not correctly done. In most cases, it is the line feed that is causing the problem.
I can use it to send mail with attachments to Gmail. However, the attachments dont work for hotmail. Hope this helps =)
You can check the files here..

How to remove 'via' email address while sending mails using php [duplicate]

When I send a mail with PHP the destinatary gets a header like this one:
noreply#justwalk.it **via** de p3nlhg147.shr.prod.phx3.secureserver.net
I want to remove the "via" part. Most automated mails from websites don't have the "via" so it's certainly possible to remove it.
How do they do it?
Yes, you can get rid the "via" part. Here's the details:
1) SPF and DKIM
Firstly, you would need to set an SPF record for the domain you are sending emails from and enable DKIM as well. These are primarily for identifying your messages against spam.
2) "From: anything#yourdomain.com"
Secondly, make sure you are setting the “From: ” header to be an email address on the domain you are sending messages from. Don’t pretend to be someone else. Use “From: someone#abc.com” if you are sending the messages from abc.com, rather than anything else, such as blah#def.com, or yours#gmail.com, or whatever. If you want the recipient to reply to your Gmail email instead of your domain email, use the “Reply-To: ” header. “From: ” must always be the domain email that you are sending the email from.
3) "Return-Path: return#yourdomain.com"
Thirdly and most importantly, set the “Return-Path: ” header to be the same domain as that of the “From: ” header. Use the 5th parameter of the mail() function for this:
mail('recipient#example.com', 'Subject', "Message Body", $headers, '-freturn#yourdomain.com')
So the Return-Path of this message would be “return#yourdomain.com” (the email address immediately following the -f switch). The $headers parameter should contain all the necessary message headers. Make sure “From: ” is something#yourdomain.com.
After these steps and measures, Gmail should now completely trust your messages from yourdomain.com. The ‘via‘ field of your messages should be gone and the ‘mailed-by‘ field as well as the ‘signed-by‘ field should be correctly showing up as yourdomain.com.
Hope it helps!
I also fetched the same problem. But I have overcome the problem by using the following code:
mail('maaaa#abcd.com', 'the subject', 'the message', null,'-faaa#abc.com');
Make sure that last parameter is -f with the email address.
You can add the
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";<br />
mail('maaaa#abc.com', 'the subject', 'the message body in html format', $headers,'-faaaa#abc.com');
for the html message body in email.
This is probably added by your MTA and you didn't say which MTA you are using.
I'd recommend sending the mails not by PHP's mail() function but via SMTP, possibly even with SMTP-Auth, using something like PHPMailer.
#Mujibur is also right, But I used. But Didn't missed Headers too.
mail($to, $subject, $message, $headers, '-f'.$from_email_address);
And its successful to me, Let's check it from your side.
See what Google says about this here: http://support.google.com/mail/bin/answer.py?hl=en&ctx=mail&answer=1311182
All the best!

One email to forward to multiple email addresses

I am looking to create one email (example#domain.com) to forward to all email addresses in a database.
People can email to example#domain.com, and then that email would get blasted to a list of predefined email addresses. It would need to include support for attachments.
I realize this is not secure at all and leaves this email address open to anybody to use, but this is what our organization wishes to do.
What would be the best way to do this on a PHP server?
Thanks.
This can be achieved in PHP Server as you have to go in depth of following things:
Email Piping
Email Headers
MIME Email attachments
Mailing List Management
While emailing to a lot of people in the way you mentioned is not a good idea...
You can use PHP's "mail" function:
$to = "user1#domain.com, user2#domain.com"; //(Comma separated list of emails)
$from = "noreply#domain.com";
$subject = "Hello";
$message = "Hello there!";
$headers = "From: ". $from . "\r\n".
"Reply-To: ".$from . "\r\n";
//send it
mail($to, $subject, $message, $headers);
while($row = mysql_fetch_array($emails))
{
$addresses[] = $row['address'];
}
$to = implode(", ", $addresses);
And then send mail using mail function ... mail($to, $subject, $message, $headers);
The way you have worded your query is a bit confusing but it seems you have a hosting server that is going to receive emails to a domain email. You then want to have those emails autoforwarded to a list of recipients. It looks like you want to investigate (given you mention sendmail) linux mail services and autoforwarding. You may be looking at postfix and possibly using procmailrc to trap and redirect incoming mail on your server.
I have done this with procmailrc before but it really depends on what service is handling the incoming mail.
There is no problem calling a CLI PHP script as suggested by #George to then read your recipients from the database and do the sending.
While Arthur's answer made sense and most likely would work, I ended up finding that my host had the feature I was looking for buried deep inside it. For those who were wondering, it is called discussion lists.

Sending mail on PHP using LAMP

I have been trying for awhile now to send mail using PHP, and I can't seem to get it to work.
I looked at the tutorials, and they make it seem simple enough. Yet, it seems to actually require more work than what is mentioned. Through research (to make this short), it seems that instead of the "From: " header actually having the address I give to it, it seems that... sendmail replaces it or something with daemon#ubuntu? Ok, even if I don't include a header I still get spam from my gmail account from the daemon#ubuntu thing (I am running ubuntu on a vm, and sending mail to my gmail account). I guess this could explain why I incessantly get errors when I try to send mail saying how there is an error in the "MAIL FROM command".
So before I get myself involved with messing around with sendmail, is this the (likely)culprit of the daemon#ubuntu nonsense? The tutorials make mention of having things like sendmail, but they really don't go into any details of what to do with them. I did make sure that sendmail is being used in my php configuration file.
I have heard of alternative mail servers like PHPmail or something like that. I really don't want to get involved in installing and configuring more software unless I really have to. So unless you guys really think sendmail isn't worth it, I would prefer advice on sendmail if it is the problem.
Thank you for your time.
edit: I am using the PHP mail() function on an Ubuntu vm by VMware Player.
So someone wanted to see my code. I somewhat wanted to avoid this simply because I tried many different possibilities. I will just include what I have in my file right now:
<?php
$myEmail = "#####gmail.com";
$fromAddr = $_POST['sender-email'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $fromAddr\r\n";
$headers .= "'X-Mailer: PHP/'" . phpversion() . "\r\n";
$message = $_POST['message'];
$status = mail($myEmail, $_POST['subj'], $message);
if($status) {
echo "Your message has been sent, " .$fromAddr. "!";
}
else {
echo "An error occurred while trying to send the mail.";
}
?>
The MIME type thing, and Content-type thing I added later on. I have tried this without them. I have also tried sending mail without using any headers at all, and still apparently get the daemon#ubuntu thing. Also, I edited out my e-mail address to post the code on here. Ok I guess I should add this too: I am getting the data from an HTML form from another page. On submit, I come to this php page where it grabs that data and then sends the email (this isn't for anything professional, I am just doing this to learn more about web stuff).
Postfix is a very easy to use and install email server, in my experience. You need an email server installed and configured before you're able to send email on your localhost.

Basic phpmailer question

I am using phpmailer to send my newsletter to my subscribers.
I set my headers as following:
$headers = "From: Sales - Blah Blah <sales#blahblah.com>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
NOW, in the delivered email, for example in a gmail account, if you press the "show details" link, the following appears.
from Sales - Blah Blah MIME-Version: 1.0
sales#blahblah.com via ecbiz103.inmotionhosting.com
to blahblah#gmail.com
date Wed, Aug 3, 2011 at 11:02 AM
subject sales !
***mailed-by ecbiz103.inmotionhosting.com***
HERE IS THE QUESTION:
HOW CAN I MAKE IT ACTUALLY SEND THE EMAIL FROM THE ACTUAL EMAIL ACCOUNT, that is "sales#blahblah.com" and be able to see the outgoing emails on my "sent" tab?
I guess, when this happens, the last line mentioned in bold would say
"mailed by blahblah.com"
Regards, George
PHPmailer as in http://phpmailer.worxware.com? Or do you mean PHP's built-in mail() function? It would appear you're using mail(), since you're building your own MIME message. Don't do that. It's too much main and unreliable. use PHPmailer (at the link mentioned earlier) or Swiftmailer to do that sort of thing for you.
Either way, if you want the mails you send to show up in your account's "sent" folder, you'd have to send the emails via your own email account using blahblah.com's mail server. Another option would be to set a BCC: to silently copy yourself on each mail, and have a mail rule on your account to divert those mails to a folder.
The simple answer is: you cant.
All phpmailer does is connect to the mail server to send the mail. It does not link to your actual e-mail account in any way at all.
EDIT:
Also, you will not be able to remove the ecbiz103.inmotionhosting.com reference as that is the hostname of the server that is sending the mail. A mail server runs on one IP address, this IP has a hostname, in this case ecbiz103.inmotionhosting.com.
You don't have a newline after the first line of headers, specifically the From header.

Categories