我一直在尝试使用PHP发送电子邮件,但是每当消息太长时,邮件就不会发送。
这是我到目前为止所拥有的:
$name= $_POST["name"];
$email= $_POST["email"];
$comment= $_POST["text"];
$msg= "Naam: " . $name . "\r\nEmail: " . $email . "\r\nBericht: " . $comment;
mail("test.test@live.nl", "Website", $msg);
答案 0 :(得分:0)
我不建议您采用这种方式,因为电子邮件可能会收到垃圾邮件。
我建议您使用phpmailer。
我给你举个例子:
try {
$mail = new PHPMailer(true);
//Server settings
$mail->isSMTP();
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example@example.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('example@example.com', 'name');
if(is_array($this->Emails)){
foreach($this->Emails as $email){
$mail->addAddress($email); // Add a recipient
}
}
else{
$mail->addAddress($this->Emails); // Add a recipient
}
if(isset($this->Attachments)){
if(is_array($this->Attachments)){
foreach($this->Attachments as $Attachment){
$mail->addAttachment($Attachment); // Add attachments
}
}
else{
$mail->addAttachment($this->Attachments); // Add attachments
}
}
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $this->Subject;
$mail->Body = $this->Body;
$mail->send();
return true;
答案 1 :(得分:-1)
我之所以建议这样做,是因为您现在用PHP发送电子邮件的方法已经过时了,它将为您省去很多麻烦。
composer require swiftmailer/swiftmailer
安装Swiftmailer 更容易管理电子邮件