php email and smtp [duplicate] - php

This question already has answers here:
Sending email with PHP from an SMTP server
(9 answers)
Closed 4 years ago.
i've got a php function which sends emails fine but I want it to use the smtp server i have specified in the ini.set function.
if i delete these 3 lines of code the email still sends fine.
the reason i want to use the smtp server is because sometimes this does not work to certain mail hosts.
connect();
$username = mysql_real_escape_string($_POST['username']);
$result = mysql_query("SELECT email FROM members WHERE username = '$_POST[username]'")
or die ("Error - Something went wrong.");
$row = mysql_fetch_array($result);
$useremail = $row['email'];
$name = "name";
$mail_from = "noreply#name.com";
$mail_to = "$useremail";
$mail_body = "Hello. Thank you for registering. Please click the link below to confirm your email address. ";
$mail_subject = "Confirm you registration".$name;
$mail_header = "From: ".$name." <".$mail_from.">\r\n";
ini_set ("SMTP", "ssl://serveraddress");
ini_set("smtp_port","465");
ini_set("sendmail_from","noreply#name.com");
$sendmail = mail($mail_to, $mail_subject, $mail_body, $mail_header);
if($sendmail == true) { mail("john#name.com", "email sent ok", "email sent to '$_POST[email]'", "yea sent ok");}
Else { mail("john#name.com", "email ERROR", "email not sent to '$_POST[email]'", "yea we got a problem");}
}

If you are serious about sending mass mailing in your web application, i'd look at the SwiftMailer library, its free and relatively easy to setup and you can set your own SMTP transporter live.
Else than that, i have had issues with sending mail to hotmail and yahoo before where the email just doesn't get there when you use the "mail()" function. Keep in mind that the web email clients are very picky about headers, structure and security and may refuse emails not written by real clients easily.
If you want to test out something like that, just use a local-address#your-domain email, this will usually not filter it out. Now if the mail doesn't get there, you know it's something else.

Related

PHP mail() will not send a mail to an Outlook Distribution List [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
How do I prevent mails sent through PHP mail() from going to spam? [duplicate]
(6 answers)
Closed 3 years ago.
So a company I am helping out has outlook for their mail. Unfortunately, outlook seems to be very inflexible on how I can set up emails. I have added multiple domains to the outlook admin center for our multiple different sites. However, it will not let me create anything useful with the new domains.
For instance, we have "information#ourMainDomain.com" set up as an account.
I tried to create an alias on this account for "information#aSecondaryDomain.com" but then outlook gives me an error saying "information#ourMainDomain.com" already exists. Evidently you cannot use the same username even with different domains.
Fine. So i tried to create a shared mailbox - same issue. information#aSecondaryDomain.com is not allowed, because information#ourMainDomain.com exists... getting frustrating now!
So, after experimenting, trying everything, and spending 2 hours on the phone with outlook support that got me no where, i opted for my own route:
I created a Distribution Group called information#aSecondaryDomain.com, told this that senders outside the organization can send to it, added all the people that need to be involved, and gave them all send access.
This allows us to send emails as info#aSecondaryDomain.com
It allows us to recieve them here as well.
Also, i can send from outside the organization, ie from my gmail i can send to this account and it comes through, Great!
The problem is when i use php for a contact form, it WILL NOT send to the distribution group. It is not going to junk that i can see, and mail trace doesnt show it ever coming in at all. If i change the send to address of mail function to one of our main emails with the organization it comes through. If i change it to send to my gmail it comes through. As soon as i set it to the address of the distribution group, it will not come through. I see no errors on the php, it thinks it sent... but it never does.
Does anyone have an ideas? Or has anyone done this before?
The code in question is very simple...
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$subject = $_REQUEST["subject"];
$msg = $_REQUEST["msg"];
$to = "information#aSecondaryDomain.org";
if (isset($email) && isset($name) && isset($msg)) {
$email_subject = "$name sent you a message";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$to.">\r\n"."Reply-To: ".$to."\r\n" ;
$msg = "From: $name<br/> Email: $email <br/> Subject: $subject <br/> Message: $msg";
$mail = mail($to, $email_subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
EDIT: I have no tried PHPMailer... same reuslt - it will send to my regular work address, and my personal, but not to the distribution group.

PHP 'mail()' Function Not Sending Email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm using a basic script on a 1&1 hosted server:
$recipient = "email#domain.com";
$sender_name = $_POST['name'];
$sender_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html; charset=UTF-8"."\r\n";
$headers .= "From: {$sender_name} <{$sender_email}>"."\r\n";
$headers .= "Reply-to: {$sender_name} <{$sender_email}>"."\r\n";
mail($recipient, $subject, $message, $headers);
..but for some reason am not receiving any emails, nor any errors as per PHP mail() function not sending email instructs.
I thought this may be a server issue but 1&1 states that it is fully supported. I've also sent emails from this server/hosting before using just a recipient, subject and body and so I'm rather unsure as to why it is not working now!
UPDATE
Sending without headers, i.e.:
mail($recipient, $subject, $message);
..does work, so it would appear to be an issue with using the headers?
There may be many reasons, for example you should study what SPF is.
The $sender_email can't be any address, for example you can't put a gmail address and send emails claiming to be that sender, without any authentication, you aren't allowed to send email on behalf on that address, because I could for example send emails putting your address in the from, pretenting to be you when I'm not (I tried to use simple words)
You should use in the From something ending in #yourdomain.com, and set up SPF to allow your server's IP to send emails for that domain. OR otherwise send emails through SMTP (with PHPmailer, for example, it's very easy)
Errors in header does affect the mail delivery. if you send email with wrong headers, for example, let's say the email of the user is user#email.com and his name is "user user", and you send the email to user#email.com it might cause the email to go in spam or not accepted at all if it doesn't match on server.
Gmail, for one, has a name associated with every email id. if you put a wrong name with the email, there's a chance the email might not show up in inbox or sometimes even spams.
"But in the link I attached the example given uses a 'from' header set by a form input?"
if you want your users to be able to send email to you, best idea would be to use your own email address as the sender email. E.g. if your website is at website.com, configure an email like "contact#website.com" and configure your script to use this as From header, you can send it to your own email at the same domain(website.com) or any other email which you authorize. You can add the users's actual details in the mail. that'll ensure the successful delivery of the email always.

email confirmation on signup in php while using local host as aserver

i am new to php and web development. i am trying to confirm user,s email on signup by sending confirmation message on their email. But it is not working is the problem with my using localhost as a server or there is some other problem? here is my code
{
$to = $_POST['email'];
$com_code = md5(uniqid(rand()));
$subject = "Confirmation from OnlineShopping to $_POST['username']";
$header = "OnlineShopping: Confirmation from OnlineShopping";
$message = "Please click the link below to verify and activate your account. rn";
$message .= "http://www.yourname.com/confirm.php?passkey=$com_code";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else
{
echo "Cannot send Confirmation link to your e-mail address";
}
}
my if($sentmail) condition is coming true and i am getting message that your confirmation link has been sent to your email address but i am not receiving any email in my inbox
Nowadays, ISPs usually block any smtp activity, you need a proper mail server. Easiest is to use the gmail smtp server to send mail, just to get you going.
There are plenty of ways to do that, just search "gmail smtp php" and you'll get a bunch of articles.
You need a host to send email with php. I recomend you to use .tk For a free domain and hostinger for free hosting. So you can use this function and test your stuff.
But you can also try changing the php.ini file. http://www.php.net/manual/en/mail.configuration.php

PHPmailer sends to junk email

I've just updated a contact form to use PHPMailer to stop emails being marked as junk, with no luck.
It's a fairly straight forward setup I'm using but its still going into peoples junk mail.
Here is my script, I was wondering if anyone could tell what was wrong?
include_once('../inc/phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$name = $_POST['name'];
$email = $_POST['email'];
$body = "Name: ".$name."\r\n";
$body .= "Email: ".$email."\r\n";
$body .= "Message: ".$_POST['message'];
$mail->From = "mailer#blah.com";
$mail->FromName = "Contact BLah";
$mail->Subject = "Contact From: Blah";
$mail->Body = $body;
$mail->AddAddress("john#blah.com", "john");
$mail->AddAddress("david#blah.com", "david");
if(!$mail->Send()) {
$errorMsg .= "Error sending message, please try again later.";
} else {
$errorMsg .= "Message Sent successfully.";
}
I thought that PHPmailer normally takes care of inserting proper headers?
Any thoughts?
EDIT: Added spam score
-Spam-Status: "score=0.0 tests=none version=3.1.7 cmae=v=1.0 c=1 a=8nJEP1OIZ-IA:10
a=soyWjZv28gkhNSke5wm04A==:17 a=fqdOs_Nl9wd82e3SDigA:9 a=l-lynuxnH-gfU2bevBoA:7
a=wPNLvfGTeEIA:10 a=nymK5Bb5l1cA:10 a=_6wjLm_vFSYA:10 xcat=Undefined/Undefined"
X-Spam-Level: *
EDIT 2: I just tried the script on a different server from the clients and it has the same result. Do I have to send through the SMTP setup for it not to be classed as spam?
Some reasons your mail can get marked spam:
You're sending spam
Your IP, or a block of IPs surrounding your IP has been marked as a spam source on one or more blackhole lists
The content of the email is triggering spam filters.
The recipient has added you to their blacklist
The recipient didn't add you to their whitelist
You're sending a mixed source mail ("From: xyz#example.com", but sending it from "someotherdomain.net")
SPF records for your server are misconfigured/not configured at all
Domain keys are misconfigured/not configured at all
etc...
PHPMailer is a tool. Consider it a hammer. The hammer may have bent the nail, but only because the wielder didn't aim right.
The only way you'll solve this problem is by examining the bounce messages (if any), and whatever showed up in the recipient's mailbox. If they receive the mail, but it goes into a spam folder, then get a copy of the mail and examine its headers. Most spam filters will put their spam score/reasoning in there.
Small hint:
add in a line like so
$mail->AddReplyTo( 'mailer#blah.com', 'Contact BLah' );
It should decrease your SPAM rating significantly.
I was having the same problem using PHPMailer, and here's what fixed the problem for me: set the Sender (this is different and distinct from the "From") to a valid email account for the domain you are sending the email from. This causes PHPMailer to properly set the "envelope-from" information so that the email passes SPF and Sender-ID validation. Without doing this, the "envelope-from" is a OS user id and server combination which will not be verifiable.
Example Code:
$mail = new PHPMailer;
$mail->From = 'from_email#domain.com';
$mail->Sender = 'sender_email#domain.com';
...
It is not necessarily PHPMailer's fault, there are several possible reasons for your server to be blacklisted. You can check here to see if this happened

How do i create an email contact form for my website

I'm looking to create a form on my website where users can enter their email address and a message (however long). Then upon clicking on a submit button, the code, sends the message as an email to my mailbox.
I was wondering if someone knew what code i could use. I am using PHP as my server-side language.
You need to have access to an smtp server somewhere. Assuming you do, use the php mail function to send the mail like so:
$Name = "John doe"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "recipient#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for receiver"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
mail($recipient, $subject, $mail_body, $header);
The smtp server is set in the php.ini file in these two lines:
SMTP = servername
smtp_port = 25
More information at w3schools site.
while the above two answers provide a basic email sending suggestion, there's one thing you should consider, the codes are not secure. spammers can inject Cc: codes and send spam using the form. if they do, your smtp provider may ban your account. try a dedicated mailer, like phpmailer
Try to use PHPForms. It is an ultimate web form builder that allows to create professionally looking email forms within just a few minutes. You will only need to create a PHP form by means of an intuitive interface and generate a code that can be easily copied and pasted to any web page.

Categories