I am building a website for a group of kitchen chefs that allows new members to subscribe without any action from the owner.
When subscribing, new users should receive an e-mail with an activation link, in order to validate their address.
In the beginning I was using the function mail(), but a few addresses weren't receiving anything from the website, so I switched to php mailer.
php mailer code (no errors shown during execution, before I had echoes to check)
require_once ('layout/phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddAddress($mdest);
$mail->SetFrom("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->Subject = "$mobj";
$mail->AltBody = strip_tags ($mmex); // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($mmex);
$mail->Send();
}catch (phpmailerException $e) {
} catch (Exception $e) {
}
$NoreplyMailURL is the (existing) web site e-mail address
$NoreplyMailNAME is something like "Chef Enrico"
$mobj is a sentence like "new password for our website" or "account activation link"
$mmex is the message text formatted in html
Gmail, some hotmail and other users are receiving our e-mails,
some hotmail, bluewin.ch and libero.it are not.
Do you have any suggestions on how to make this code stronger? Is it possible that if receiving providers does not support HTML formatting, the e-mail is not shown? I guess they should see all tags if that was the case, am I wrong?
Thank you very much in advance!
Related
I don't use PHP that often but when I do and I need to write a function to send E-Mails, I just used the mail() function. I have used it on a shared hosting service and I always received the E-Mails from a... well... not an account? A bot? It didn't even have an E-Mail address.
And that's what I want to achieve at this very moment - send an E-Mail to me without really connecting to a SMTP server or going through authentication. How can be that done with PHPMailer? Is it even possible in my case? And if you somehow got my question, how are such E-Mails even called; the ones that... aren't sent by... well... an E-Mail account?
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master\src\Exception.php';
require 'PHPMailer-master\src\PHPMailer.php';
require 'PHPMailer-master\src\SMTP.php';
$mail = new PHPMailer();
$mail->FromName = "A random name";
$mail->addAddress("myemail#gmail.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject is here";
$mail->Body = "Hello, <br>test body ";
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
This did make me laugh a bit...
No, emails can't just magically spring into existence, but there isn't necessarily a direct correlation between email user accounts and addresses.
When you send from a shared hosting account by calling mail(), the mail server knows the account you're sending from, and sometimes doesn't require authentication as a result. This for example is how GoDaddy operates. Unfortunately this approach is very prone to abuse because there is often little preventing you from flat-out lying about who you are. This is why such services are usually a) terrible and b) extremely unreliable for actually delivering messages.
If you don't specify a "from" address, the server will usually make one up from information it does have – typically your user account name, or the name of the user running the script (e.g. www-data), and the hostname of the server you're on, often something like randomnumber.hostingprovider.example.com. Look at the headers of messages you've sent before, and you'll probably see something like that.
Sometimes this address can be the same for all users on a given shared server, so your delivery reputation can depend on what others are sending, which could well be spam, phishing, malware, etc.
This vagueness is terrible for deliverability, so if you host your contact form on such a system, expect messages from it to end up in a spam folder.
If you use SMTP to send via a "proper" account you gain a lot more control and reliability, but unfortunately some hosting providers (GoDaddy again) block outbound SMTP and force you to use their mail servers. This is a way of saying that if you want to send email that will be delivered, use a decent hosting provider.
Once you get control over your sending, you can choose exactly what addresses messages are sent from, subject to authentication constraints including things like SPF, DKIM, and DMARC, but that's another story.
To my knowledge it is possible and have it work correctly each time. In the last year I found that the e-mails I sent using the mail() function would immediately go into my spam box (as well as the junk box of others) causing great confusion. I upgraded to PHPMailer to solve this and I have never set it up to use SMTP and works just fine.
The code I have is:
$mail = new PHPMailer(true);
$html = $email->get(); // This gets a standard HTML Template to use as the base of the e-mail.
$title = 'Title for Inside the HTML';
$subject = 'The E-mail Subject';
$html = str_replace('[title]',$title,$html); //Use this to replace the [title] element in my HTML Template with $title.
$html = str_replace('[date]',date("F j, Y g:i A"),$html); // Again, replaces [date] in the HTML Template with the current date and time.
$body = 'Hello My Good Friend,<br>This is just a simple <strong>HTML Message</strong><br><br>Thank you.';
$html = str_replace('[body]',$body,$html); //Add my HTML Message to the HTML Template.
try {
//Recipients
$mail->setFrom('noreply#example.com', 'My Organization');
$mail->addAddress($row["email"], $row["fullname"]); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = strip_tags($html,'<br>');
$mail->send();
//Return Success Message
$rMsg .= '<div class="alert alert-success">
<strong>Success: </strong> We have e-mailed the warning.</div>';
} catch (Exception $e) {
$rMsg .= '<div class="alert alert-danger">
<strong>Error: </strong> There was an error e-mailing the warning.</div>';
}
So, what I am doing is I am using Sendgdrid's API (PHP API Library) to send e-mails. The sending part works, I just want to overhaul it a little to add some more customization, but I am not sure if it's possible. I am using CodeIgniter so I am skipping some code, but you get the general idea.
What I want to achieve is to customize this part
I want the company logo to appear there instead of the automatically generated A.
I want to achieve this:
Is there some header I need to modify?
My mail sending process is this (fictional data):
$email = new \SendGrid\Mail\Mail();
$email->setFrom("noreply#mail.com", "Sender Name");
$email->setSubject("Mail Subject");
$email->addTo($mail_data->email, $mail_data->name);
$email->addContent(
"text/html", $this->load->view('mails/recovery', $mail_data, TRUE)
);
$sendgrid = new \SendGrid($this->config->item('sendgrid_api_key'));
try {
$response = $sendgrid->send($email);
// do some stuff
} catch (Exception $e) {
// do some other stuff
}
So the mail arrives correct and everything, but is there a way to add like an avatar for the mail sender? Like the company's logo or something. Is there a way to do this via code?
I checked something about creating a google account, but what if I am using a noreply# address and also what happens if for another mail I use a different address? I'd have to add google accounts to each one?
I use PHPMailer to send some email to one ore more detinations.
However I encountered a problem. Here comes:
This is my code:
$mail2->AddAddress($cliemail);
$mail2->AddCC('important#destination.com');
$mail2->SetFrom('important#destination.com', "...");
$mail2->AddReplyTo('important#destination.com', "...");
$mail2->Subject = 'Email subject... '.$wnume;
$mail2->AltBody = 'To be able to see this email please...';
$mail2->MsgHTML(file_get_contents('contents.html'));
$mail2->AddAttachment('teste/quiz.pdf'); // attachment
$mail2->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
So, my email must be sent from important#destination.com (it is another email of course)
And it gets sent to $cliemail, but it does not arrive to the CC (important#destination.com)
So apparently I cannot send an email to myself... Is this normal?
What could be the cause?
I tried to remove the AddCC and try to simply send the email directly to important#destination.com but it still does not arrive.
Is this some setting in my email account that I must do in order to be able to receive an email from myself?
So:
When I send the email to a third party email address using the above
code, it works
When I send the email to myself (the sender) (the same address that is the "SetFrom") it does not arrive
Even if I set the CC with my address, it still does not arrive, while it arrives at the main destination address ($cliemail)
Please help
I'm a newbie in PHP. My goal is to send an email to the user registered in my system.
Here is my code:
$msg= " Hi $gen./$lName, copy and paste this code in order to activate
your account copy and paste this code:$num" ;
$email = "$emailadd";
$name= "GoodFaith Network Marketing Inc.";
$subject = "Account Activation";
$message = wordwrap($msg,70) ;
$sender = "cjoveric#myalphaedge.com";
$mail_header="From: ".$name."<". $sender. ">\r\n";
$sendmail=mail($email, $subject,$message, $mail_header );
I was able to send an email, but my problem is I want to know if the user's email address exists in Yahoo, GMail or other mail services.
Is there any way I could filter out invalid emails?
Use SMTP mail the best and easy to use SMTP.
$mail->IsHTML(true);
$mail->From="admin#example.com";
$mail->FromName="Example.com";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $from_name); // indicates ReplyTo headers
$mail->AddCC('cc#phpgang.com.com', 'CC: to phpgang.com');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send())
{
$error = 'Mail error: '.$mail->ErrorInfo;
return true;
}
else
{
$error = 'Message sent!';
return false;
}
Not really. About the best you can do is send an email and see if the user responds to it.
Doing a regex check on email address can be frustrating for some users, depending on regex. So, I recommend to skip your regex test and just send the verification email with a confirmation link -- anything else will leave your application brittle and subject to breakage as soon as someone comes up with a new DNS or SMTP extension. It's best to get that dealt with when their paying attention.
For example:
-a confirmation code that needs to be filled in your website
-a link, going to your website, that needs to be visited
And still it is uncertain whether the email is existing afterwards, as it is easy to simply create a temporary email to pass the validation and delete it afterwards.
Instead of validating email addresses you can use the Google API to let your users sign in using their account. It is also possible to use OpenID on a similar way. Though, even this is not 100% perfect. But heay, nothing is 100%. We all try to make is work as we want as much as possible. That's it.
PS: ICANN is expected to approve UNICODE domain names Real Soon Now. That will play merry hell with Regex patterns for email addresses.
I'm using PHPmailer to send an email from a website to the website owner. It works fine to some addresses (e.g. my Gmail account), and it used to work to the owner's address, but he's recently changed from POP to IMAP and now he doesn't receive emails from the website. He does receive emails from other sources. This is the code:
$mail = new PHPMailer(true);
try {
$mail->AddAddress($to, 'Example To');
$mail->SetFrom('example#example.com', 'Example');
$mail->AddReplyTo('example#example.com', 'Example');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($message);
$mail->Send();
//echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Any advice much appreciated.
Thanks!
G
That has nothing to do with the PHP code. The IMAP protocol is just responsible for fetching the mails from the server as a user (with IMAP the mails stay on server ... you have an open stream for a long time ...).
So: did he switch to another email provider? Maybe it is in spam. Check the maillog! ;-)